Saturday, January 29, 2011

shell script to iterate through text files and delete a string

I have a few hundred text files in one directory with lists of users:

&usera
&userb
userc
&userd

note that some do not lead with the &. Also, in some files, the last line does not end with a newline character.

What is the simplest way in a shell script to go through all those files and remove, say userb, ensuring that each existing user remains on its own line?

thanks!

  • find /directory -name '*.users' -exec sed -i -r '/^&?userb$/d' {} \;
    
    carillonator : thanks! though I had to quote: `'{}'` the filename reference
    Gordon Davisson : You should probably change the sed command to `'/^&?userb$/d'` to avoid deleting, say, "otheruserbeth" by mistake.

0 comments:

Post a Comment