LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Removing same line out of many files (https://www.linuxquestions.org/questions/programming-9/removing-same-line-out-of-many-files-410163/)

mijohnst 01-31-2006 06:32 PM

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
mv -f 1.tmp 1.dat

I'm sure there is a much better way. I'd just like to look at an example because everything that I'm doing to do this in mass with sed isn't working. Thanks again!

Tinkster 01-31-2006 07:34 PM

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

Booster 01-31-2006 07:35 PM

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 ;)
for each in `ls -1 *.dat` # note that that is ls -"one", not -"ell"
do
  sed 's/Last_Name//g' $each > $PWD/$each
done

PLEASE copy your data to a temp directory before trying the above! I haven't tried it! ;)

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!

Booster 01-31-2006 07:36 PM

Quote:

Originally Posted by Tinkster
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

Heh - you had a simpler response, and hence beat me to it. Thanks, btw, I often forget the simple solutions. ;)

mijohnst 01-31-2006 10:09 PM

That's easy enough... :) For fun sake, what if you had many directories of files?

For example:
Code:

/opt/program/datefiles1/
/opt/program/datafiles2/
/opt/program/datafiles3/

Maybe use something like:
Code:

find /opt/program/ -name "*.dat" | sed 's/Last_Name/d'
I like your idea about making a little script Booster, I was just trying to think if it could be done in a one line command. Thanks for the help... Always fun learning!

Tinkster 01-31-2006 10:12 PM

Quote:

Originally Posted by mijohnst
That's easy enough... :) For fun sake, what if you had many directories of files?

For example:
Code:

/opt/program/datefiles1/
/opt/program/datafiles2/
/opt/program/datafiles3/

Maybe use something like:
Code:

find /opt/program/ -name "*.dat" | sed 's/Last_Name/d'
I like your idea about making a little script Booster, I was just trying to think if it could be done in a one line command. Thanks for the help... Always fun learning!

Why the pipe?! :}

Code:

find /opt/program/ -name "*.dat" -exec sed -i 's/Last_Name/d' {} \;

Cheers,
Tink

mijohnst 01-31-2006 10:25 PM

Why the pipe? Because I don't know any better... lol

Thanks Tink... :)

Booster 01-31-2006 10:34 PM

Quote:

Originally Posted by Tinkster
Why the pipe?! :}

Code:

find /opt/program/ -name "*.dat" -exec sed -i 's/Last_Name/d' {} \;

Cheers,
Tink

Why the find?! ;)
Code:

for each in `ls -1R /opt/program/* | grep "\.dat"
do
  sed 's/Last_Name//g' $each > $each.tmp
  mv $each.tmp $each
done

:D

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!

kshkid 01-31-2006 10:35 PM

Quote:

Originally Posted by mijohnst
That's easy enough... :) For fun sake, what if you had many directories of files?

For example:
Code:

/opt/program/datefiles1/
/opt/program/datafiles2/
/opt/program/datafiles3/

Maybe use something like:
Code:

find /opt/program/ -name "*.dat" | sed 's/Last_Name/d'
I like your idea about making a little script Booster, I was just trying to think if it could be done in a one line command. Thanks for the help... Always fun learning!

if you had intented to use a pipe anyway,
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.