How can I script changes to a few hundred files
August 15, 2013 2:35 PM   Subscribe

I have a few hundred text files, all in the same directory , each of which contain a single line of text that looks like so: string OR string OR string ... OR string OR I need to add "EID(" to the beginning of each file, and replace the last "OR " with a ")" I am on a Mac and am fine with Terminal- it is how I got this far. At this point though, I am totally failing at getting the right awk or sed to make this happen. Hope me?

My normal approach to this is to Google until I find the exact thing I am trying to do. This time I am close, but not quite there. Clearly, I have spent too much time as a PM and too little time coding lately.

Things I have tried:

This got the EID( at the end instead of the beginning: for f in *; do echo 'EID(' >> "$f"; done

This trimmed the OR but just printed instead of saving: for f in *; do sed "s/...$//" $f; done

This printed the file names but didn't do anything: for f in *; do echo ${line:(-3)} $f; done

I can use the first command to get the end parentheses on, so really it is the the final trim and the first insert that are problems.
posted by rockindata to Computers & Internet (6 answers total) 1 user marked this as a favorite
 
Best answer:
for f in *; do
echo Processing $f
cat "$f" | sed -e '1,$s/^/EID(/' -e '1,$s/[ \t]*OR[ \t]*$/)/' > "${f}.fixed"
done

posted by spacewrench at 2:51 PM on August 15, 2013


Best answer: Spacewrench's script looks good but if you want to end up with the same file names as before, between cat and done, you could add:
mv "${f}.fixed" "$f" 

posted by aubilenon at 2:55 PM on August 15, 2013


Best answer: True. Or (since I'm an old gray-haired Unix geek with plenty of unpleasant experiences...):
mv "${f}" "${f}.orig" && mv "${f}.fixed" "${f}"
(damhik.)
posted by spacewrench at 2:59 PM on August 15, 2013 [2 favorites]


Response by poster: Awesome! Success! Thank you both very muchly!
posted by rockindata at 3:02 PM on August 15, 2013


For future reference, you can use
sed -ioriginal
to create an automatic backup ($filename.original here), obviating the mv commands, and -i also allows you to operate on all the files via a wildcard, like 'sed -ibak -e '...' *', eliminating the file loop.
posted by rhizome at 4:41 PM on August 15, 2013


Just in case anyone's interested, the free and awesome Textwrangler lets you do a search-and-replace of a folder of files using grep.

I find it a lot easier to practice on a couple of files with that first, rather than jumping straight into the terminal.
posted by derbs at 7:33 AM on August 16, 2013


« Older How much help should I accept from my husband who...   |   I know I want to keep it, but can't. So can you... Newer »
This thread is closed to new comments.