Problem with mutliple sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with mutliple sed
# 1  
Old 02-27-2009
Problem with mutliple sed

Hi I'm working on a file which looks like:

00001| |XXX|ABCD|27-SEP-1983|31-DEC-2099|Name|00001|N| |31|L| | | | |25-AUG-1990

I need to change the date format from DD-MMM-YYYY to MM/DD/YYYY

So i using
cat <filename> | sed 's%-JAN-%/01/%;s%-FEB-%/02/%;s%-MAR-%/03/%;s%-APR-%/04/%;s%-MAY-%/05/%;s%-JUN-%/06/%;s%-JUL-%/07/%;s%-AUG-%/08/%;s%-SEP-%/09/%;s%-OCT-%/10/%;s%-NOV-%/11/%;s%-DEC-%/12/%;' | sed 's%|\([0-9][0-9]\)/\([0-9][0-9]\)/\([0-9][0-9][0-9][0-9]\)%|\2/\1/\3%'

Now the first sed is working perfectly fine. It is replacing all the date formats to DD/MM/YYYY but the problem is in the next sed which is working only on the first date field not the rest.

I'm getting the output as:
00001| |XXX|ABCD|08/27/1983|31/12/2099|Name|00001|N| |31|L| | | | |25/08/1990

Whereas I'm looking for the output as :
00001| |XXX|ABCD|08/27/1983|12/31/2099|Name|00001|N| |31|L| | | | |08/25/1990

Any help would be appreciated.
Thanks

Last edited by Alim; 02-27-2009 at 02:50 AM..
# 2  
Old 02-27-2009
A solution with awk:

Code:
awk '
BEGIN {
  FS=OFS="|"
  a="JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC"
  split(a,m,",")
  for(i=1;i<13;i++)
    n[m[i]]=i
}
{split($5,d,"-");$5=sprintf("%02d/%s/%s",n[d[2]],d[1],d[3])}
{split($6,d,"-");$6=sprintf("%02d/%s/%s",n[d[2]],d[1],d[3])}
{split($17,d,"-");$17=sprintf("%02d/%s/%s",n[d[2]],d[1],d[3])}
{print}' file

Use nawk or /usr/xpg4/bin/awk on Solaris.

Regards
# 3  
Old 02-27-2009
It works. Thanks a lot.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Load mutliple files into Netezza table

Hi All, I get ~1000 files everyday and the current unix script process one file at a time... like it unzips the file, loads into table . This process will repeat how many ever files were present in the directory. I was wondering can we process multiple files (like unzipping and loading X amount... (1 Reply)
Discussion started by: rajasony
1 Replies

2. Shell Programming and Scripting

Print mutliple patterns in a line using sed

Hi, I am trying to print multiple patterns in a line using sed. But it is printing only the last occurance of a pattern. If the line is the the output should be Lookup Procedure|Stored proc But the output I am getting is Stored proc The code I am using is echo... (9 Replies)
Discussion started by: kedar_laveti
9 Replies

3. Shell Programming and Scripting

Awk with mutliple filed separators

I try to trim a FTP log using awk, but the escape sequences does not work the way I want it. Input data Wed 3Oct10 21:48:00 - (002117) Sent file d:\ftp\home\tools\htmake.rar successfully (48.2 kB/sec - 40997 Bytes) Wed 3Oct10 22:25:46 - (002118) Sent file d:\ftp\files\main\oct\camera1... (4 Replies)
Discussion started by: Jotne
4 Replies

4. Shell Programming and Scripting

Executing mutliple commands in Unix

Hi I always do a ftp in Unix to bring in a file from Mainframes. Then run a set of commands say 8 commands one by one to execute the brought in file. Upon completion of this commands, the data from the file gets transfered to Mainframe database. The problem is i can execute only 1 file at a time.... (2 Replies)
Discussion started by: cnelatur
2 Replies

5. Shell Programming and Scripting

How to set mutliple words variable from command line

I'm writing a script (C shell) to search for a pattern in file. For example scriptname pattern file1 file2 filenN I use for loop to loop through arguments argv, and it does the job if all arguments are supplied. However if only one argument is supplied (in that case pattern ) it should ask to... (5 Replies)
Discussion started by: patryk44
5 Replies

6. Shell Programming and Scripting

Need help on Mulitple files mutliple actions

Hi all, I have mistkanely gzipped twice an entire folder and sub folders, and also renamed the files during that process. I am trying to undo this, and I need help to create the batch to work on it. All folders are under my images directory, I have a output.txt file that holds all the... (1 Reply)
Discussion started by: saariko
1 Replies

7. Shell Programming and Scripting

mutliple files in the same directory

I have over 900 files that have the same name except for a unique numeric assignment. For all files I would like to cut the 2nd column and paste all into one new file. All in bash. sample input format for each file: 1 2 3 1 2 3 1 2 3 sample command for what I want to do: cut -d' ' -f2... (8 Replies)
Discussion started by: epi8
8 Replies

8. UNIX for Dummies Questions & Answers

process mutliple files in the same directory

I have over 900 files that have the same name except for a unique numeric assignment. For all files I would like to cut the 2nd column and paste all into one new file. All in bash. sample input format for each file: 1 2 3 1 2 3 1 2 3 sample command for what I want to do: cut -d' ' -f2... (1 Reply)
Discussion started by: epi8
1 Replies

9. UNIX for Dummies Questions & Answers

displaying mutliple fields on command line

This is probably the dumbest question you guys can get, but I'm trying, as a complete noob, to display the unix calendar for all the months without Saturday and Sunday showing. How can I remove those fields without having to type all the fields in individually such as: cal -y | awk '{print $2,... (3 Replies)
Discussion started by: Trellot
3 Replies

10. Shell Programming and Scripting

Help Needed - print mutliple lines

I have the following input -- appl = host = user = / pid = 76 elapsed = 0.000 seconds server_elapsed = 2.999 select emp_no, dept_no from emp where empname like 'Rob%' and empno=10 ; -- appl = host = user = / pid = 76 elapsed = 0.000 seconds server_elapsed = 0.999 select emp_no, dept_no... (5 Replies)
Discussion started by: newlearner
5 Replies
Login or Register to Ask a Question