LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-21-2011, 11:31 PM   #1
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Rep: Reputation: 30
script


I have a database log ( db.log ) which the messages will be appended to it , some of the messages is non-useful , so I want to write a script to write all the lines that have the message "warning" to another log ( eg. db.log2 ), and then remove this line in db.log , as below example , can advise what can i do ? Thanks.


The log is as below
===================
#vi db.log

"
aaa
bbb
ccc
warning 111
ddd
eee
warning 222
fff
"



run the script , the desired output is as below
===============================================
#vi db.log
aaa
bbb
ccc
ddd
eee
fff


#vi db.log2
warning 111
warning 222
 
Old 07-21-2011, 11:35 PM   #2
(=AA=)
LQ Newbie
 
Registered: Dec 2002
Location: UK
Distribution: Mostly FreeBSD and CentOS
Posts: 24

Rep: Reputation: 3
cat db.log | grep warning > warning-db.log
cat db.log | grep -v warning > no-warnings-db.log

warning-db.log - this would be the log file containing the warnings only.
no-warnings-db.log - this would be the log file minus the warnings.

Original log file remains untouched.
 
Old 07-22-2011, 01:34 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
Actually, you don't need cat
Code:
grep warning db.log > warning-db.log
grep -v warning db.log > no-warnings-db.log
 
Old 07-22-2011, 01:37 AM   #4
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by (=AA=) View Post
cat db.log | grep warning > warning-db.log
cat db.log | grep -v warning > no-warnings-db.log

warning-db.log - this would be the log file containing the warnings only.
no-warnings-db.log - this would be the log file minus the warnings.

Original log file remains untouched.
Thx reply,

your method is simple , but I would like to keep the original file name of log to db.log , as if use your method , the file name would be changed .

The reason for do not want to change file name is because db.log is the standard file name , many users use this file name .

Thanks.
 
Old 07-22-2011, 02:02 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
The problem is I'd expect db.log to be always (or most of the time) open & being written to by the db logger.
You shouldn't try to change the content if this is the case.
OTOH, if this is all after you've moved onto a new log file, then you can rename the no-warnings-db.log to db.log
 
Old 07-22-2011, 02:30 AM   #6
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by chrism01 View Post
The problem is I'd expect db.log to be always (or most of the time) open & being written to by the db logger.
You shouldn't try to change the content if this is the case.
OTOH, if this is all after you've moved onto a new log file, then you can rename the no-warnings-db.log to db.log
Yes , the db.log is always read / open by user.

I want to update the db.log daily or weekly .
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash and xterm: how make apps started by and for a script persist when script terminates porphyry5 Linux - General 4 06-15-2011 02:27 PM
[SOLVED] Script question: create a shell script in kde to log in on a server with ssh c4719929 Linux - Newbie 1 01-31-2011 04:05 AM
Need help with script to organise files into folders as part of DVD backup script jasybee2000 Linux - Newbie 5 06-15-2009 08:29 PM
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 09:40 AM
MySQL Updates With Null When Perl Script Run From Shell Script ThisGuyIKnow Programming 6 08-12-2008 10:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration