loop through files in each folder and perform sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting loop through files in each folder and perform sed
# 1  
Old 11-21-2011
loop through files in each folder and perform sed

Dear all,

I have few log folders in directory (FILE) and i need to perform sed on each files inside each folders. Here is my script, and i wish to rename each file back to the same file name after modification. Can anyone guide me on my script below?

eg:

folder1 contain files ABC.log,ABCD.log
folder 2 contain files EFG.log,EFGH.log

Code:
for toclean in `ls $FILE/*`
do
basetoclean=`basename $toclean .txt`
sed '1,/============/d' $toclean > $toclean.txt
sed -e '1d' -e '2d' -e '/^$/,/Output/d' $toclean.txt > $toclean.log
done

Thanks alot and looking forward for your help!!


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

Last edited by Franklin52; 11-22-2011 at 04:00 AM.. Reason: Code tags
# 2  
Old 11-21-2011
Quote:
Originally Posted by ymeyaw
eg:

folder1 contain files ABC.log,ABCD.log
folder 2 contain files EFG.log,EFGH.log
A few things jump out at me straight off:

Code:
for toclean in `ls $FILE/*`
do
basetoclean=`basename $toclean .txt`   # note 1 below
sed '1,/============/d' $toclean > $toclean.txt   # note 2 below
sed -e '1d' -e '2d' -e '/^$/,/Output/d' $toclean.txt > $toclean.log  #note 3
done

1) I don't understand your need to take the basename of the original file. Not to mention that you dont use the variable. Also, given your examples both indicate that the files have the suffix .log it seems removing .txt seems wrong.

2) If $toclean.txt is really just a temp file, then it is better to create a temporary file in /tmp than to use some form of the original filename. For example:
Code:
sed 's/foo/bar/'  $toclean >/tmp/cleanscript_$$.tmp1

The name makes some previsions to avoid collisions if more than one instance of the script is running concurrently, and all temp files can be easily cleaned up with one rm command at the end.

3) It appears you are trying to overlay the original file, however since the sufix wasn't stripped from the original name you'll end up with something line path/file.log.log

Further, it's not a good idea to overlay the file directly (in my opnion). Disk fill, and other things causes commands to incorrectly generate, or fail to generate, output, so until you are sure that the command was successful, I'd write the second output to another temp file and check the return code. If the return code indicates success, then move the tmp file to the original file.

Finally remove any temp files that you created. Assuming all were created with the example name above:
Code:
rm -f /tmp/cleanscript_$$.*

Hope this helps you move forward.
# 3  
Old 11-22-2011
Thansk agama for your help!!!

I put a tmp as you said and remove the tmp after that. Things work well!!
Thanks again!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

For loop to accept params and delete folder/files

Hi Folks - I'm trying to build a simple for loop to accept params and then delete the folder & files on the path older than 6 days. Here is what I have: Purge () { for _DIR in "$1" do find "${_DIR}"/* -mtime +0 -exec rm {} \; done } I would be passing... (4 Replies)
Discussion started by: SIMMS7400
4 Replies

2. Shell Programming and Scripting

sed to rename files in bash loop

I am trying to use sed to rename all .txt files in /home/cmccabe/test. However, I am getting an error that I seems to be putting the files in a new directory s, instead of in the original. Thank you :). bash # rename classified cd /home/cmccabe/test pattern2_old="_classify"... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. UNIX for Dummies Questions & Answers

Loop awk command on files in a folder

Hi, I'd like to loop an action over all files with given extension within a folder. The "main" action is: awk -F "\t" 'BEGIN{OFS="\t"}{if ($10=="S") print$0; }' input.txt > output.txt The input.txt should be every file in the folder with *.subVCF extension; and the output should be a file... (3 Replies)
Discussion started by: dovah
3 Replies

4. Shell Programming and Scripting

How to use a loop for multiple files in a folder to run awk command?

Dear folks I have two data set which there names are "final.map" and "1.geno" and look like this structures: final.map: gi|358485511|ref|NC_006088.3| 2044 gi|358485511|ref|NC_006088.3| 2048 gi|358485511|ref|NC_006088.3| 2187 gi|358485511|ref|NC_006088.3| 17654 ... (2 Replies)
Discussion started by: sajmar
2 Replies

5. Shell Programming and Scripting

UNIX shell script - cut specified line and perform loop

Guys, I have a requirement as below. consider,if i use df command, its getting the below output. file system kbytes used avail %used Mounted on /dev/sample/ 45765 40000 5765 50% / /dev/filesys/ 30000 20000 1000 80% /u .... .... Now i wanted to cut the /u... (11 Replies)
Discussion started by: AraR87
11 Replies

6. Shell Programming and Scripting

Replace character in files of entire folder? sed? or what?

Hello, I do have several files in one folder each file contains measurement data. for each file I would like to replace the character "," by "." ? How can I do this and how can I do this for each file at once? E.G. data_1.dat, data_x.dat (original version) data_1out.dat, data_x_out.dat... (10 Replies)
Discussion started by: rollinator
10 Replies

7. Shell Programming and Scripting

For loop for number of files in a folder

Hi All, Need a for loop which should run for number of files in a folder and should pass the file name as parameter to another shell script for each loop. Please help me. Thanks. (2 Replies)
Discussion started by: chillblue
2 Replies

8. Shell Programming and Scripting

sed to rename files in a folder - please help with script

Hello, I am new to shell scripting and stuck on renaming files in a folder. The files have the format chp01_00001.wav chp01_00002.wav .... chp02_00001.wav chp02_00002.wav .... but I want them to have the following names: chp_bloomy_00001.wav chp_bloomy_00002.wav chp_bloomy_00003.wav... (8 Replies)
Discussion started by: Bloomy
8 Replies

9. Shell Programming and Scripting

Loop through text file > Copy Folder > Edit XML files in bulk?

I have a text file which contains lines in this format - it contains 105 lines in total, but I'm just putting 4 here to keep it short: 58571,east_ppl_ppla_por 58788,east_pcy_hd_por 58704,east_pcy_ga_por 58697,east_pcy_pcybs_por It's called id_key.txt I have a sample folder called... (9 Replies)
Discussion started by: biscuitcreek
9 Replies

10. Shell Programming and Scripting

Sed or awk script to remove text / or perform calculations from large CSV files

I have a large CSV files (e.g. 2 million records) and am hoping to do one of two things. I have been trying to use awk and sed but am a newbie and can't figure out how to get it to work. Any help you could offer would be greatly appreciated - I'm stuck trying to remove the colon and wildcards in... (6 Replies)
Discussion started by: metronomadic
6 Replies
Login or Register to Ask a Question