Add a column to the middle of the tab delimited file (sed / awk / whatever)

I am trying to add a column (with content "0") in the middle of an existing tab delimited text file. I suppose sed or awk will do what I want. I have seen various solutions on the Internet that do something like this, but they are not explained simply enough for me to change!

I currently have this content:

 Affx-11749850 1 555296 CC 

I need this content

 Affx-11749850 1 0 555296 CC 

Using the awk '{$3=0}1' filename command ruined my formatting AND replaced column 3 with 0, instead of adding a third column with 0.

Any help (with an explanation!), So I can solve this problem and future similar problems, very valuable.

+4
source share
2 answers

Using the implicit rule { print } and adding 0 to the second column:

 awk '$2 = $2 FS "0"' file 

Or using sed , assuming single-space delimiters:

 sed 's/ / 0 /2' file 

Or perl :

 perl -lane '$, = " "; $F[1] .= " 0"; print @F' 
+8
source
 awk '{$2=$2" "0; print }' your_file 

checked below:

 > echo "Affx-11749850 1 555296 CC"|awk '{$2=$2" "0;print}' Affx-11749850 1 0 555296 CC 
+1
source

All Articles