Matching filenames and modifying them internally


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matching filenames and modifying them internally
# 1  
Old 10-27-2012
Matching filenames and modifying them internally

hi

i am trying to match filenames and modify the files internally depending on the match
the filenames are something like this
cm01FEB2012bhav
cm01AUG2012bhav
...

Internally the file(cm01FEB2012bhav) looks like this

20MICRONS,EQ,64.1,65.7,62.45,63.7,64.5,64.1,113043,01-FEB-2012
3IINFOTECH,EQ,16,16.3,15.5,16.1,16.1,15.8,2797315,01-FEB-2012
3MINDIA,EQ,3560,3599,3420,3451.8,3450,3597.75,1615,01-FEB-2012
.......

Here i am trying to replace 01-FEB-2012 with 01/02/2012(the filename is cm01FEB2012bhav).As you can see the filename and this field are almost the same.

I have written the code for matching the file cm01FEB2012bhav. I intend to write such if statements for all 12 possibilities(JAN,FEB,MAR...).However the first if statement is only not working Smilie.
Any help in solving this problem is appreciatedSmilie
Code:
#!bin/bash
for file in *.csv;do
if [ "$file" = "cm[0-9][0-9]FEB[0-9][0-9][0-9][0-9]bhav\.csv" ]
then
sed 's^\(..\)\-\(...\)\-\(....\)^\1\/02\/\3^' $file
fi
done

# 2  
Old 10-27-2012
Code:
#!/bin/bash

for file in *.csv
do
        DD=`echo ${file} | cut -c 3-4`
        MM=`echo ${file} | cut -c 5-7`
        YY=`echo ${file} | cut -c 8-11`
        SR_STR=`echo $DD-$MM-$YY`
        RP_STR=`date --date="$DD-$MM-$YY" +"%d/%m/%Y"`
        sed 's:'$SR_STR':'$RP_STR':g' ${file}
done

This User Gave Thanks to Yoda For This Post:
# 3  
Old 10-27-2012
The code is working fine. I want to redirect the output to a file. how do I do that?
# 4  
Old 10-27-2012
You can redirect the output by performing the following modification:-

Code:
sed 's:'$SR_STR':'$RP_STR':g' ${file} > output_file

But if you want to replace the content in the same file, then you can redirect the output to a tmp file and then rename it back to original file.

Code:
sed 's:'$SR_STR':'$RP_STR':g' ${file} > tmp; mv tmp ${file}


Last edited by Yoda; 10-27-2012 at 02:53 PM..
This User Gave Thanks to Yoda For This Post:
# 5  
Old 10-27-2012
Thanks a lot! Smilie Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to combine all matching dates and remove non-matching

Using the awk below I am able to combine all the matching dates in $1, but I can not seem to remove the non-matching from the file. Thank you :). file 20161109104500.0+0000,x,5631 20161109104500.0+0000,y,2 20161109104500.0+0000,z,2 20161109104500.0+0000,a,4117... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

3. Shell Programming and Scripting

Insert lines above matching line with content from matching

Hi, I have text file: Name: xyz Gender: M Address: "120_B_C; ksilskdj; lsudlfw" Zip: 20392 Name: KLM Gender: F Address: "65_D_F; wnmlsi;lsuod;,...." Zip:90233I want to insert 2 new lines before the 'Address: ' line deriving value from this Address line value The Address value in quotes... (1 Reply)
Discussion started by: ysrini
1 Replies

4. Shell Programming and Scripting

Print and Append the matching filenames

Hi, I want to check all files in a folder for some specific start string and append all matching filenames with _1, _2 .... for each file found in the directory. But, $file below gives me all details of the files like access permissions, owner and filename etc. I just want all the filenames... (3 Replies)
Discussion started by: chetancrsp18
3 Replies

5. Web Development

Change internally 403 Error to 404 Error

I have the htaccess file within the folder in the domain I want to deny access to all users, execept one IP, after that redirects Error 403 resulting to a 404 error custom html page as shown below. # Restringting access! deny from all allow from 204.127.111.105 # Example of allowed IP... (1 Reply)
Discussion started by: cgkmal
1 Replies

6. UNIX for Dummies Questions & Answers

How do I configure Linux server to be able to send emails internally?

Hi The script: #!/bin/sh #set -x # set admin email so that you can get email ADMIN="myemailaddress" # set alert level ALERT=4 df -HP | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output; do #echo $output usep=$(echo $output | awk '{ print... (5 Replies)
Discussion started by: wbdevilliers
5 Replies

7. Shell Programming and Scripting

Count matching filenames in a folder

Hi all, I have 4 files for example named abc01012009.txt abc02012009.txt abc03012009.txt abc04012009.txt in a folder. I would like to firstly backup the latest file available, in this case, the latest date available, abc04012009.txt to its subfolder named backup, and then rename the... (4 Replies)
Discussion started by: tententen
4 Replies

8. AIX

Mail bounced back externally - internally ok.

I am unable to send mail externally. It gets refused from any mail server. Here's my current config: oslevel -s 5300-07-05-0831 ******** mail -v foo@yahoo.com < /tmp/oracle.out Warning: .cf file is out of date: sendmail AIX5.3/8.13.4 supports version 10, .cf file is version 9... (3 Replies)
Discussion started by: gravy26
3 Replies

9. IP Networking

publishing 192.168 address internally

OK this is what I want to do: On server1 I want to type <ping server2> and be able to get: pinging 192.168.0.12 ...... etc. and by server2, I mean one-word, not server2.example.com I know if I add the addresses to /etc/hosts I can achieve this, but I would like to not have to update all... (2 Replies)
Discussion started by: vertical98
2 Replies

10. UNIX for Dummies Questions & Answers

how does sleep work internally?

hi can someone explain how the sleep() function actually works, like where is it defined, wat files does it refer etc...? (1 Reply)
Discussion started by: meetbhattu
1 Replies
Login or Register to Ask a Question