0

For example suppose I have the following piece of data

ABC,3,4
,,ExtraInfo
,,MoreInfo
XYZ,6,7
,,XyzInfo
,,MoreXyz
ABC,1,2
,,ABCInfo
,,MoreABC

It's trivial to get grep to extract the ABC lines. However if I want to also grab the following lines to produce this output

ABC,3,4
,,ExtraInfo
,,MoreInfo
ABC,1,2
,,ABCInfo
,,MoreABC

Can this be done using grep and standard shell scripting?

Edit: Just to clarify there could be a variable number of lines in between. The logic would be to keep printing while the first column of the CSV is empty.

deltanovember
  • 42,611
  • 64
  • 162
  • 244

3 Answers3

5

grep -A 2 {Your regex} will output the two lines following the found strings.

Update: Since you specified that it could be any number of lines, this would not be possible as grep focuses on matching on a single line see the following questions:

Community
  • 1
  • 1
Bobby
  • 18,217
  • 15
  • 74
  • 89
2

You can use this, although a bit hackity due to the grep at the end of the pipeline to mute out anything that does not start with 'A' or ',':

$ sed -n '/^ABC/,/^[^,]/p' yourfile.txt| grep -v '^[^A,]'

Edit: A less hackity way is to use awk:

$ awk '/^ABC/ { want = 1 } !/^ABC/ && !/^,/ { want = 0 } { if (want) print }' f.txt

You can understand what it does if you read out loud the pattern and the thing in the braces.

holygeek
  • 15,653
  • 1
  • 40
  • 50
1

The manpage has explanations for the options, of which you want to look at -A under Context Line Control.

casualcoder
  • 4,770
  • 6
  • 29
  • 35