Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove quotes in awk command

Tags:

regex

awk

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" 
like image 873
shantanuo Avatar asked Oct 20 '13 07:10

shantanuo


People also ask

How do you remove a quote in Linux?

A single line sed command can remove quotes from start and end of the string. The above sed command execute two expressions against the variable value. The first expression 's/^"//' will remove the starting quote from the string. Second expression 's/"$//' will remove the ending quote from the string.

How do you escape double quotes in awk?

One use of an escape sequence is to include a double-quote character in a string constant. Because a plain double quote ends the string, you must use ' \" ' to represent an actual double-quote character as a part of the string. For example: $ awk 'BEGIN { print "He said \"hi!\

How do I remove a quote from a string in bash?

A simple and elegant answer from Stripping single and double quotes in a string using bash / standard Linux commands only: BAR=$(eval echo $BAR) strips quotes from BAR . If you don't want anything printed out, you can pipe the evals to /dev/null 2>&1 .


1 Answers

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" 
like image 92
anubhava Avatar answered Oct 18 '22 04:10

anubhava