Skip to main content
added 535 characters in body
Source Link
slm
  • 380k
  • 127
  • 793
  • 897

Using grep

What if you used the after context switch to grep (-A) and specified a 1 to get the first line after a match?

$ grep -E -A 1 "name=|age=|class=|marks=" student.txt 

Example

Sample file.

$ cat student.txt name= 1st line after name 2nd line after name age= 1st line after age 2nd line after age class= 1st line after class 2nd line after class marks= 1st line after marks 2nd line after marks 

Then when you execute the above command:

$ grep -E -A 1 "name=|age=|class=|marks=" student.txt name= 1st line after name -- age= 1st line after age -- class= 1st line after class -- marks= 1st line after marks 

Using awk

As @RahulPatil suggested using the construct to awk:

'/string1/||/string2/||...' 

Something like this would do what you're looking for.

$ awk ' /name=/||/age=/||/class=/||/marks=/{nr[NR]; nr[NR+1]}; NR in nr ' student.txt 

Example

$ awk ' /name=/||/age=/||/class=/||/marks=/{nr[NR]; nr[NR+1]}; NR in nr ' student.txt name= 1st line after name age= 1st line after age class= 1st line after class marks= 1st line after marks 

Using grep

What if you used the after context switch to grep (-A) and specified a 1 to get the first line after a match?

$ grep -E -A 1 "name=|age=|class=|marks=" student.txt 

Example

Sample file.

$ cat student.txt name= 1st line after name 2nd line after name age= 1st line after age 2nd line after age class= 1st line after class 2nd line after class marks= 1st line after marks 2nd line after marks 

Then when you execute the above command:

$ grep -E -A 1 "name=|age=|class=|marks=" student.txt name= 1st line after name -- age= 1st line after age -- class= 1st line after class -- marks= 1st line after marks 

Using grep

What if you used the after context switch to grep (-A) and specified a 1 to get the first line after a match?

$ grep -E -A 1 "name=|age=|class=|marks=" student.txt 

Example

Sample file.

$ cat student.txt name= 1st line after name 2nd line after name age= 1st line after age 2nd line after age class= 1st line after class 2nd line after class marks= 1st line after marks 2nd line after marks 

Then when you execute the above command:

$ grep -E -A 1 "name=|age=|class=|marks=" student.txt name= 1st line after name -- age= 1st line after age -- class= 1st line after class -- marks= 1st line after marks 

Using awk

As @RahulPatil suggested using the construct to awk:

'/string1/||/string2/||...' 

Something like this would do what you're looking for.

$ awk ' /name=/||/age=/||/class=/||/marks=/{nr[NR]; nr[NR+1]}; NR in nr ' student.txt 

Example

$ awk ' /name=/||/age=/||/class=/||/marks=/{nr[NR]; nr[NR+1]}; NR in nr ' student.txt name= 1st line after name age= 1st line after age class= 1st line after class marks= 1st line after marks 
Source Link
slm
  • 380k
  • 127
  • 793
  • 897

Using grep

What if you used the after context switch to grep (-A) and specified a 1 to get the first line after a match?

$ grep -E -A 1 "name=|age=|class=|marks=" student.txt 

Example

Sample file.

$ cat student.txt name= 1st line after name 2nd line after name age= 1st line after age 2nd line after age class= 1st line after class 2nd line after class marks= 1st line after marks 2nd line after marks 

Then when you execute the above command:

$ grep -E -A 1 "name=|age=|class=|marks=" student.txt name= 1st line after name -- age= 1st line after age -- class= 1st line after class -- marks= 1st line after marks