My input file is as follows:
12/13/2011,07:14:13.724,12/13/2011 07:14:13.724,231.56.3.245,LasVegas,US
I wish to get the following:
<
This might work for you:
sed 's/\....//;s/\....//' input_file.csv >output_file.csv
sed 's/\(:[0-9][0-9]\)\.[0-9]\{3\}/\1/g' input_file.csv > output.csv
You were almost there. In classic sed
, you have to use backslashes in front of parentheses and braces to make them into metacharacters. Some versions of sed
may have a mechanism to invert operations, so that the braces and parentheses are metacharacters by default, but that's not reliable across platforms.
Also (strong recommendation): use single quotes around the sed command. Otherwise, the shell gets a crack at interpreting those backslashes (and any $
signs, etc) before sed
sees it. Usually, this confuses the coder (and especially the maintaining coder). In fact, use single quotes around arguments to programs whenever you can. Don't get paranoid about it - if you need to interpolate a variable, do so. But single-quoting is generally easier to code, and ultimately easier to understand.
I chose to work on just one time unit; you were working on three. Ultimately, given systematically formed input data, there is no difference in the result - but there is a (small) difference in the readability of the script.
Try:
sed 's,\(:[0-9]\{2\}\).[0-9]\{3\},\1,g'
Also, try \d
instead of [0-9]
, your version of sed may support that.
You were near but some characters are special in sed (in my version, at least): {
, }
, (
, )
, but not :
. So you need to escape them with a back-slash.
And \1
takes expression between paretheses, it should be the first part until seconds, not the second one.
A modification of your version could be:
sed "s/\([0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}\)\.[0-9]\{1,3\}/\1/g" input_file.csv > output.csv
Since the sed
solution has already been posted, here is an alternate awk
solution:
[jaypal:~/Temp] cat inputfile
12/13/2011,07:14:13.724,12/13/2011 07:14:13.724,231.56.3.245,LasVegas,US
[jaypal:~/Temp] awk -F"," -v ORS="," '
{for(i=1;i<NF;i+=1)
if (i==2||i==3) {sub(/\..*/,"",$i);print $i}
else print $i;printf $NF"\n"}' inputfile
12/13/2011,07:14:13,12/13/2011 07:14:13,231.56.3.245,LasVegas,US
Explanation:
,
and Output Record Separator to ,
. for loop
we will loop over each fields.if loop
we would do substitution
to the fields when the for loop
parses over second and third fields. for loop
for <NF
we just print out $NF
which is the last field. This won't cause a ,
to be printed after last field.