bash script help

Soldato
Joined
4 May 2009
Posts
3,370
Location
Southampton
Hi All,

Been trying to create a script that will delete lines from multiple files according to the contents of a template file but I can't seem to get it right.

Code:
file_location="/tmp/*"
template_file=bleh.txt

for file in $(ls -1 ${file_location})
do
     for remove in $(cat ${template_file}
       do
          sed '/'${remove}'/d' ${file}
    done
done

I'm thinking of something like the above, could anyone help?

I think I also have to output the edits to a new file
Code:
sed '/'${remove}'/d' ${file} >${file}_new
mv ${file}_new ${file}
but not sure how t go about this
 
Last edited:
ok so having this:

Code:
file_location="/tmp/*"
template_file=bleh.txt

for file in $(ls -1 ${file_location})
do
     for remove in $(cat ${template_file}
       do
          sed '/'${remove}'/d' ${file} >${file}_new
    done
    mv ${file}_new ${file}
done

only saves the last edit to the output file..I can see why but how can it get all the changes for one file saved to the output file?
 
sed -i is not an option on this box (bash on AIX) and the only reason I am writting it to a new file is because its the only way it seems to work.

Code:
sed '/'${remove}'/d' ${file} >>${file}_new
using above takes the one line out and appends the remainder of the file to ${file}_new. This means you end up with multiple versions of the file in one file.
 
I've found the answer. If I put the file move within the 2nd loop it will take 1st instance of remove out and move it to the original file. this will then be the file that is used to look for the 2nd instance to delete.

Code:
file_location="/tmp/*"
template_file=bleh.txt

for file in $(ls -1 ${file_location})
do
     for remove in $(cat ${template_file}
       do
          sed '/'${remove}'/d' ${file} >${file}_new
          mv ${file}_new ${file}
    done
done
 
Back
Top Bottom