Computing.Net > Forums > Programming > sed chokes on pipe

sed chokes on pipe

Reply to Message Icon

Original Message
Name: 4u2nv71
Date: December 19, 2007 at 06:59:01 Pacific
Subject: sed chokes on pipe
OS: AIX
CPU/Ram: P5
Model/Manufacturer: IBM
Comment:

Here is the code snippet:

egrep "$searchstring" $masterlist > $fcslist
sed "/$searchstring/d" $masterlist > $masterswap


Works great except when the searchstring looks like:

abc123|def456

That searchstring interprets the way I want it in the egrep (or)... but sed chokes on it of course, and does nothing. Is there a way to work around this? Perl?

The idea is to find the searchstring, and when found, remove it from the masterlist.


Report Offensive Message For Removal


Response Number 1
Name: klint
Date: December 19, 2007 at 08:56:39 Pacific
Reply: (edit)

Your use of the alternation character (|) is extended regular expression syntax. That's why you need to use egrep instead of grep. By default, sed uses basic regular expression syntax (just like grep.)

The following command-line option is from GNU sed 4.1.5:

<quote>
-r
--regexp-extended
Use extended regular expressions rather than basic regular expressions. Extended regexps are those that egrep accepts; they can be clearer because they usually have less backslashes, but are a GNU extension and hence scripts that use them are not portable. See Extended regular expressions.
</quote>

If you are using another version of sed, you won't have the sed -r option but you can still use alternation by quoting it with a backslash (just as if you were using grep): abc123\|def456.

By the way, the vertical bar character is not always called a pipe. It's only a pipe when you're using it to pipe the output of one process to the input of another process.

klint


Report Offensive Follow Up For Removal

Response Number 2
Name: 4u2nv71
Date: December 19, 2007 at 11:19:30 Pacific
Reply: (edit)

Klint,

No GNU - working in AIX.

And I think you would have a hard time finding anyone who knows how to power on a PC that doesn't know what a pipe character is. You are splitting hairs, which doesn't answer questions.

It would be like me saying your name is typically spelled with a C for Clint, which while true, but also pointless.



Report Offensive Follow Up For Removal

Response Number 3
Name: klint
Date: December 19, 2007 at 15:54:45 Pacific
Reply: (edit)

Sorry if my previous answer sounded rude but that was not the intention, and the bit about the name of the vertical bar character was just a bit of supplementary information.

The main part of the answer was that, since you are not using GNU, you have to quote the "|" with a backslash, like this: abc\|def. Does that answer it?


Report Offensive Follow Up For Removal

Response Number 4
Name: ghostdog
Date: December 21, 2007 at 04:30:35 Pacific
Reply: (edit)

use awk

searchstring="abc123|def456"
awk -v var=$searchstring '
BEGIN{ n=split(var,pattern,"|") }
$0 ~ pattern[1]{next}
$0 ~ pattern[2]{next}
1
' $masterlist


Report Offensive Follow Up For Removal

Response Number 5
Name: 4u2nv71
Date: December 27, 2007 at 08:36:41 Pacific
Reply: (edit)

Thanks for the replies, the quote on the pipe doesn't work since this sed call is getting passed variables, so where the escape goes is unknown.

The awk way, as much as I use and like awk, is a darn ugly expansion of a 1 line solution.

There must be a perl way...


Report Offensive Follow Up For Removal


Response Number 6
Name: klint
Date: December 28, 2007 at 16:55:17 Pacific
Reply: (edit)

The Awk I'm using, which is GNU Awk, works well with extended regular expressions. Hopefully awk on AIX does too. Try it just like this:

awk "!/$searchstring/" $masterlist > $masterswap

Here, you are asking awk to print any line that does not match the regular expression.

If that does not work, try "man sed" to see if the AIX version of sed has any options for supporting extended regular expressions. I haven't used AIX for 6 years so I don't know. Good luck.


Report Offensive Follow Up For Removal

Response Number 7
Name: User123456789
Date: January 3, 2008 at 04:14:23 Pacific
Reply: (edit)

Just replace "|" with "\|", no ?

Hi there.


Report Offensive Follow Up For Removal

Response Number 8
Name: klint
Date: January 3, 2008 at 04:37:30 Pacific
Reply: (edit)

User123456789, see the OP's reply of Dec 27th: "the quote on the pipe doesn't work since this sed call is getting passed variables, so where the escape goes is unknown."

As things stand at the moment, I think the one-line Awk solution is the best bet.

Cheers,
klint


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: sed chokes on pipe

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge