LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to use sed to find lines with special characters (https://www.linuxquestions.org/questions/linux-general-1/how-to-use-sed-to-find-lines-with-special-characters-928081/)

bryanwdly 02-07-2012 07:37 AM

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

trey85stang 02-07-2012 08:56 AM

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


bryanwdly 02-07-2012 12:54 PM

Not limited to sed will use awk
 
Thanks for the correction and insight...greatly appreciated...will build on my awk knowledge.


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