O que o Regex $ 's / \ r $ //' faz?

1

Todd Schneider usa o seguinte comando em sua NYC Taxi Data Analysis para preparar um arquivo CSV para o PostresSQL.

sed $'s/\r$//' $filename | sed '/^$/d' | ...

O segundo comando sed remove linhas em branco. O que o primeiro faz?

    
por rhaskett 30.10.2017 / 18:00

1 resposta

2

De acordo com man bash :

  Words of the form $'string' are treated specially.  The word expands to
   string,  with backslash-escaped characters replaced as specified by the
   ANSI C standard.  Backslash escape sequences, if present,  are  decoded
   as follows:                                      
          \a     alert (bell)                       
          \b     backspace                          
          \e                                        
          \E     an escape character                
          \f     form feed                          
          \n     new line                           
          \r     carriage return                    
          \t     horizontal tab                     
          \v     vertical tab                       
          \     backslash                          
          \'     single quote                       
          \"     double quote                       
          \?     question mark                      
          \nnn   the  eight-bit  character  whose value is the octal value
                 nnn (one to three digits)          
          \xHH   the eight-bit character whose value  is  the  hexadecimal
                 value HH (one or two hex digits)   
          \uHHHH the  Unicode (ISO/IEC 10646) character whose value is the
                 hexadecimal value HHHH (one to four hex digits)
          \UHHHHHHHH                                
                 the Unicode (ISO/IEC 10646) character whose value is  the
                 hexadecimal value HHHHHHHH (one to eight hex digits)
          \cx    a control-x character              
  The  expanded  result  is  single-quoted, as if the dollar sign had not
   been present.

Consequentemente, $'s/\r$//' exclui um ASCII CR no final da linha. É uma maneira grosseira de converter as terminações de linha do Windows em finais de linha do UNIX.

    
por 30.10.2017 / 18:11