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.