Replace the nth column date as MM/DD/YYYY


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace the nth column date as MM/DD/YYYY
# 1  
Old 08-13-2012
Replace the nth column date as MM/DD/YYYY

Hi,
I need some unix command to replace the following thing.
Code:
cat test.dat
1234|test|8/19/2009|8/20/2009|test
1234|test|8/9/2009|8/21/2009|test
1234|test|8/1/2009|8/2/2009|test

after processing
Code:
1234|test|08/19/2009|08/20/2009|test
1234|test|08/09/2009|08/21/2009|test
1234|test|08/01/2009|08/02/2009|test


Last edited by Scott; 08-13-2012 at 05:04 AM.. Reason: Use code tags, please...
# 2  
Old 08-13-2012
Code:
awk -F"|" '
{	for(i=3;i<=4;i++)
		{
			split($i,a,"/");
			if(a[1]<10){a[1]="0"substr(a[1],1)};
			if(a[2]<10){a[2]="0"substr(a[2],1)};
			$i=a[1]"/"a[2]"/"a[3]};
			print
}' OFS="|" filename

# 3  
Old 08-13-2012
if the file is in the below format
Code:
1234|test|8/19/2009||test
1234|test|8/9/09|8/21/2009|test
1234|test|8/1/86||test
1234|test||8/1/09|test

output..
Code:
1234|test|08/19/2009||test
1234|test|08/09/2009|08/21/2009|test
1234|test|08/01/1986||test
1234|test||08/01/2009|test

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 08-13-2012 at 05:26 AM.. Reason: Please use code tags for data and code samples
# 4  
Old 08-13-2012
And what decides the century for years? That is, how is 86 1986 and not 2086 or 1886, etc.?
# 5  
Old 08-13-2012
u can ignore that part.. to be precise
Code:
1234|test|8/19/2009||test
1234|test|8/9/2009|8/21/2009|test
1234|test|8/1/2009||test

output..
Code:
1234|test|08/19/2009||test
1234|test|08/09/2009|08/21/2009|test
1234|test|08/01/2009||test


Last edited by Franklin52; 08-13-2012 at 06:07 AM.. Reason: fixed code tags
# 6  
Old 08-13-2012
Code:
nawk '{
for(i=3;i<=4;i++)
 if((n=split($i,a,"/"))==3)
  $i=sprintf("%02d/%02d/%04d",a[1],a[2],a[3])
}1' FS=\| OFS=\| infile

This User Gave Thanks to elixir_sinari For This Post:
# 7  
Old 08-13-2012
Code:
awk -F"|" '
{	for(i=3;i<=4;i++)
		{
			n=split($i,a,"/");
			if(n==3) 
				{
					if(a[1]<10){a[1]="0"substr(a[1],1)};
					if(a[2]<10){a[2]="0"substr(a[2],1)};
					$i=a[1]"/"a[2]"/"a[3]
				}
		};
			print
}' OFS="|" filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)

I have a csv which has lot of columns . I was looking for an awk script which would extract a column twice. for the first occurance the header and data needs to be intact but for the second occurance i want to replace the header name since it a duplicate and extract year value which is in ddmmyy... (10 Replies)
Discussion started by: Kunalcurious
10 Replies

2. Shell Programming and Scripting

Replace Value of nth Column of Each Line Using Array

Hello All, I am writing a shell script with following requirement: 1. I have one input file as below CHE01,A,MSC,INO CHE02,B,NST,INC CHE03,C,STM,INP 2. In shell script I have predefined array as below: Array1={A, B, C} Array2= {U09, C04, A054} (6 Replies)
Discussion started by: angshuman
6 Replies

3. Shell Programming and Scripting

Search term in nth field and replace kth column

Hi, I have a text file which looks like this a.txt A,12,Apple,Red B,33,Banana,Yellow C,66,Sky,Blue I need to search for a particular field(s) in particular column(s) and for that matching line need to replace the nth column. Sample scenario 1: Search for 66 in second field and Sky in... (5 Replies)
Discussion started by: wahi80
5 Replies

4. Shell Programming and Scripting

awk search and replace nth column by using a variable.

I am passing a variable and replace nth value with the variable. I tried using many options in awk command but unable to ignore the special characters in the output and also unable to pass the actual value. Input : "1","2","3" Output : "1","1000","3" TempVal=`echo 1000` Cat... (2 Replies)
Discussion started by: onesuri
2 Replies

5. Shell Programming and Scripting

How to search and replace string from nth column from a file?

I wanted to search for a string and replace it with other string from nth column of a file which is comma seperated which I am able to do with below # For Comma seperated file without quotes awk 'BEGIN{OFS=FS=","}$"'"$ColumnNo"'"=="'"$PPK"'"{$"'"$ColumnNo"'"="'"$NPK"'"}{print}' ${FileName} ... (5 Replies)
Discussion started by: Amit Joshi
5 Replies

6. Shell Programming and Scripting

awk to search for specific line and replace nth column

I need to be able to search for a string in the first column and if that string exists than replace the nth column with "-9.99". AW12000012012 2.38 1.51 3.01 1.66 0.90 0.91 1.22 0.82 0.57 1.67 2.31 3.63 0.00 AW12000012013 1.52 0.90 1.20 1.34 1.21 0.67 ... (14 Replies)
Discussion started by: ncwxpanther
14 Replies

7. Shell Programming and Scripting

Convert date column as yyyy/mm/dd format

Hi All, I have file like “April 10, 2013”,”raj” “April 29, 2013”,”raj1” Output : “2013/04/10”,”raj” “2013/04/29”,”raj1” Please help me how to do... (9 Replies)
Discussion started by: bmk
9 Replies

8. Shell Programming and Scripting

change date format from yyyy/mm/dd to dd/mm/yyyy

(Attention: Green PHP newbie !) I have an online inquiry form, delivering a date in the form yyyy/mm/dd to my feedback form. If the content passes several checks, the form sends an e-mail to me. All works fine. I just would like to receive the date in the form dd/mm/yyyy. I tried with some code,... (6 Replies)
Discussion started by: keyboarder
6 Replies

9. Shell Programming and Scripting

converting the date field from dd/mm/yyyy to yyyy/mm/dd

How to convert the date field from dd/mm/yyyy to yyyy/mm/dd in unix my script will generate text file which have two fields one is date and another is name of the server for example this is sample date which I have to sort based on older to newer date the problem is when I found out sort will... (4 Replies)
Discussion started by: pareshan
4 Replies

10. Shell Programming and Scripting

Change Date from dd-mmm-yyyy to mm/dd/yyyy

I want to change a date from format dd-mmm-yyyy to mm/dd/yyyy. Is there a way to do this with sed or do you have to write a case statement to convert JAN to 01? Thanks (9 Replies)
Discussion started by: stringzz
9 Replies
Login or Register to Ask a Question