LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   need help with sed/awk (https://www.linuxquestions.org/questions/programming-9/need-help-with-sed-awk-791623/)

jbeiter 02-25-2010 09:03 PM

need help with sed/awk
 
at least I assume this would best be done with sed/awk. I have a list of 900+ rpms that need to be installed to match a dev system.. problem is 400 of them are already installed so rpm is bitching then giving up.

I need to take my list of 400 "sorry blah.blah.blah is already installed" and remove the blah.blah.blah line from my script of 900+ wgets.

so I need to parse file 1 and each line that matches a line with that package in file 2, removes it from the resulting file 3. (at least that's my line of thought)

I'm not much of a programmer and I'm sure someone out there can figure this out in minutes compared to the many days it will take me to get this to work.

Here is a sample of lines from each file:

file1:
package basesystem-8.0-5.1.1 is already installed

file2:
wget http://foo-server/RHEL5/basesystem-8.0-5.1.1.noarch.rpm

so file three will leave all of the wget for packages which do not appear in file1 (or some clever way of just removing those lines from file2.)

If there's an easier way to do this (ie: better way to run rpm -ivh *.rpm that won't choke) or using bash or perl, I'm open to enlightenment.

GrapefruiTgirl 02-25-2010 09:20 PM

Code:

for i in  $(awk '{print $2}' file1); do
 sed -i "/$i/d" file2
done

Looks pretty simple, and is not fully tested by me with your exact files (and their contents) so make sure to have a backup of your file2 before proceeding.

This just grabs each occurrence of the packagename in file1, and deletes any line containing that package, from file2.


Sasha

jbeiter 02-26-2010 05:29 AM

I think this worked. Thank you so much Sasha!

ps: I love your quote :)

grail 02-26-2010 05:50 AM

Just a quick two cents, but you can replace the awk with a simple cat if you like:

for i in "$(cat file1)"; do
sed -i "/$i/d" file2
done

Side note: Grapefruitgirl - how do you get the code box?

whizje 02-26-2010 06:33 AM

Go to advanced in Quick Reply box and select the text you want in the code box and hit # in the menu.

pixellany 02-26-2010 07:06 AM

Quote:

Originally Posted by grail (Post 3877638)
Just a quick two cents, but you can replace the awk with a simple cat if you like:

for i in "$(cat file1)"; do
sed -i "/$i/d" file2
done

Side note: Grapefruitgirl - how do you get the code box?

How is this equivalent to the AWK statement which only returns the 2nd field?

grail 02-26-2010 08:18 AM

Sorry, my bad ... thought it was only the filename there <walks off smaking himself about the head and ears :( >

GrapefruiTgirl 02-26-2010 11:13 AM

Quote:

Originally Posted by whizje (Post 3877677)
Go to advanced in Quick Reply box and select the text you want in the code box and hit # in the menu.

@ grail,

yes, the above will get you [code] tags too, by going to the "Advanced Editor" and pressing the "#" icon; but usually, since most of the time I'm using the "Quick Reply" field at the bottom of a page, I just manually put the [code] tags above and below the code.

code <--here
blah blah
/code <--and here

And put those words "code" in [square brackets]

Sasha


All times are GMT -5. The time now is 02:09 AM.