LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-10-2009, 12:05 AM   #1
sumitdevbharadwaj
LQ Newbie
 
Registered: Nov 2008
Posts: 20

Rep: Reputation: 0
Thumbs up 'sed' search query!!!


Hi All,

I have a tabular file, say:

$ cat testfile
ABC 123 Delhi
DEF 123 Agra
GHI 456 Bangalore
JKL 123 Mumbai

when I run run the command : sed -n '/123/p' testfile
it gives me correct o/p:
ABC 123 Delhi
DEF 123 Agra
JKL 123 Mumbai

NOW, what should be the command to get the third i.e. unmatched line only :
GHI 456 Bangalore

Thanks in advance.

Sumit Dev Bharadwaj
 
Old 07-10-2009, 12:23 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,124

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Better done with grep - it has a switch for this. Check the manpage.
 
Old 07-10-2009, 12:25 AM   #3
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71
Like this?
Code:
 sed -n '/123/!p' testfile
 
Old 07-16-2009, 01:50 PM   #4
sumitdevbharadwaj
LQ Newbie
 
Registered: Nov 2008
Posts: 20

Original Poster
Rep: Reputation: 0
Hey Thanks Uncle Theodore....I tried and it worked....cheers!!!
 
Old 07-16-2009, 02:17 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Are you sure that's what you want? (It will print EVERY LINE that does not contain the string "123")

What about the following:
Quote:
ABC 5123 New York
DEF 1235 Paris
 
Old 07-17-2009, 02:24 AM   #6
sumitdevbharadwaj
LQ Newbie
 
Registered: Nov 2008
Posts: 20

Original Poster
Rep: Reputation: 0
Yes Pixellany, thats what I wanted, all the lines except the ones containing 123.

In the example quoted by you above, yes I would like to have these lines:
ABC 5123 New York
DEF 1235 Paris

also for my output.

Thanks for your concern anyways... :-)
 
Old 07-17-2009, 02:32 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by sumitdevbharadwaj View Post
Yes Pixellany, thats what I wanted, all the lines except the ones containing 123.

In the example quoted by you above, yes I would like to have these lines:
ABC 5123 New York
DEF 1235 Paris

also for my output.
Well, then Uncle Theodore's code will not work!! The lines above will NOT be printed, because they contain "123".
 
Old 07-17-2009, 03:34 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Try grep as suggested above. Options -vw should do the trick.
 
Old 07-19-2009, 01:31 PM   #9
sumitdevbharadwaj
LQ Newbie
 
Registered: Nov 2008
Posts: 20

Original Poster
Rep: Reputation: 0
Oops...my mistake...I didn't look at the example carefully.
Pixellany you are right.

In the example above by Uncle Theodore, I would not like to have those lines quoted above. Moreover in my actual scenario, the column containing numbers has a definite number of digits, so this thing should not be an issue for me. Anyway, Uncle's command is working very fine for me!!!
Thanks colucix...once I try ur trick, I'll inform what was the result.
Cheers!!!
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash text search query basildon Linux - Newbie 2 09-17-2008 07:41 PM
help building a mysql search query Rickead2000 Linux - Software 1 10-17-2007 02:34 PM
sed query csdhiman Linux - Server 3 10-05-2007 07:18 AM
PHP search query? cdc5205 Programming 2 03-17-2006 08:15 PM
stacked search in sed reavm Programming 1 11-25-2004 11:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:55 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