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.
Hmm...have been struggling with this for awhile...
I have 2 files : file A and file B. File A contains a list of servers in a certain format, and file B contains servernames that need to be removed from File A.
Question : how do I use sed to remove / delete lines from file A using the contents of FileB? Servers in File B, that have entries in File A, those entries should be deleted.
I have this code but it doesnt seem to do the work for some erason..ended up with the same file! Appreciate if you can help.
--------------------
#!/bin/sh
for i in `cat fileB`;
do
sed '/^${i}/d' fileA>newfile
mv newfile fileA
echo ${i}
done
To remove lines beginning with the word "opt" from file "list"
i="opt"
sed /^$i/d list
OR
sed "/^$i/d" list
In this example, quoting is not required, since there is no ambiguous meaning. If quotes are used, they must be double-quotes to allow bash to expand $i.
What was the purpose of ${i}? The curly brackets don't seem necessary.
To remove lines beginning with the word "opt" from file "list"
i="opt"
sed /^$i/d list
OR
sed "/^$i/d" list
In this example, quoting is not required, since there is no ambiguous meaning. If quotes are used, they must be double-quotes to allow bash to expand $i.
What was the purpose of ${i}? The curly brackets don't seem necessary.
I'm just used to putting the curly brackets :-P I read somewhere that its good practice..
I did what you mentioned...it worked if I declared i="opt" like, but not if I put all the patterns I want to match, in one file, like my initial example above....
It seems your basic loop is going to be very inefficient. For every value of "i" it makes one substitution and then writes a new file. If the files are large, it will be slow. Perhaps the solution using "join" is better.
Note:
instead of:
sed /keyword/d file > newfile
mv newfile file
It seems your basic loop is going to be very inefficient. For every value of "i" it makes one substitution and then writes a new file. If the files are large, it will be slow. Perhaps the solution using "join" is better.
Note:
instead of:
sed /keyword/d file > newfile
mv newfile file
how about:
sed -i /keyword/d file
Hmmm...it seems like it doesnt recognize the -i value..? (I'm on Solaris 5.8 machine).
I have to read a bit about the join command because I'm not sure what that one will do...
I'm just used to putting the curly brackets :-P I read somewhere that its good practice..
It's really not, IMO - just extra clutter. Though I suppose you could make an argument for consistency. I only use the full ${var} form when (a) it's required, such as for arrays and parameter expansion or (b) when it's practically required, such as with ambiguous strings - ${foo}bar because 'foobar' doesn't exist.
Quote:
Originally Posted by pixellany
Note:
instead of:
sed /keyword/d file > newfile
mv newfile file
how about:
sed -i /keyword/d file
pixellany - I recommend it but always note that it's not standard/portable.
Took me a long time to marshal all those quotes and I missed your reply - you already noted #2 ('-i' is a GNU extension to sed) and are looking into #3 (join). Sorry. I didn't *mean* to be redundant.
Took me a long time to marshal all those quotes and I missed your reply - you already noted #2 ('-i' is a GNU extension to sed) and are looking into #3 (join). Sorry. I didn't *mean* to be redundant.
That is OK
What do you mean I can do join as long as its sorted? Trying to read about join but would appreciate if you can explain a bit more.
Hmmmm this is funny, when I ran this, to check whether it can get the values from fileB and process it :
#!/bin/sh
for i in `cat fileB`;
do
sed '/^'$i'/d' fileA>newfile
echo $i
exit
done
I ended up with a newfile that was minus the line that matched first $i pattern! So why doesnt it work when it keeps going to the end of the loop???? I dotn understand why it work with only the first $i. Grrrrrrrrr...
Last edited by wondergirl; 02-21-2008 at 09:08 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.