How to remove '-' for date fields alone in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove '-' for date fields alone in a file
# 1  
Old 10-21-2010
Bug How to remove '-' for date fields alone in a file

Hi,
I have a scenario like, I need to replace a hyphens(-) for date field alone in the file.
I have minus(-) sign for other fields it should remain as such, only for date fields hyphens must be removed. Please find the content for the file below:

sample.txt:
-----------
Code:
KSA,B12467428,2010-06-04,2010-06-04,2011-06-04,ITI, ,N,2011-06-04,AT,IFGC,13097573, ,001,OUT,1, 0000000000627.00,   ,   ,   , 0000000000000.00,     ,,, -0000000000627.00, 0000000000000.00, 0000000000532.95, 0000000000094.05, 00000.1500, 0000000000000.00
KSA,M03201053,2009-06-11,2009-06-11,2010-06-11,IMP,P,R,2010-06-11,ME,IFGC,06490230, ,013,XLL,1,-0000000000657.00,   ,   ,   , 0000000000000.00,     ,,,-0000000000657.00, 0000000000000.00,-0000000000545.31,-0000000000111.69, 00000.0000, 0000000000000.00

Expect results from the sample.txt: (only for date values hyphens(-) are removed)
---------------------------------------------------------------
Code:
KSA,B12467428,20100604,20100604,20110604,ITI, ,N,20110604,AT,IFGC,13097573, ,001,OUT,1, 0000000000627.00,   ,   ,   , 0000000000000.00,     ,,, -0000000000627.00, 0000000000000.00, 0000000000532.95, 0000000000094.05, 00000.1500, 0000000000000.00
KSA,M03201053,20090611,20090611,20100611,IMP,P,R,20100611,ME,IFGC,06490230, ,013,XLL,1,-0000000000657.00,   ,   ,   , 0000000000000.00,     ,,,-0000000000657.00, 0000000000000.00,-0000000000545.31,-0000000000111.69, 00000.0000, 0000000000000.00

Please help me to achieve this using unix command, because as i am new bee to scripting. Smilie
Thanks very much in advance.
KK

Last edited by Franklin52; 10-21-2010 at 08:15 AM.. Reason: Please use code tags
# 2  
Old 10-21-2010
Code:
$ ruby -F"," -ane '2.upto(4){|x|$F[x].gsub!("-","");$F[7].gsub!("-","")}; print $F.join(",")' file

# 3  
Old 10-21-2010
Try...
Code:
sed 's/\(....\)-\(..\)-\(..\)/\1\2\3/g' file1 > file2

# 4  
Old 10-21-2010
Code:
$
$
$ # show the content of the input file "f1"
$ cat -n f1
     1  KSA,B12467428,2010-06-04,2010-06-04,2011-06-04,ITI, ,N,2011-06-04,AT,IFGC,13097573, ,001,OUT,1, 0000000000627.00, , , , 0000000000000.00, ,,, -0000000000627.00, 0000000000000.00, 0000000000532.95, 0000000000094.05, 00000.1500, 0000000000000.00
     2  KSA,M03201053,2009-06-11,2009-06-11,2010-06-11,IMP,P,R,2010-06-11,ME,IFGC,06490230, ,013,XLL,1,-0000000000657.00, , , , 0000000000000.00, ,,,-0000000000657.00, 0000000000000.00,-0000000000545.31,-0000000000111.69, 00000.0000, 0000000000000.00
     3  2009-09-09,KSA,M03201053,2009-06-11,2010-06-11,IMP,P,R,2010-06-11,ME,IFGC,06490230, ,013,XLL,1,-0000000000657.00, , , , 0000000000000.00, ,,,-0000000000657.00, 0000000000000.00,-0000000000545.31,-0000000000111.69, 00000.0000, 0000000000000.00
     4  KSA,M03201053,2009-06-11,2009-06-11,2010-06-11,IMP,P,R,2010-06-11,ME,IFGC,06490230, ,013,XLL,1,-0000000000657.00, , , , 0000000000000.00, ,,,-0000000000657.00, 0000000000000.00,-0000000000545.31,-0000000000111.69, 00000.0000,2010-12-31
$
$
$ # run the Perl one-liner to transform the dates
$ perl -lne 's/(^|,)(\d{4})-(\d\d)-(\d\d)/$1$2$3$4/g; print' f1
KSA,B12467428,20100604,20100604,20110604,ITI, ,N,20110604,AT,IFGC,13097573, ,001,OUT,1, 0000000000627.00, , , , 0000000000000.00, ,,, -0000000000627.00, 0000000000000.00, 0000000000532.95, 0000000000094.05, 00000.1500, 0000000000000.00
KSA,M03201053,20090611,20090611,20100611,IMP,P,R,20100611,ME,IFGC,06490230, ,013,XLL,1,-0000000000657.00, , , , 0000000000000.00, ,,,-0000000000657.00, 0000000000000.00,-0000000000545.31,-0000000000111.69, 00000.0000, 0000000000000.00
20090909,KSA,M03201053,20090611,20100611,IMP,P,R,20100611,ME,IFGC,06490230, ,013,XLL,1,-0000000000657.00, , , , 0000000000000.00, ,,,-0000000000657.00, 0000000000000.00,-0000000000545.31,-0000000000111.69, 00000.0000, 0000000000000.00
KSA,M03201053,20090611,20090611,20100611,IMP,P,R,20100611,ME,IFGC,06490230, ,013,XLL,1,-0000000000657.00, , , , 0000000000000.00, ,,,-0000000000657.00, 0000000000000.00,-0000000000545.31,-0000000000111.69, 00000.0000,20101231
$
$

