LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-16-2016, 10:29 AM   #1
sysmicuser
Member
 
Registered: Mar 2010
Posts: 353

Rep: Reputation: 0
Unhappy How to delete all lines in a file starting with a string/date ?


Good folks,

I have a tomcat output file "catalina.out" and I want to delete all lines with a particular string, in this instance date 13-Aug-2016.

I tried the below, unfortunately it didn't work. Moreover the who lot of text was rolling over on my screen for quite a few minutes because of which I couldn't do any work, I suspect it impacted memory and cpu usage too.

Sample lines from catalina.out file are as below:

Code:
13-Aug-2016 23:14:26.427 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
13-Aug-2016 23:14:26.427 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/opt/tomcat
13-Aug-2016 23:14:26.427 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/opt/tomcat
13-Aug-2016 23:14:26.427 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/opt/tomcat/temp
13-Aug-2016 23:14:26.427 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
13-Aug-2016 23:14:26.608 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
13-Aug-2016 23:14:26.663 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
13-Aug-2016 23:14:26.681 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"]
13-Aug-2016 23:14:26.683 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
13-Aug-2016 23:14:26.694 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 1015 ms
13-Aug-2016 23:14:26.740 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
13-Aug-2016 23:14:26.740 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.4
13-Aug-2016 23:14:26.763 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /opt/apache-tomcat-8.5.4/webapps/ROOT
13-Aug-2016 23:16:20.064 INFO [localhost-startStop-1] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [112,513] milliseconds.
And code/sed snippet is below:

Code:
sed '/^13-Aug-2016/d' catalina.out
sed '/^13\-Aug\-2016/d' catalina.out
Both of the above unfortunately didn't work.

Please assist, Thanks heaps in advance.
 
Old 08-16-2016, 10:39 AM   #2
sysmicuser
Member
 
Registered: Mar 2010
Posts: 353

Original Poster
Rep: Reputation: 0
Got this hack with vim, but I don't want to open the file. I prefer it with some sort of one liner or with sed.

Code:
:%g/13-Aug-2016/d
 
Old 08-16-2016, 10:54 AM   #3
JockVSJock
Senior Member
 
Registered: Jan 2004
Location: SATX
Distribution: RHEL/CentOS
Posts: 1,211
Blog Entries: 4

Rep: Reputation: 131Reputation: 131
Use could use find and then either xargs or exec to run an execution of what was returned.

I would recommend running the find command first and make sure that you are in agreement with the results before deleting anything.

Code:
find /path/to/files -mtime 3 type -f | xargs rm -rf *
 
Old 08-16-2016, 11:00 AM   #4
sysmicuser
Member
 
Registered: Mar 2010
Posts: 353

Original Poster
Rep: Reputation: 0
No, I don't want to delete multiple files but multiple lines with a matching string in a file.
 
Old 08-16-2016, 11:28 AM   #5
killingthemonkey
Member
 
Registered: Mar 2011
Location: Winston-Salem, NC
Distribution: Fedora, CentOS
Posts: 125

Rep: Reputation: 12
I was able to get this to work:

Code:
sed -i -e "/^15-Aug.*$/d" testfile
EDIT: You of course, replace '15-Aug' with your own string.

EDIT 2: Oh yeah, and change testfile to your file name.

Last edited by killingthemonkey; 08-16-2016 at 11:43 AM. Reason: Completeness
 
Old 08-16-2016, 11:34 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,491

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Your first example worked just fine as is for me?? I added an additional line with a different date to confirm and it was the only line returned. Not sure what to add??

I noticed your in Windows now ... is it possible it has dodgey line endings?
 
Old 08-16-2016, 04:15 PM   #7
vincix
Member
 
Registered: Feb 2011
Distribution: Centos 6.7, 7
Posts: 656

Rep: Reputation: 57
I also don't understand why your first example doesn't work. As far as I can see, if you use the 'd' option, then sed deletes all lines that contain the respective string. You don't have to match the entire line.
 
Old 08-16-2016, 04:24 PM   #8
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,687

Rep: Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259Reputation: 1259
Wouldn't it be simpler to do a "grep -v '^13-Aug-2016' ..."
 
Old 08-16-2016, 05:35 PM   #9
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 349

Rep: Reputation: 163Reputation: 163
The sed scripts in post#1 work; the output goes to the screen by default.
You can redirect it to a new file
Code:
sed '/^13-Aug-2016/d' catalina.out > catalina.out.new
And if it looks good, cp or mv it back to the input file.
Or in-place edit the input file with the -i option
Code:
sed -i '/^13-Aug-2016/d' catalina.out
 
Old 08-25-2016, 01:29 AM   #10
sysmicuser
Member
 
Registered: Mar 2010
Posts: 353

Original Poster
Rep: Reputation: 0
Thanks all, grail I wanted to do on command line not by opening file in vim.

Now, -i -e should be used or just -i ?
 
Old 08-25-2016, 02:22 AM   #11
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,172

Rep: Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672
why don't you try it? why don't you read the man page of sed?
sed and vi understand the same commands, so the command worked in vi will also work with sed.
 
Old 08-25-2016, 10:25 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,491

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Not sure what you meant about vim as I did not mention it?? As for the switches, have a look at the man page as it is quite descriptive about what -i and -e does.
 
  


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] delete all lines in a file which consists of particular string dinakumar12 Linux - Newbie 8 03-22-2011 01:17 AM
find a string in a file and delete that line as well as x lines after tdnnash25 Linux - Server 3 06-24-2010 02:13 PM
[SOLVED] Delete all lines containing a string, plus 4 lines below it? RedHelix Linux - Newbie 4 01-27-2010 09:13 AM
Delete Duplicate Lines in a file, leaving only the unique lines left xmrkite Linux - Software 6 01-14-2010 06:18 PM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM


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

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