Datestamp format 2nd change in csv file (awk or sed)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Datestamp format 2nd change in csv file (awk or sed)
# 1  
Old 09-12-2014
Datestamp format 2nd change in csv file (awk or sed)

I have a csv file formatted like this:

Code:
2014-08-21 18:06:26,A,B,12345,123,C,1232,26/08/14 18:07

and I'm trying to change it to MM/DD/YYYY HH:MM for both occurances.
I have got this:
Code:
awk -F, 'NR <=1 {print;next}{"date +%d/%m/%Y\" \"%H:%m -d\""$1 "\""| getline dte;$1=dte}1' OFS="," test.csv

This changes and formats the first occurance, but how do I convert the second one at the same time?

I'm also hitting this error
Code:
awk: cmd. line:1: (FILENAME=test.csv FNR=258) fatal: cannot create child process for `date +%d/%m/%Y" "%H:%m -d"2014-04-15 14:14:11"' (fork: Resource temporarily unavailable)

Is awk exhausting all my memory?

In fact, whilst that command runs on one flavour of unix, it doesn't run on redhat :-(. The first field is null.


Thanks in advance

Last edited by say170; 09-12-2014 at 11:01 AM.. Reason: pasted wrong code line
# 2  
Old 09-12-2014
What Operating System your machine is running on?
Code:
uname -a

# 3  
Old 09-12-2014
Linux (Redhat)

reports as
Code:
Linux hostname 2.6.9-104.ELsmp #1 SMP Wed May 9 19:42:36 EDT 2012 i686 i686 i386 GNU/Linux

I can use bash, ksh, or sh ...

the fork error was on cygwin, so you can ignore that one...

Last edited by say170; 09-12-2014 at 12:09 PM..
# 4  
Old 09-12-2014
Ok, the ksh93 builtin printf includes a %T formatting option.

For the sample input you posted, try something like:-
Code:
#!/bin/ksh93

while IFS="," read d1 v1 v2 v3 v4 v5 v6 d2
do
        printf "%(%m-%d-%Y %H:%M)T,%s,%(%m-%d-%Y %H:%M)T\n" "$d1" "$v1,$v2,$v3,$v4,$v5,$v6" "${d2:6:2}-${d2:3:2}-${d2:0:2} ${d2:9}"
done < file.csv

# 5  
Old 09-12-2014
Code:
akshay@nio:/tmp$ cat file.csv
2014-08-21 18:06:26,A,B,12345,123,C,1232,26/08/14 18:07

Code:
akshay@nio:/tmp$ cat date_format.awk
function fmt(v,reverse){

	split(v,dt,"[-/ :]")	
	str=dt[1]" "dt[2]" "dt[3]" "dt[4]" "dt[5]" "dt[6]
	
	if(reverse)
	{
		dt[3] += (dt[3] >=0 && dt[3] <=69) ? 2000 : (dt[3]>=70 && dt[3] <= 99) ? 1900 : 0

		str = dt[3]" "dt[2]" "dt[1]" "dt[4]" "dt[5]" " ( length(dt[6])?dt[6]:0 )
	}

	return strftime("%d/%m/%Y %H:%m",mktime(str))
}
{
	$1 = fmt($1) 
	$8 = fmt($8,1)
}1

Resulting
Code:
akshay@nio:/tmp$ awk -vFS="," -f date_format.awk file.csv
21/08/2014 18:08 A B 12345 123 C 1232 26/08/2014 18:08

This User Gave Thanks to Akshay Hegde For This Post:
# 6  
Old 09-12-2014
This one certainly won't win the beauty contest, but it might work for you.
Code:
sed -r "s/^([0-9]{4})-([0-9]{2})-([0-9]{2}) (.....)...(,.*,)([0-9]{2})\/([0-9]{2})/\2-\3-\1 \4\5\7\/\6/"

Smilie Smilie
# 7  
Old 09-12-2014
Quote:
Originally Posted by Yoda
Ok, the ksh93 builtin printf includes a %T formatting option.
I don't have ksh93, only straight ksh which returns

Code:
./csvsplit2.ksh[6]: : bad substitution

---------- Post updated at 11:20 AM ---------- Previous update was at 10:28 AM ----------

Quote:
Originally Posted by Akshay Hegde
Code:
akshay@nio:/tmp$ cat file.csv
2014-08-21 18:06:26,A,B,12345,123,C,1232,26/08/14 18:07

Resulting
Code:
akshay@nio:/tmp$ awk -vFS="," -f date_format.awk file.csv
21/08/2014 18:08 A B 12345 123 C 1232 26/08/2014 18:08

I think something is not quite right as the times are different in the result. Ah.. %m not %M. Thanks... all working now.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change date format in am/pm in csv files using UNIX

Hi All, I'm new to forum good to hear all. I stuck in converting date format in csv file using unix csv file contains as below ,750,0000000000000000GCJR, ,06/22/2016 14:48:44 I want to convert into as below ,750,0000000000000000GCJR, ,06/22/2016 02:48:44 PM Please reply asap..... (22 Replies)
Discussion started by: Raghureds
22 Replies

2. Shell Programming and Scripting

How to parse this file using awk and output in CSV format?

My source file looks like this: Cust-Number = "101" Cust-Name="Joe" Cust-Town="London" Cust-hobby="tennis" Cust-purchase="200" Cust-Number = "102" Cust-Name="Mary" Cust-Town="Newyork" Cust-hobby="reading" Cust-purchase="125" Now I want to parse this file (leaving out hobby) and... (10 Replies)
Discussion started by: Balav
10 Replies

3. Shell Programming and Scripting

Date format change in a csv file

Hi, We have csv file where date is coming in MM/DD/YYYY HH:MM:SS (06/23/2015 20:59:12) in multiple places But we need to change the date format to DD/Mon/YYYY HH:MM:SS (23/Jul/2015 20:59:12) using shell script. Please let us know how can we achieve the same. (16 Replies)
Discussion started by: dholea
16 Replies

4. Shell Programming and Scripting

Need to change date format in a csv file using awk

Example: Input csv file 00245DLS,Sitel Ocala,12/31/2014,18:45,1.00,7.00,0.00,0.00 00245DLS,Sitel Ocala,12/31/2014,19:00,-1.00,-1.00,-1.00,-1.00 00245HB,Charlotte,01/01/2015,00:00,-1.00,-1.00,-1.00,0.00 Output csv file 00245DLS,Sitel Ocala,2014/12/31,18:45,1.00,7.00,0.00,0.00 00245DLS,Sitel... (8 Replies)
Discussion started by: adit
8 Replies

5. Shell Programming and Scripting

Change a file content format using awk

Hi, i have a file input.txt Continent North America Country USA Capital Washington D.C. Country Canada Capital Ottawa Continent South America Country Argentina Capital Buenos Aires Country Brazil Capital Brasília Coutry Colombia Capital Bogotá and i want to get an output.txt ... (3 Replies)
Discussion started by: fastlane3000
3 Replies

6. 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

7. Shell Programming and Scripting

awk/sed/something else for csv file

Hi, I have a filename.csv in which there are 3 colums, ie: Name ; prefixnumber ; number root ; 020 ; 1234567 user1,2,3 ; 070 ; 7654321 What I want is to merge colum 2 and 3 that it becomes 0201234567 or even better +31201234567 so the country number is used and drop the leading 0.... (9 Replies)
Discussion started by: necron
9 Replies

8. Programming

awk script to convert a text file into csv format

hi...... thanks for allowing me to start a discussion i am collecting usb usage details of all users and convert it into csv files so that i can export it into some database.. the input text file is as follows:- USB History Dump by nabiy (c)2008 (1) --- Kingston DataTraveler 130 USB... (2 Replies)
Discussion started by: certteam
2 Replies

9. Shell Programming and Scripting

Using awk/sed in handling csv file.

Please study the below script and the output Script: echo "Minimum ${host} ${process} response time=${min} ms" >> ${OUTDIR}/${OUTFILE}; echo "Maximum ${host} ${process} response time=${max} ms" >> ${OUTDIR}/${OUTFILE}; echo "Average ${host} ${process} response time=${avg} ms" >>... (0 Replies)
Discussion started by: ajincoep
0 Replies

10. Shell Programming and Scripting

AWK CSV to TXT format, TXT file not in a correct column format

HI guys, I have created a script to read 1 column in a csv file and then place it in text file. However, when i checked out the text file, it is not in a column format... Example: CSV file contains name,age aa,11 bb,22 cc,33 After using awk to get first column TXT file... (1 Reply)
Discussion started by: mdap
1 Replies
Login or Register to Ask a Question