38

I have a text file that needs to be processed using awk.

"5","1211274723","0","D","2"
"1","1211292921","0","A","2"
"5","1211295793","0","A","2"
"5","1211310146","0","A","2"
"5","1211310310","0","A","2"
"4","1211315271","0","A","2"
"5","1211318203","0","D","2"
"2","1211323658","0","A","2"
"5","1211329224","0","A","2"
"5","1211330064","0","A","2"


# cat testme.csv | awk -F',' '{print "set", $2, $3}'
set "1211274723" "0"
set "1211292921" "0"
set "1211295793" "0"
set "1211310146" "0"
set "1211310310" "0"
set "1211315271" "0"
set "1211318203" "0"
set "1211323658" "0"
set "1211329224" "0"
set "1211330064" "0"

The only problem is that I do not know how to remove the quotes around phone numbers. So that my final output will look something like this...

set 1211274723 "0"
set 1211292921 "0"
set 1211295793 "0"
set 1211310146 "0"
set 1211310310 "0"
set 1211315271 "0"
set 1211318203 "0"
set 1211323658 "0"
set 1211329224 "0"
set 1211330064 "0"
anubhava
  • 761,203
  • 64
  • 569
  • 643
shantanuo
  • 31,689
  • 78
  • 245
  • 403

2 Answers2

74

You can use gsub function:

awk -F',' '{gsub(/"/, "", $2); print "set", $2, $3}' testme.csv 
set 1211274723 "0"
set 1211292921 "0"
set 1211295793 "0"
set 1211310146 "0"
set 1211310310 "0"
set 1211315271 "0"
set 1211318203 "0"
set 1211323658 "0"
set 1211329224 "0"
set 1211330064 "0"
anubhava
  • 761,203
  • 64
  • 569
  • 643
26

Using double quotes as the field separator;

 awk -F'"' '{print "set", $4, "\""$6"\""}' testme.csv
perreal
  • 94,503
  • 21
  • 155
  • 181
  • An answer in the pure KISS principle for a "can't see the forest for the trees" situation. You sir get a point just for this :). – Lohmar ASHAR Jan 20 '21 at 20:45