Sed question
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. File A : ====== servera yaddayaddablabla serverb yahdhdhydhhd serverc dhhdkkdkkdkd serverd ddkdkkdd ........ you get the idea File B : ======= serverc serverd 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 |
Instead of
Code:
sed '/^${i}/d' fileA>newfile Code:
sed '/^'${i}'/d' fileA>newfile |
Quote:
|
Code:
# join -v 1 file file1 |
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. |
Quote:
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 how about: sed -i /keyword/d file |
Quote:
I have to read a bit about the join command because I'm not sure what that one will do... Thanks for your comments! |
random thoughts
Quote:
Quote:
Quote:
|
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.
|
Quote:
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... |
well if you insert an 'exit' before 'done'...
[edit: and you rewrite newfile at each iteration] |
Are you sure you are really running this very script ?
Code:
#!/bin/sh Here is something that demonstrates it works: Code:
#!/bin/sh Code:
--- |
Quote:
is there some sort of limitation with sed with the numbers of itireation or something?????? I'm beating my head against the wall for this! |
All times are GMT -5. The time now is 12:04 AM. |