Lucia
Lucia

Reputation: 911

printing lines with more than three fields and the line above

I have a file such as follows:

scaf_1245 457 1
457
scaf_457 784 5
457 4578 784 487 4578
scaf_847 487 4
457 487 487

How could I print the lines that have more than three fields (the lines that contain only numbers) with the line above it with awk and grep?

I know:

awk 'NF>3' file 

grep -B 1 pattern

Desired output:

scaf_457 784 5
457 4578 784 487 4578

but don't know how to combine these.

Upvotes: 0

Views: 2021

Answers (1)

fedorqui
fedorqui

Reputation: 289905

Let grep do it:

grep -B1 ".* .* .* .*" file

This looks for those lines having at least four blocks of text. Then, -B1 prints the match and 1 line Before the matched line.

With awk you can say:

awk 'NF>3 {print f; print} {f=$0}' file

It stores the current line in a variable f with f=$0. Then, when NF>3 is matched (that is, more than 3 fields in a line), it prints the stored line together with the current one. This can have the problem of printing repeated lines.

For your given file it returns:

scaf_457 784 5
457 4578 784 487 4578

From man grep:

-B NUM, --before-context=NUM

Print NUM lines of leading context before matching lines. Places a line containing a group separator (described under --group-separator) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given.

Upvotes: 3

Related Questions