简体   繁体   中英

Grep a pattern and also print the header of the file

I have a file that contains information like the following:

header info line 1 
header info line 2
header info line 3
   ....
process: 1
   info 1
   info 2
   info 3

process: 2
   info 1
   info 2
   info 3

process: 3
   info 1
   info 2
   info 3

What I want to do is grep for one of the process lines (ex "process: 2") then delete the other process while keeping the header information. What I know is the number of lines after the "process: #" (Lets just use 3 for this example). What I don't know is how many process numbers there are. What I was trying was:

 grep "process: 2" -A 3 file.txt

However I lose the header information. I want to keep the header info but get rid of all the other process info. I feel like I can do this with an egrep but I'm not sure how.

My desired output is the following:

header info line 1
header info line 2
header info line 3
   ....
process: 2
   info 1
   info 2
   info 3

Better use awk for this:

$ awk -v N=3 -v header=4 '/process: 2/{c=N+1} NR<=header || c&&c--;' file
header info line 1 
header info line 2
header info line 3
   ....
process: 2
   info 1
   info 2
   info 3

This uses printing with sed or awk a line following a matching pattern together with a check on the lines on the header.

Explanation

  • -v N=3 -v header=4 provide the amount of lines the header has ( header ) and how many lines after the match should be printed ( N ).
  • /process: 2/{c=N+1} when process: 2 line is seen, set the variable c (from counter).
  • c&&c-- evaluate c . If its value is bigger than 0 , it evaluates as True, so that the line is printed. Also, decrement the value so that just N lines are printed.
  • NR<=header if the line number is equal or lower than the given value header , it evaluates to True and the line is printed.

sed's nice too

sed -n '1,4p; /^process: 2$/ {N;N;N;p;q}' file.txt

That will print the first 4 lines, and when we see the desired pattern, read the next 3 lines, print and quit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM