grep 命令。 “^1”不起作用

grep 命令。 “^1”不起作用
ttys000 -bash: cat n_modified.txt 
     1  calibre library
     2  desktop
     3  documents
     4  downloads
     5  library
     6  movies
     7  music
     8  pictures
     9  public
    10  sites
    11  myfirstdirectory
    12  vasylgolub.conf
    13  CALIBRE LIBRARY
    14  DESKTOP
    15  DOCUMENTS
    16  DOWNLOADS
    17  LIBRARY
    18  MOVIES
    19  MUSIC
    20  PICTURES
    21  PUBLIC
    22  SITES
    23  MYFIRSTDIRECTORY
    24  VASYLGOLUB.CONF
    25                 
    26         
    27           
    28           
    29         
    30        
    31       
    32          
    33        
    34       
    35                  
    36            .    
    37  CALIBRE LIBRARY
    38  DESKTOP
    39  DOCUMENTS
    40  DOWNLOADS
    41  LIBRARY
    42  MOVIES
    43  MUSIC
    44  PICTURES
    45  PUBLIC
    46  SITES
    47  MYFIRSTDIRECTORY
    48  VASYLGOLUB.CONF

ttys000 -bash: grep '^1' n_modified.txt 

ttys000 -bash:

如上所述,grep '^1' n_modified.txt没有给我以 1 开头的句子。为什么?

答案1

例如这一行:

    10  sites

space从那时开始1

你应该使用这个命令:

grep '^\s*1' n_modified.txt 

看这个例子:http://regexr.com/39cas

答案2

因为输入的每一行都以空格开头,而不是数字 1,所以你的正则表达式将会失败。

尝试:

$ LC_ALL=C grep '^[[:blank:]]*1' file
     1  calibre library
    10  sites
    11  myfirstdirectory
    12  vasylgolub.conf
    13  CALIBRE LIBRARY
    14  DESKTOP
    15  DOCUMENTS
    16  DOWNLOADS
    17  LIBRARY
    18  MOVIES
    19  MUSIC

或者:

awk '$1 ~ /^1/' file

相关内容