How to replace second column of csv file with a specific value "XYX"

10,993

Solution 1

In awk the variable NF is the number of fields on the current line. You can refer to each field like $1,$2...$NF where $1 is the first field and $2 is the second field, upto $NF which is the last field on the current line (i.e. if NF is 5 then $NF is $5).

$ cat file
1,2,3,4,5

$ awk '$2="XYX"' FS=, OFS=, file
1,XYX,3,4,5

Solution 2

are you looking for:

awk '{$2="XYX"}1' file

?

if you want FS to be comma:

 awk -F, '{$2="XYX"}1' OFS=, file
Share:
10,993

Related videos on Youtube

user752590
Author by

user752590

Updated on June 04, 2022

Comments

  • user752590
    user752590 almost 2 years

    To replace last field of csv file with xyz I use following command:

    awk -F, '{$NF="xyz";}1' OFS=, file
    

    How can I overwrite the second column of file with value xyz?