Skip to main content
Add an explanation.
Source Link
Stephen Kitt
  • 481.5k
  • 60
  • 1.2k
  • 1.4k

Memorise the previous line:

awk 'NR > 1 { print prev, $1 } { prev = $0 } END { print prev }'

This processes the input as follows:

  • if the current line is the second or greater, print the previous line (stored in prev, see the next step) and the first field of the current line, separated by the output field separator (the space character by default);
  • in all cases, store the current line in the prev variable;
  • at the end of the file, print the previous line.

Memorise the previous line:

awk 'NR > 1 { print prev, $1 } { prev = $0 } END { print prev }'

Memorise the previous line:

awk 'NR > 1 { print prev, $1 } { prev = $0 } END { print prev }'

This processes the input as follows:

  • if the current line is the second or greater, print the previous line (stored in prev, see the next step) and the first field of the current line, separated by the output field separator (the space character by default);
  • in all cases, store the current line in the prev variable;
  • at the end of the file, print the previous line.
Source Link
Stephen Kitt
  • 481.5k
  • 60
  • 1.2k
  • 1.4k

Memorise the previous line:

awk 'NR > 1 { print prev, $1 } { prev = $0 } END { print prev }'