Visit Our UNIX and Linux User Community


Strip time from CSV File?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Strip time from CSV File?
# 1  
Old 08-05-2009
Error Strip time from CSV File?

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:

Code:
"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:

Code:
"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.
# 2  
Old 08-05-2009
Possibilities, with sed:

Code:
sed 's/\(.*\)-.*/\1"/' file

with awk:

Code:
awk -F, '{sub("-.*","\"",$NF)}1' OFS="," file

Regards
# 3  
Old 08-05-2009
@Franklin52: how does the regex in the sed command skip dashes in the name? and what does the \1" do? Thanks in advance
# 4  
Old 08-05-2009
Quote:
Originally Posted by figaro
@Franklin52: how does the regex in the sed command skip dashes in the name?
sed has a greedy pattern match, so it should match the last dash.

Quote:
Originally Posted by figaro
and what does the \1" do? Thanks in advance
With sed you can save substrings with \(.*\) and recall them back with \1, \2, \3 etc. with:

Code:
/\(.*\)-.*/

we saved a substring \(.*\) before the last dash, and with:

Code:
\1"/

we print a double quote " after the substring \1.

Hope this helps.
# 5  
Old 08-05-2009
Thank you so much Franklin!
This is just what i was looking for !

Thanks!!!!
# 6  
Old 08-05-2009
Thanks for the explanation.

Previous Thread | Next Thread
Test Your Knowledge in Computers #668
Difficulty: Medium
IEEE 802.11 is part of the IEEE 802 set of LAN protocols, and specifies the set of media access control (MAC) and physical layer (PHY) protocols for implementing virtual local area network (VLAN) Wi-Fi computer communication in various frequencies.
True or False?

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add current time stamp column in existing csv file

Hi , I want to add a new column 'current_time stamp' in my existing csv file with current time stamp for all the records.I tried something this but this is printing 0 with date & time and printed date one line above header.Please help awk -F "," 'BEGIN{ OFS="," } {$6=system("date... (5 Replies)
Discussion started by: netdbaind
5 Replies

2. Shell Programming and Scripting

Get first and last time entries for a given date in CSV file

CSV date sorted file has multiple entries for most dates. Need to eliminate all but the first and last entries of each date, being the start and end times for that day. 2009-11-20,23:57:46 2009-11-20,23:58:46 2009-11-20,23:59:46 2009-11-21,00:00:45 2009-11-21,00:01:46 2009-11-21,00:02:45... (2 Replies)
Discussion started by: mdennis6
2 Replies

3. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

4. Shell Programming and Scripting

awk work with time change in csv file

Hi, i have csv input file looks like below 3rd field is date and time field i want to change it with user supplied date and time says year=2011 month=09 day=05 hour=11 count=2 when count is say 10 then first ten records should pick and it should increment the... (2 Replies)
Discussion started by: raghavendra.nsn
2 Replies

5. Shell Programming and Scripting

strip csv file

Hi everyone, I hope someone can help me: i am trying to get some info from a csv file, after i awk the column i need , i made a selection and output it in a file. now i need to get a list from this file, but i stuck with some fields. basically i have a text file with next data: 3... (3 Replies)
Discussion started by: lostym
3 Replies

6. UNIX for Dummies Questions & Answers

Strip time from date in a file

I have a couple of datetime fields in a file with contents like below: ICPBR|6373056085|1|O||||JOHN|SMITH|||H200|706|445668|||123 SMITH ST|LAGRANGE|IL|66666 |||||||N|N|N|N|N|||345676|2009.02.20-13:09:04|257655957|2009.02.20-13:09:04||||N|||||F||||||||||| I want to strip the time off the... (4 Replies)
Discussion started by: ChicagoBlues
4 Replies

7. UNIX for Advanced & Expert Users

Time validation in a csv file

Hi, Im having a hard time in creating a script with the following conditions below. I have a csv file generated which is updated every 5 mins and it contains a timestamp in it. for example: time data 00:00 1 00:05 0 00:10 6 00:15 3 however, there is a time that... (4 Replies)
Discussion started by: mdap
4 Replies

8. UNIX for Dummies Questions & Answers

How to strip the contants from a file

Hi, I have some EDI data which 830, 862 and 997. Here is the sample data: ISA~00~ ~00~ ~ZZ~F159B ~ZZ~U1CAD ~051215~184 3~U~00200~000011432~0~P~< GS~FA~TC11A~U1CAD~051215~1843~000011432~X~002002 ST~997~0001 AK1~SH~1168 AK2~856~11680001 AK5~A... (2 Replies)
Discussion started by: isingh786
2 Replies

Featured Tech Videos