bash grep newline

后端 未结 11 1748
渐次进展
渐次进展 2021-01-04 12:38

[Editorial insertion: Possible duplicate of the same poster\'s earlier question?]

Hi, I need to extract from the file:

first
second
         


        
11条回答
  •  囚心锁ツ
    2021-01-04 13:16

    I don't think grep is the way to go on this.

    If you just want to strip the first line from any file (to generalize your question), I would use sed instead.

    sed '1d' INPUT_FILE_NAME
    

    This will send the contents of the file to standard output with the first line deleted.

    Then you can redirect the standard output to another file to capture the results.

    sed '1d' INPUT_FILE_NAME > OUTPUT_FILE_NAME
    

    That should do it.

    If you have to use grep and just don't want to display the line with first on it, then try this:

    grep -v first INPUT_FILE_NAME 
    

    By passing the -v switch, you are telling grep to show you everything but the expression that you are passing. In effect show me everything but the line(s) with first in them.

    However, the downside is that a file with multiple first's in it will not show those other lines either and may not be the behavior that you are expecting.

    To shunt the results into a new file, try this:

    grep -v first INPUT_FILE_NAME > OUTPUT_FILE_NAME
    

    Hope this helps.

提交回复
热议问题