Skip to main content
3 of 3
edited tags
αғsнιη
  • 41.9k
  • 17
  • 75
  • 117

print specific column with specific output field separator

cat A.tsv
1,a,d
2,b,e
3,c,f
$ awk -F ',' -v OFS="," '{print $2, $3}' A.tsv 
a,d
b,e
c,f
  • I expected the following 4 commands to give the same results as above:
$ awk 'FS=","; OFS=","; {print $2, $3}' A.tsv 
1,a,d
1,a,d
,
2,b,e
2,b,e
b,e
3,c,f
3,c,f
c,f
$ awk -F ',' 'OFS=","; {print $2, $3}' A.tsv 
1,a,d
a,d
2,b,e
b,e
3,c,f
c,f
$ awk -v OFS="," 'FS=","; {print $2, $3}' A.tsv 
1,a,d
,
2,b,e
b,e
3,c,f
c,f
$ awk -F ',' 'FS=OFS; {print $2, $3}' A.tsv 
1,a,d
a d
2,b,e
 
3,c,f

Can someone explain why the last 4 commands have different results than the first?

Refer: Difference between awk -FS and awk -f in shell scripting - Stack Overflow

Porcupine
  • 2.2k
  • 4
  • 26
  • 60