Skip to main content
deleted 13 characters in body
Source Link
Sergiy Kolodyazhnyy
  • 16.9k
  • 12
  • 58
  • 111

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.

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 now is first word is printed out , 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.

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.

deleted 13 characters in body
Source Link
Sergiy Kolodyazhnyy
  • 16.9k
  • 12
  • 58
  • 111

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

Remember that up to now we printed a formatted string without new line character. Thus , that's what {printf " %s\n", $1} lets us achieve - print the current line'sis being done now is first filedword is printed out , newline inserted, and then insert new line. Final code block (the {printf "%s",$0} part) prints whole line itself ,( but again - leaving out new-line so that we can splicedoesn't terminate with newline character). Thus next first word when we get to next lineinserted will remain on the same line. The processProcess continues on and on, until 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" + print(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.

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, that's what {printf " %s\n", $1} lets us achieve - print the current line's first filed and then insert new line. Final code block (the {printf "%s",$0} part) prints whole line , but again - leaving out new-line so that we can splice first word when we get to next line. The process continues on and on, until 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 for index,line in enumerate(sys.stdin): if index == 0: print(line.strip(),end=" ") continue words = line.strip().split() print(words[0])  print(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.

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 now is first word is printed out , 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.

deleted 13 characters in body
Source Link
Sergiy Kolodyazhnyy
  • 16.9k
  • 12
  • 58
  • 111

Alternative awk approach:

$ awk 'NR == 1{printf "%s", $0;next}{printf " %s\n"%s\n%s", $1}; {printf "%s",$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, that's what {printf " %s\n", $1} lets us achieve - print the current line's first filed and then insert new line. Final code block (the {printf "%s",$0} part) prints whole line , but again - leaving out new-line so that we can splice first word when we get to next line. The process continues on and on, until 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 for index,line in enumerate(sys.stdin): if index == 0: print(line.strip(),end=" ") continue words = line.strip().split() print(words[0]) print(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.

Alternative awk approach:

$ awk 'NR == 1{printf "%s", $0;next}{printf " %s\n", $1}; {printf "%s",$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, that's what {printf " %s\n", $1} lets us achieve - print the current line's first filed and then insert new line. Final code block (the {printf "%s",$0} part) prints whole line , but again - leaving out new-line so that we can splice first word when we get to next line. The process continues on and on, until 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 for index,line in enumerate(sys.stdin): if index == 0: print(line.strip(),end=" ") continue words = line.strip().split() print(words[0]) print(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.

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, that's what {printf " %s\n", $1} lets us achieve - print the current line's first filed and then insert new line. Final code block (the {printf "%s",$0} part) prints whole line , but again - leaving out new-line so that we can splice first word when we get to next line. The process continues on and on, until 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 for index,line in enumerate(sys.stdin): if index == 0: print(line.strip(),end=" ") continue words = line.strip().split() print(words[0]) print(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.

deleted 16 characters in body
Source Link
Sergiy Kolodyazhnyy
  • 16.9k
  • 12
  • 58
  • 111
Loading
added 854 characters in body
Source Link
Sergiy Kolodyazhnyy
  • 16.9k
  • 12
  • 58
  • 111
Loading
Source Link
Sergiy Kolodyazhnyy
  • 16.9k
  • 12
  • 58
  • 111
Loading