正则表达式 $'s/\r$//' 有什么作用?

正则表达式 $'s/\r$//' 有什么作用?

Todd Schneider 在他的命令中使用了以下命令纽约市出租车数据分析为 PostresSQL 准备 CSV 文件。

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

第二个 sed 命令删除空行。第一个是做什么的?

答案1

根据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.

因此删除行尾的$'s/\r$//'ASCII 。CR这是将 Windows 行结束符转换为 UNIX 行结束符的粗略方法。

相关内容