Reputation: 905
i have a file with 3 columns, the 2nd column is lat/long and may or may not have data.
2012-01-10 21:27:52.811,,ABC -- No lat/long
2012-01-10 21:27:52.811,37.8889329,-112.1876328,XYZ -- with lat long
Can some one help me with a sed/awk/perl to transform it to
2012-01-10 21:27:52.811,"37.8889329,-112.1876328",XYZ
In all other cases it should not add any double quotes
Thanks in advance
Upvotes: 0
Views: 2201
Reputation: 246764
Just with bash:
oldIFS=$IFS
IFS=,
while read a b c d; do
if [[ -z "$d" ]]; then
printf "%s,,%s\n" "$a" "$c"
else
printf "%s,\"%s,%s\",%s\n" "$a" "$b" "$c" "$d"
fi
done < filename > filename.new
IFS=$oldIFS
Upvotes: 0
Reputation: 25032
Given the constraints, you can use sed and specify the fields to change:
sed '/,,/! s/,/,"/1; s/,/",/3'
This works by restricting the substitutions only to those lines where two commas ,,
do not occur together. Within the lines where substitutions will be made, we use numeric flags to add a double-quote after the first comma and before the third comma.
Upvotes: 0
Reputation: 58371
This might work for you:
sed '/^[^,]*,,/!s/,\([^,]*,[^,]*\)/,"\1"/' file
2012-01-10 21:27:52.811,,ABC -- No lat/long
2012-01-10 21:27:52.811,"37.8889329,-112.1876328",XYZ -- with lat long
Upvotes: 1
Reputation: 141780
I would probably do something like this, using awk
:
% echo '2012-01-10 21:27:52.811,,ABC -- No lat/long
2012-01-10 21:27:52.811,37.8889329,-112.1876328,XYZ -- with lat long
' | awk -F',' -v OFS=',' '
$2 && $3 {
$2 = "\"" $2
$3 = $3 "\""
}
1
'
2012-01-10 21:27:52.811,,ABC -- No lat/long
2012-01-10 21:27:52.811,"37.8889329,-112.1876328",XYZ -- with lat long
This assumes that the fields are comma-delimited on the way in and the way out.
If the second and third fields are populated then
"
before the second field"
after the third fieldPrint all lines (1
).
There are a lot of assumptions here, so you'll have to dig out the sed & awk book and tweak this to meet your needs.
Upvotes: 3