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

awk print specific column with specific output field separator

Became Hot Network Question
edited tags
Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 265
Source Link
Porcupine
  • 2.2k
  • 4
  • 26
  • 60

awk 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