Help changing date output with AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help changing date output with AWK
# 1  
Old 03-25-2010
Help changing date output with AWK

Hello all,
I have an issue where I'm trying to change a date in a csv text file. The file contains lines that have several fields. For example

Code:
"John", "Smith","some_address","some_city","555-555-5555","11/11/1972"
"Joan","User","some_address","some_city","444-444-4444","12/02/1963"

The date is separated by the "/" and is the date is the last field in the record, Field 12. What I would like to do is run AWK against the file and change the date from
dd/mm/yyyy to YYYYMMDD and have it stay in the same field on all records, so the output will look like this
Code:
"John","Smith","some_address","some_city","555-555-5555","YYYYMMDD"
"Joan","User","some_address","some_city","444-444-4444","YYYYMMDD"

I tried:
Code:
$ awk -F "/" '{print $3, $2, $1}'

but that returns the records in the format
Code:
YYYYMM,"John","Smith"......

Any one out there know how to solve this? Thanks in advance

Any ideas on how to accomplish this?

Last edited by Franklin52; 03-26-2010 at 04:14 AM.. Reason: Please use code tags!
# 2  
Old 03-25-2010
Code:
sed 's/"\(..\)\/\(..\)\/\(....\)"/"\3\2\1"/' file

# 3  
Old 03-25-2010
its long solution but jsut gives an idea how to do it with AWK:

Code:
awk -F\, '{split($NF,a,"[/\"]");{$NF="\""a[4]a[3]a[2]"\""}}1' OFS=\, FILE

# 4  
Old 03-25-2010
Thanks, but that didn't seem to do it. The date remained the same
# 5  
Old 03-25-2010
Quote:
Originally Posted by commobox
Thanks, but that didn't seem to do it. The date remained the same
If you are using Solaris then use "nawk or /usr/xpg4/bin/awk" instead of awk:

Code:
nawk -F\, '{split($NF,a,"[/\"]");{$NF="\""a[4]a[3]a[2]"\""}}1' OFS=\, FILE

and SED will only change standard output that is screen so u should redirect the SED solution to another file:

Code:
sed 's/"\(..\)\/\(..\)\/\(....\)"/"\3\2\1"/' file > myfile.csv

# 6  
Old 03-25-2010
Code:
awk -F\" '{split($(NF-1),a,"\/");sub($(NF-1),a[3]a[2]a[1])}1'  infile

# 7  
Old 03-25-2010
Quote:
Originally Posted by danmero
Code:
awk -F\" '{split($(NF-1),a,"\/");sub($(NF-1),a[3]a[2]a[1])}1'  infile

Why your code doesn't need OFS=\" ?

Code:
awk -F\" '{split($(NF-1),a,"/");$(NF-1)=a[3]a[2]a[1]}1' OFS=\" urfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Changing CSV files with date . Subtracting date by values

Hi All, I have a CSV file which is as below. Basically I need to take the year column in it and find if the year is >= 20152 . If that is then I should subtract all values by 6. In the below example in description I am having number mentioned as YYWW so I need to subtract those by -5. Whereever... (8 Replies)
Discussion started by: arunkumar_mca
8 Replies

2. Shell Programming and Scripting

awk changing numbering in output file

The below awk is supposed filter $8 of example.txt using the each line in gene.txt. I think it is but why is it renumbering the 1,2,3 in $1 to 28,29,394? I have attached the data as it is large, example.txt is the file to be searched, gene.txt has the lines to match, and filtered.txt is the current... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

awk - append date to output of a command

Hi all; I am running a script:/var/tmp/gcsw -ne | grep "State:2" | wc that gives me output like:80 480 6529 but i need this output as:2013-01-18 13:00 -> 80 480 6529 (1 Reply)
Discussion started by: gc_sw
1 Replies

4. Shell Programming and Scripting

changing date to resemble "messages file" date

the following was taken from a perl script: my $date = strftime "%B %d %H:%M:%S", localtime; how can i modify it so this date outputs the date in the form of the date of the messages file. for example: Sep 20 11:48:44 As it is right now, the perl script outputs the date like this: ... (1 Reply)
Discussion started by: SkySmart
1 Replies

5. UNIX for Advanced & Expert Users

Date Conversion on output string from awk

Hi, I would like to convert the output from awk function to date and print on the screen. Example : echo "Start Date: May 24 2010" | gawk -F": " '{print $2}' Output : May 04 2010 I want this to be converted to 2010/05/24 Can i use date function here and how? Thanks, Deepika (2 Replies)
Discussion started by: deepikad
2 Replies

6. Shell Programming and Scripting

Getting a specific date from cal output with AWK

Hi guys! I'll make this short... Is there any good way to get the day number that first matches the Monday column from the cal command output with awk (or any other text manipulator commands) ? I'm sorry if my question wasn't clear at all. For example... One cal output would be $... (6 Replies)
Discussion started by: Casey
6 Replies

7. UNIX for Dummies Questions & Answers

send output of a file as input for changing date

Hi, Please help me out on this one. I want to send the output of a file as input for changing the date using date command. Example, i have a file date.txt whose contents are 081014462009 I need to use the date in that file as input for date command. I tried cat date.txt | date ; but it... (2 Replies)
Discussion started by: foxtron
2 Replies

8. Linux

changing date

Hi, I want to create one user who has right to change the date in linux, (1 Reply)
Discussion started by: manoj.solaris
1 Replies

9. UNIX for Dummies Questions & Answers

Changing Creation Date to a Prespecified Date of a File In Unix

Dear Expert, Is there a command to do that in Unix? In such a way that we don't need to actually "write" or modified the content. -- monkfan (4 Replies)
Discussion started by: monkfan
4 Replies

10. UNIX for Dummies Questions & Answers

changing the date

I need to change the date of the UNIX server at work to 20 aug 2001. How do I do this? And will this affect any databases running, or do I need to bring all databases down? (2 Replies)
Discussion started by: JBX
2 Replies
Login or Register to Ask a Question