How to print first column of next line in current line?

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.

Alternative awk approach:

$ awk 'NR == 1{printf "%s", $0;next}{printf " %s\n%s", $1,$0}' input.txt                                    
abc 123 abc
abc 789 bcd
bcd 456 acb
acb 135

The way this works is simple: first line is special case - we print it without new-line, and tell awk to go to next line without executing other code blocks. After that, NR == 1{printf "%s", $0;next} is skipped, but other parts do the job.

Remember that up to now we printed a formatted string without new line character. Thus , what is being done by printf " %s\n%s",$1,$0 now is first word is printed out ( and because there was no newline, it remains on the same line of output) , newline inserted, and then whole line itself ( but doesn't terminate with newline character). Thus next first word inserted will remain on the same line. Process continues on and on till we reach the end of file.

Possible improvement is to include END{print ""} block to insert final newline. In certain cases where resulting file is to be processed by other scripts it might be desirable.


While the user requested AWK specifically, same approach with printing formatted strings can be taken with other languages, for instance Python. Python alternative provided for those curious about how this can be implemented in other languages:

#!/usr/bin/env python
from __future__ import print_function
import sys

old = None
for index,line in enumerate(sys.stdin):
    if index == 0:
        print(line.strip(),end=" ")
        continue
    words = line.strip().split()
    print(words[0] + "\n" + line.strip(),end=" ")

And usage like so:

$ ./append_first.py < input.txt                            
abc 123 abc
abc 789 bcd
bcd 456 acb
acb 135

Same idea about final newline applies here.


Here is an ugly sed way just for fun

sed '2,$ s/[^ ]\+/& &/; 2,$ s/ /\n/' file | paste -d ' ' - -
abc 123 abc
abc 789 bcd
bcd 456 acb
acb 135 

Explanation

  • 2,$ from the second line to the last
  • s/[^ ]\+/& &/ double the first set of non whitespace characters
  • ; separates commands, like in the shell
  • s/ /\n/ replace the first space with a newline
  • paste -d ' ' - - stick this mess together (append second line to third, fourth line to third, etc)