tyler_durden

Last edited by durden_tyler; 10-21-2010 at 09:43 AM.. Reason: fixed the regex to take care of boundary conditions
# 5  
Old 10-21-2010
Code:
sed 's/\([0-9]\)-/\1/g' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove duplicate lines from file based on fields

Dear community, I have to remove duplicate lines from a file contains a very big ammount of rows (milions?) based on 1st and 3rd columns The data are like this: Region 23/11/2014 09:11:36 41752 Medio 23/11/2014 03:11:38 4132 Info 23/11/2014 05:11:09 4323... (2 Replies)
Discussion started by: Lord Spectre
2 Replies

2. Shell Programming and Scripting

Please help, need to create script to remove lines by date in file

Please Help (novice to PERL and SHELL scripting)…. Need to create a script which removes all lines in $filename = "cycle_calendar_ftp_out" older than current date – a variable which will be a number of days passed to script. For Ex it will look at the end date which is the last field (4) and... (2 Replies)
Discussion started by: m3pwr
2 Replies

3. Shell Programming and Scripting

How do I take out(remove) the date part in the file name in a script?

Hi All, I need to create links between two directories. have multiple files in a specified location. Source Location ex: /opt/xdm/input/ Target Location ex: /opt/xdm input file names: 1. abc_app.aus.apac.yyyymmdd.dtd 2. abcd_app.aus.apac.yyyymmdd.dtd I need to build a code that reads... (1 Reply)
Discussion started by: kthri_82
1 Replies

4. Shell Programming and Scripting

How to remove the date part of the file?

Hi Gurus, I have file name like: abcd.20131005.dat I need to remove mid part of data final name should be abcd.dat thanks in advance (2 Replies)
Discussion started by: ken6503
2 Replies

5. Shell Programming and Scripting

Remove duplicates based on query and subject fields from blast output file

Hi all I have a blast outfile file like this : NZ_1540841_1561981 ICMP_1687819_1695946 92.59 27 2 0 12826 12852 3136 3162 0.28 38.2 NZ_1540841_1561981 ICMP_1687819_1695946 95.65 23 1 0 12268 12290 5815 5837 0.28 38.2 NZ_1540841_1561981 ICMP_3674888_3676546 82.70 185 32 0 9454 9638 11 195 6e-24 ... (2 Replies)
Discussion started by: pbioinfo
2 Replies

6. UNIX for Dummies Questions & Answers

Remove the date portion from file name

hi, I am trying to remove the last field before the period (.) from a list of file names in a directory in a shell script. Below is the list of file names. ENVID_archival_20120214092258.log ENVID_Get_Source_Files_20120214091828.log ENVID_Get_Source_Files_20120214092523.log... (6 Replies)
Discussion started by: Vijay81
6 Replies

7. UNIX for Dummies Questions & Answers

Remove whitespaces between comma separated fields from file

Hello all, I am a unix dummy. I am trying to remove spaces between fields. I have the file in the following format 12332432, 2345 , asdfsdf ,100216 , 9999999 12332431, 2341 , asdfsd2 ,100213 , 9999999 &... (2 Replies)
Discussion started by: nitinbjoshi
2 Replies

8. UNIX for Dummies Questions & Answers

Remove the date from the file

Hi I am getting the file which is having the format as follows: set-I abcxyz20080109 abc20080110 Set-II abc-20070109 abcxyz-20070110 I need to get only the file name without date as Set-I abcxyz abc Set-II abc (5 Replies)
Discussion started by: pradkumar
5 Replies

9. Shell Programming and Scripting

How can i remove spaces in between the fields in a file

Hey , I have a file and it's having spaces for some of the fields in it. Like the one below. I want to remove the spaces in them through out the file. The spaces occur randomly and i can't say which field is having space. So please help. Here is sample file with spaces after 5th field. (3 Replies)
Discussion started by: dsravan
3 Replies

10. HP-UX

How do I take out(remove) the date part in the file name?

Hi Guys here I am again, I have two files in a specified location. Location ex: /opt/xdm/input/ input file names: 1. abc_app.yyyymmdd.dtd 2. abd_app.yyyymmdd.dtd I need to build a code that reads the files from the location based on the oldest date and cuts the date part... (5 Replies)
Discussion started by: ruthless
5 Replies
Login or Register to Ask a Question