Question

I have this issue written on the title, I'm trying this:

awk '{tamal = match($0,/Pattern/)}{ if (tamal == NULL) ;else print $0;} {if (tamal == NULL) ;else NR=$NR+1;print $0 }' File

Well, for some of you, it obviously doesn't work, I have been trying using different ways to modify the NR, but I've been doing it wrong.

What I want is like this:

Taco de pollo 
213451346257 
Taco de carne 
4358363693 
Ensalada 
432523498 
El mejor Taco 
234238485

and obtain this if the pattern is:

Taco

:

Taco de pollo 
213451346257 
Taco de carne 
4358363693 
El mejor Taco 
234238485
Was it helpful?

Solution

The pattern match prints the previous line, then the matched line. Then prev variable is assigned to the current line - when the following line is parsed prev holds the previous line.

awk  '/Taco/ {print prev;print $0} {prev=$0}' datafile

OTHER TIPS

This grep should do:

grep -B 1 Taco datafile

-B 1 Before one line.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top