LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 09-16-2013, 02:43 PM   #1
shusai
LQ Newbie
 
Registered: May 2011
Posts: 2

Rep: Reputation: 0
sed: remove lines from a text file not containing parentheses


I want to delete lines from a text file which do not contain parentheses, e.g.:

This is the first line.
This is (123) another line.
Another line.
One more (useless) line.

The result should be:

This is (123) another line.
One more (useless) line.

I found out that
Code:
sed '/(/!d' file > output
should do what I want, but it gives me an empty file.

How can I delete lines NOT containing parentheses?
Thank you for your help!
 
Old 09-16-2013, 03:20 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Personally, I would just grep for parenthesis and toss it into a new file. Something like

Code:
grep "(" file > newfile
Unless there's some reason this wouldn't work for you.
 
1 members found this post helpful.
Old 09-16-2013, 03:29 PM   #3
kabamaru
Member
 
Registered: Dec 2011
Location: Greece
Distribution: Slackware
Posts: 276

Rep: Reputation: 134Reputation: 134
There are many ways you can do this. The thing is, your command _should_ work (works fine here). Do you by any chance use the same file as input, as well as output? If that's the case, yes your file's contents will just get erased. So, either you choose an output file other than the input file, or edit the original file in place (generally unsafe) with:

Code:
sed -i '/(/!d' file

Last edited by kabamaru; 09-16-2013 at 03:30 PM.
 
1 members found this post helpful.
Old 09-16-2013, 10:36 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by shusai View Post
I want to delete lines from a text file which do not contain parentheses, e.g.:

This is the first line.
This is (123) another line.
Another line.
One more (useless) line.

The result should be:

This is (123) another line.
One more (useless) line.

I found out that
Code:
sed '/(/!d' file > output
should do what I want, but it gives me an empty file.

How can I delete lines NOT containing parentheses?
Thank you for your help!
Try "grep '(' file >output". No real need for sed.
 
1 members found this post helpful.
Old 09-17-2013, 11:04 AM   #5
shusai
LQ Newbie
 
Registered: May 2011
Posts: 2

Original Poster
Rep: Reputation: 0
Thank you all for your help, both the grep and sed with -i work!
 
  


Reply



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] sed use - extract substring from lines of text file tastiero Linux - Newbie 26 03-12-2012 04:10 AM
Remove lines in text file that contain two '@' symbols xsyntax Linux - Newbie 5 12-07-2009 05:58 PM
sed to remove specific lines in a file tekmann33 Linux - Newbie 3 05-21-2009 03:41 PM
Remove lines in a text file based on another text file asiandude Programming 10 01-29-2009 10:59 AM
Remove odd lines from a text file Mr. Gone Programming 2 09-19-2005 11:16 AM

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

All times are GMT -5. The time now is 01:25 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
Open Source Consulting | Domain Registration