I have file which contain line below like I want to manage through awk
filename:-test.txt
"A","@900",9999,"Test Place","Quayside Sc, Sligo, Tel: 071 9154382","SCRIPT",20150317
I want to manage this is as single string "Quayside Sc,Sligo, Tel: 071 9154382"
It automatically take first string before comma when I perform following command
echo "A","@900",9999,"Test Place","Quayside Sc, Sligo, Tel: 071 9154382","SCRIPT",20150317 | awk -F ',' '{ print $4 "|" $8 }'
Test Place|SCRIPT
Using FPAT
in gnu-awk you can get whole quoted string as single field:
awk 'BEGIN{ FPAT="\"[^\"]*\"|[^,]*" } {print $4 ORS $5}' file
"Test Place"
"Quayside Sc, Sligo, Tel: 071 9154382"
FPAT="\"[^\"]*\"|[^,]*"
uses a regex to break down fields surrounded by quotes or separated by comma.
For demo purpose here is each parsed field:
awk 'BEGIN{ FPAT="\"[^\"]*\"|[^,]*" } {for (i=1; i<=NF; i++) {
printf "$%d: <%s>\n", i, $i}}' file
$1: <"A">
$2: <"@900">
$3: <9999>
$4: <"Test Place">
$5: <"Quayside Sc, Sligo, Tel: 071 9154382">
$6: <"SCRIPT">
$7: <20150317>
Update: If you don't have gnu-awk 4 then you can use this perl
command for same effect:
perl -F',(?=(?:(?:[^\"]*\"){2})*[^\"]*$)' -ane 'print $F[3] . "\n" . $F[4] . "\n"' file
"Test Place"
"Quayside Sc, Sligo, Tel: 071 9154382"