How to replace the content of specific column with awk? Tab Delimited File

7,352

You need to set Output Field Separator, to tab \t:

One way to do it is with -v option:

awk -vOFS='\t' '{$3 = "AD"; print}' file

another possibility inside awk, say in the BEGIN block:

awk 'BEGIN{OFS="\t"}{$3 = "AD"; print}' file

If you don't set OFS, then awk by default uses single space as a field separator.

Share:
7,352

Related videos on Youtube

adrotter
Author by

adrotter

Updated on September 18, 2022

Comments

  • adrotter
    adrotter over 1 year

    Here's a similar question but I ran into a problem that was not addressed in the question. How to replace the content of a specific column with awk?

    I want to do something similar, however my file is tab delimited instead of space delimited. I want to change field 3. Lets say my file looks like this:

    NAME\tNUMBER\tID\tSEQ\t...\t...\t... 
    

    Lets say I want to change field 3 to AD

    awk '{$3 = "AD"; print}' infile > outfile
    

    my output is

    NAME\tNUMBER\tAD SEQ ... ... ... ... ... ...
    

    my tabs turn into spaces after the replaced I'm sure I can just tr the output file but I want to know what causes awk to change the field delimiter.

    expected output:

    NAME\tNUMBER\tAD\tSEQ\t...\t...\t...\t