21

How can I add a new column to a file using awk?

original.file

F1 F2 F3 ..F10 

add F11 to original.file

F1 F2 F3 ..F10 F11
user438383
  • 5,716
  • 8
  • 28
  • 43
user951487
  • 845
  • 7
  • 19
  • 30
  • Where does F11 come from? Is it computed from F1..F10 or is it stored in another file? – mouviciel Sep 26 '11 at 07:48
  • how is it stored in another file? each field per line? or also a field from a CSV, or something like that? Is there any mapping relation between the two files? better give us examples, original.file, "another.file" and your expected output. – Kent Sep 26 '11 at 08:34
  • 2
    Possible duplicate: http://stackoverflow.com/q/1636755/45249. My answer there is to use `paste` instead of `awk` but other answers may be useful as well. – mouviciel Sep 26 '11 at 08:56

3 Answers3

33

awk '{print $0, "F11"}' original.file

nick
  • 643
  • 1
  • 5
  • 11
24

If you want to add a column to a file, you can do the following.

remark: We assume that the field separator FS equals to the string "fs". You can replace this by anything, or if you just use <blanks> as field separator, you can remove the BEGIN{FS=OFS="fs"} part in any of the following solutions.

add a column at the beginning:

awk 'BEGIN{FS=OFS="fs"}{print value OFS $0}' file

add a column at the end:

awk 'BEGIN{FS=OFS="fs"}{print $0 OFS value}' file

add a column before column n:

awk 'BEGIN{FS=OFS="fs"}{$n = value OFS $n}1' file

add column after column n:

awk 'BEGIN{FS=OFS="fs"}{$n = $n OFS value}1' file

add a column before each of column n1 < n2 < ... < nm: (start at the back)

awk 'BEGIN{FS=OFS="fs"; split("n1,n2,n3,...,nm",a,",")}
     {for(i=m;i>0;--i) $(a[i]) = value OFS $(a[i])}1' file

or for different values

awk 'BEGIN{FS=OFS="fs"; split("n1,n2,n3,...,nm",a,","); split("value1,value2,...,valuem",v,",")}
     {for(i=m;i>0;--i) $(a[i]) = v[i] OFS $(a[i])}1' file

add a column after each of column n1 < n2 < ... < nm: (start at the back)

awk 'BEGIN{FS=OFS="fs"; split("n1,n2,n3,...,nm",a,",")}
     {for(i=m;i>0;--i) $(a[i]) = $(a[i]) OFS value}1' file

or for different values

awk 'BEGIN{FS=OFS="fs"; split("n1,n2,n3,...,nm",a,","); split("value1,value2,...,valuem",v,",")}
     {for(i=m;i>0;--i) $(a[i]) = $(a[i]) OFS v[i]}1' file
kvantour
  • 25,269
  • 4
  • 47
  • 72
  • Oh great. very helpful. Just wondering where does the 1 come from in 'add a column after/before column n' and what's the meaning ? – applequist Jan 10 '19 at 12:11
  • 1
    @applequist 1 is the default condition which executes the default action which is `print $0`. See https://unix.stackexchange.com/q/63891/273492 – kvantour Jan 10 '19 at 12:27
12

try:

awk 'BEGIN{getline to_add < "f3"}{print $0,to_add}' f

Reads the column to add from file "f3" and saves it in the variable to_add. After that it adds the column to each line of file f.

HTH Chris

Chris
  • 2,987
  • 2
  • 20
  • 21
  • 2
    This adds the first line of f3 to each column for me. Should it be awk '{getline to_add < "f3"; print $0,to_add}' f instead? – Ian Hinder May 09 '16 at 10:46