Removing same line out of many files
I'm kind of playing around with the sed command and found a task that it might come in useful, although I'm not doing something right.
Say that I have a line with the with the word "Last_Name" in a file called 1.dat. Then say about 100 of these files (1.dat, 2.dat, 3.dat, etc) in a directory. Wouldn't sed be the command that I would use to remove that "Last_Name" out of each one of those files at one time? This is what I've been doing one by one: Code:
sed 's/Last_Name/d' 1.dat >>1.tmp |
Well ... if you know for sure that what you're about to do
is the action that will lead to the result you desired.... (If your sed is version 3 or higher). sed -i 's/Last_Name/d' * Cheers, Tink |
hmmm, neato problem. ;)
Off the top of my head: Code:
mkdir $PWD/tmp # just in case you try this - we'll not remove the originals ;) You were missing a slash in your sed example, it works like "sed 's/search_for_this/replace_with_this/' I'm not sure if you wanted to delete the LINE that "Last_Name" is in or just the string "Last_Name"... if it's the line use the "/d" at the end of sed command, if it's just the string (and every occurrence of the string) us the "/g" For reference, google "sed oneliners" and "bash advance scripting" - both are great documents. Cheers! |
Quote:
|
That's easy enough... :) For fun sake, what if you had many directories of files?
For example: Code:
/opt/program/datefiles1/ Code:
find /opt/program/ -name "*.dat" | sed 's/Last_Name/d' |
Quote:
Code:
find /opt/program/ -name "*.dat" -exec sed -i 's/Last_Name/d' {} \; Cheers, Tink |
Why the pipe? Because I don't know any better... lol
Thanks Tink... :) |
Quote:
Code:
for each in `ls -1R /opt/program/* | grep "\.dat" Sorry, just got such a good laugh out of the replies, I just had to. ;) Oh yes, the fun IS in the learning, so try to figure out if what I suggested would work or not! (I'm not sure, I just typed.... ;) ) Cheers! |
Quote:
then use xargs. Code:
find /opt/program/ -name "*.dat" | xargs sed 's/Last_Name/d' |
All times are GMT -5. The time now is 12:50 PM. |