Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
In need of a script, please help [solved]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
Satori80
Tux's lil' helper
Tux's lil' helper


Joined: 24 Feb 2004
Posts: 137

PostPosted: Thu Aug 18, 2005 6:37 pm    Post subject: In need of a script, please help [solved] Reply with quote

I've tried to figure this out on my own but I give up. My messages file has been filled with BANDWIDTH_IN statements to the point where it's over 8 GB. I solved the problem but now I'd like to remove all those lines. At first I thought of grep and then sed but I can't find out how to make either do what I need. Every example I've found wants to remove duplicate lines or expressions from a line, not a whole line with a specific expression. :(

I need a quick n' dirty script or command line to search for each line containing 'BANDWIDTH_IN', delete the entire line from the file, and then save the file back to /var/log/messages.


Last edited by Satori80 on Wed Aug 31, 2005 10:18 am; edited 1 time in total
Back to top
View user's profile Send private message
R!tman
Veteran
Veteran


Joined: 18 Dec 2003
Posts: 1303
Location: Zurich, Switzerland

PostPosted: Thu Aug 18, 2005 7:56 pm    Post subject: Reply with quote

Maybe this works:
Code:
`cat file.you.want.to.change | sed -e '/BANDWIDTH_IN/D'` >> changed.file

But try first, like this
Code:
cat file.you.want.to.change | sed -e '/BANDWIDTH_IN/D' | grep BANDWIDTH_IN
Back to top
View user's profile Send private message
the_mgt
Apprentice
Apprentice


Joined: 05 Aug 2005
Posts: 259
Location: Germany, near Hannover

PostPosted: Thu Aug 18, 2005 9:05 pm    Post subject: Reply with quote

eh, don't call me stupid, but have you tried
Code:

cat /var/log/messages|grep -v 'BANDWIDTH_IN' > messages2
??

One thing about catting and grepping: NEVER ever cat from a file, pipe it through grep and write it back in the file you originally catted!!! This usually ends ugly!

Edited because my codeline was BS ;)


Last edited by the_mgt on Fri Aug 19, 2005 11:06 am; edited 2 times in total
Back to top
View user's profile Send private message
Leo Lausren
Apprentice
Apprentice


Joined: 24 Feb 2004
Posts: 198
Location: Denmark

PostPosted: Fri Aug 19, 2005 6:32 am    Post subject: Reply with quote

You could use sed
Code:
sed -i '/BANDWIDTH_IN/d' /var/log/messages

Best to run it with -e instead of -i first, to see if it does the right thing.
_________________
Blog: common sense – nonsense
Back to top
View user's profile Send private message
Satori80
Tux's lil' helper
Tux's lil' helper


Joined: 24 Feb 2004
Posts: 137

PostPosted: Fri Aug 19, 2005 8:34 am    Post subject: Reply with quote

R!tman wrote:
Maybe this works:
Code:
`cat file.you.want.to.change | sed -e '/BANDWIDTH_IN/D'` >> changed.file



Figured I'd try the first suggesiton first. I tried it on a test file and it worked as expected. It's working on a copy of my /var/log/messages file now, like so:

Code:
cat ./messages | sed -e '/BANDWIDTH/D' >> messages.2 && mv ./messages ./messages.old && mv messages.2 messages


After I verify that the edited messages file looks like it should I'll archive the old one just in case.

Thanks very much to all of you for your help! I appreciate the advice given by each of you.

This is why I love Gentoo. Of all the distros I've tried over the years, the Gentoo userbase is the best in the Linux world. I don't care what some of the more elitist primadonna user groups think of Gentoo, you guys that take time out of your day to help someone with such trivial yet frustrating issues are the ones that make Gentoo as great as it is.
Back to top
View user's profile Send private message
Earthwings
Bodhisattva
Bodhisattva


Joined: 14 Apr 2003
Posts: 7753
Location: Germany

PostPosted: Fri Aug 19, 2005 10:16 am    Post subject: Reply with quote

Leo Lausren wrote:
You could use sed
Code:
sed -i '/BANDWIDTH_IN/d' /var/log/messages

Best to run it with -e instead of -i first, to see if it does the right thing.

That version is what I thought of as well. I made a test run with a random file (5.5 MB) and using in-place editing is several times slower than creating a new file. Makes sense of course as appending lines to a file is much quicker than deleting lines in the middle of a file. Therefore for a 8 GB file creating a temporary file is the way to go if space is not an issue.
_________________
KDE
Back to top
View user's profile Send private message
Leo Lausren
Apprentice
Apprentice


Joined: 24 Feb 2004
Posts: 198
Location: Denmark

PostPosted: Sat Aug 20, 2005 3:18 pm    Post subject: Reply with quote

Earthwings wrote:
I made a test run with a random file (5.5 MB) and using in-place editing is several times slower than creating a new file. Makes sense of course as appending lines to a file is much quicker than deleting lines in the middle of a file. Therefore for a 8 GB file creating a temporary file is the way to go if space is not an issue.

That make a lot of sense, and space is usually available these days.
_________________
Blog: common sense – nonsense
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum