AWK print record posterior to matched record (the post that already exists is about fields, not records)

StackOverflow https://stackoverflow.com/questions/21220210

문제

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
도움이 되었습니까?

해결책

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

다른 팁

This grep should do:

grep -B 1 Taco datafile

-B 1 Before one line.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top