LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 02-07-2012, 07:37 AM   #1
bryanwdly
LQ Newbie
 
Registered: Feb 2012
Posts: 2

Rep: Reputation: Disabled
How to use sed to find lines with special characters


Given the following file content:

a|b|c|d|e
aa|bb|cc|d|e
x|y|z|c|e
x|y|c|f|r

Tried using the following sed command...but did not work

sed "/^.*|.*|c|/p" file

The correct output should be:

a|b|c|d|e
x|y|c|f|r

I am tring to find lines where c is in the third position.


Any suggestions?

Thanks

Last edited by bryanwdly; 02-07-2012 at 07:44 AM.
 
Old 02-07-2012, 08:56 AM   #2
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,090

Rep: Reputation: 41
do you have to use sed? awk would be a better choice...

Code:
$ cat test.txt 
a|b|c|d|e
aa|bb|cc|d|e
x|y|z|c|e
x|y|c|f|r
$ awk -F"|" '{if($3 == "c")print}' test.txt 
a|b|c|d|e
x|y|c|f|r

##And sed if you must....
$ sed "/^.*|.*|c|.*|.*/ ! d" test.txt 
a|b|c|d|e
x|y|c|f|r
Also the problem with your sed line... is you are matching the following in quotes... sed for the most part is greedy which is why you are getting the second line matched.
Code:
"a|b|c|"d|e
x|"y|z|c|"e
"x|y|c|"f|r

Last edited by trey85stang; 02-07-2012 at 09:03 AM.
 
1 members found this post helpful.
Old 02-07-2012, 12:54 PM   #3
bryanwdly
LQ Newbie
 
Registered: Feb 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Not limited to sed will use awk

Thanks for the correction and insight...greatly appreciated...will build on my awk knowledge.
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] using sed to trim lines greater than maximum number of characters cxny Linux - Software 10 09-03-2011 01:25 PM
Using sed to replace special characters in a file wskibum Linux - Software 2 03-24-2011 09:47 PM
Replacing lines in files that contain special characters arizonagroovejet Linux - General 3 06-22-2009 09:19 PM
Help w/ sed parsing special characters clem_c_rock General 8 08-31-2007 04:06 PM
using sed to insert lines with special characters disorderly Linux - Software 26 04-20-2006 05:30 PM


All times are GMT -5. The time now is 11:27 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration