nmuntz
August 5, 2009, 12:49pm
1
Hi,
I've been trying (and failing miserably) all morning to strip from a CSV file the time from it.
Can somebody point me in the right direction on how to do this using sed or awk?
The file looks like:
"James","07/20/2009-14:40:11"
"Steve","08/06/2006-02:34:37"
"John","11/03/2008-12:12:34"
and i want to strip it to make it look like:
"James","07/20/2009"
"Steve","08/06/2006"
"John","11/03/2008"
using cut -d '-' -f 1 < file is not an option since some names may contain dashes....
Thanks a lot in advance for your help.
Possibilities, with sed:
sed 's/\(.*\)-.*/\1"/' file
with awk:
awk -F, '{sub("-.*","\"",$NF)}1' OFS="," file
Regards
figaro
August 5, 2009, 1:25pm
3
@Franklin52 : how does the regex in the sed command skip dashes in the name? and what does the \1" do? Thanks in advance
sed has a greedy pattern match, so it should match the last dash.
With sed you can save substrings with \(.*\) and recall them back with \1, \2, \3 etc. with:
/\(.*\)-.*/
we saved a substring \(.*\) before the last dash, and with:
\1"/
we print a double quote " after the substring \1.
Hope this helps.
nmuntz
August 5, 2009, 1:51pm
5
Thank you so much Franklin!
This is just what i was looking for !
Thanks!!!!
figaro
August 5, 2009, 2:49pm
6
Thanks for the explanation.