Skip to main content
fixed typos
Source Link
Archemar
  • 32.3k
  • 18
  • 75
  • 107

I am trying to take a text file called 'input', of the form

line1

line2

line3

PATTERN

x y z

x y z

lineN

line1 
line2
line3
PATTERN
x y z
x y z
lineN

and use awk to place a number (1) after each "x y z" line. I need the lines before the PATTERN and after the "x y z' lines (lineN) in the output. The output I needs looks like this:

line1

line2

line3

PATTERN

x y z 1

x y z 1

lineN

line1
line2
line3
PATTERN
x y z   1 
x y z   1
lineN

What I have so far is: awk '/PATTERN/ {getline; print $0 " 1" }' < input > output

awk '/PATTERN/ {getline; print $0 "   1" }' < input > output 

This gives:

x y z 1

x y z   1

Is it possible to set up a loop to get awk to append an arbitrary number of "x y z" lines or stop after the "x y z" type lines finish?

Thank you.

I am trying to take a text file called 'input', of the form

line1

line2

line3

PATTERN

x y z

x y z

lineN

and use awk to place a number (1) after each "x y z" line. I need the lines before the PATTERN and after the "x y z' lines (lineN) in the output. The output I needs looks like this:

line1

line2

line3

PATTERN

x y z 1

x y z 1

lineN

What I have so far is: awk '/PATTERN/ {getline; print $0 " 1" }' < input > output

This gives:

x y z 1

Is it possible to set up a loop to get awk to append an arbitrary number of "x y z" lines or stop after the "x y z" type lines finish?

Thank you.

I am trying to take a text file called 'input', of the form

line1 
line2
line3
PATTERN
x y z
x y z
lineN

and use awk to place a number (1) after each "x y z" line. I need the lines before the PATTERN and after the "x y z' lines (lineN) in the output. The output I needs looks like this:

line1
line2
line3
PATTERN
x y z   1 
x y z   1
lineN

What I have so far is:

awk '/PATTERN/ {getline; print $0 "   1" }' < input > output 

This gives:

x y z   1

Is it possible to set up a loop to get awk to append an arbitrary number of "x y z" lines or stop after the "x y z" type lines finish?

Source Link

Appending an input file with awk after a pattern (looping?)

I am trying to take a text file called 'input', of the form

line1

line2

line3

PATTERN

x y z

x y z

lineN

and use awk to place a number (1) after each "x y z" line. I need the lines before the PATTERN and after the "x y z' lines (lineN) in the output. The output I needs looks like this:

line1

line2

line3

PATTERN

x y z 1

x y z 1

lineN

What I have so far is: awk '/PATTERN/ {getline; print $0 " 1" }' < input > output

This gives:

x y z 1

Is it possible to set up a loop to get awk to append an arbitrary number of "x y z" lines or stop after the "x y z" type lines finish?

Thank you.