Search code examples
parsingawksedsplitgawk

How to use awk, to split a particular column (using delimiter), then add suffix and then merge?


In the data given below (which is tab separated):

# data
1  xyz   alfa  x=abc;z=cbe;d=fed  xt
2  xyz   alfa  y=cde;z=xy  ft
3  xyb   delta xy=def  zf

I want to add a suffix _LT in the elements (values of the variables) of 4th column after splitting at ;.

Output like:

1  xyz   alfa  x=abc_LT;z=cbe_LT;d=fed_LT  xt
2  xyz   alfa  y=cde_LT;z=xy_LT  ft

I am able to add suffix at specific columns, but can't split(at delim)-add-merge.

awk -v  PRE='_LT' '{$4=$4PRE; print}' OFS="\t" data.txt > data_LT.txt

Solution

  • you can use split function, loop and merge... or use substitutions

    $ awk -v PRE='_LT' '{gsub(/;/,PRE";",$4); sub(/$/,PRE,$4); print}' OFS='\t' data.txt
    1   xyz alfa    x=abc_LT;z=cbe_LT;d=fed_LT  xt
    2   xyz alfa    y=cde_LT;z=xy_LT    ft
    3   xyb delta   xy=def_LT   zf
    
    • gsub(/;/,PRE";",$4) replace all ; with _LT; only for 4th column
    • sub(/$/,PRE,$4) append _LT to 4th column