ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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!
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).
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.
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.
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!
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' {} \;
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'
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.