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
3
  • Where does F11 come from? Is it computed from F1..F10 or is it stored in another file?
    – mouviciel
    Commented Sep 26, 2011 at 7: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
    Commented Sep 26, 2011 at 8:34
  • 2
    Possible duplicate: stackoverflow.com/q/1636755/45249. My answer there is to use paste instead of awk but other answers may be useful as well.
    – mouviciel
    Commented Sep 26, 2011 at 8:56

4 Answers 4

33

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

2
  • 2
    Or even awk '$0=$0"F11"' original.file
    – fedorqui
    Commented Sep 27, 2013 at 11:26
  • 1
    awk -v OFS=x '{print $0, "F11"}' if you want to change the output field separator (x) Commented Aug 1, 2016 at 0:00
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
2
  • 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
    Commented Jan 10, 2019 at 12:11
  • 1
    @applequist 1 is the default condition which executes the default action which is print $0. See unix.stackexchange.com/q/63891/273492
    – kvantour
    Commented Jan 10, 2019 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

1
  • 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
    Commented May 9, 2016 at 10:46
0

If you data is tabulated, this is the only thing worked for me:

awk '{print $0 "\tVALUE"}' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.