PDA

View Full Version : [SOLVED] Exclude some matches with sed



Intrepid Ibex
February 18th, 2012, 03:08 PM
Hey,

The task is to edit this (http://www.pastebin.com/Wqy9ebsi) file by changing all that /sbin/ stuff to /usr/bin/, excluding /sbin/modprobe lines:

I only know how to do this by changing them back afterwards:

# sed -i "s|/sbin/|/usr/bin/|g" asd
# sed -i "s|/usr/bin/modprobe|/sbin/modprobe|g" asd

StephenF
February 18th, 2012, 03:11 PM
You want a negative lookahead assertion.

Intrepid Ibex
February 18th, 2012, 03:12 PM
Yes, I know, but I don't know how to do that.

StephenF
February 18th, 2012, 04:20 PM
Wanting to know < wanting to learn. Here is your know.

$ perl -pe 's|/sbin/(?!modprobe)|/usr/bin/|g' in.txt >out.txt

sed < perl

Here is the in-place version.

$ perl -pi'.bak' -e 's|/sbin/(?!modprobe)|/usr/bin/|g' file.txt

matt_symes
February 18th, 2012, 04:28 PM
Hi

Use Perl as suggested.

sed and grep do not implement lookaheads or look behinds.

Kind regards

Intrepid Ibex
February 18th, 2012, 06:15 PM
Wanting to know < wanting to learn. Here is your know.

Yeah, well, I'd much rather have someone just tell me how to do the single thing I'm never gonna do again, instead of reading some docs and finding out myself. Thanks.

The Cog
February 20th, 2012, 06:41 PM
You can do it in a single line with sed like this:

sed -i -e "s|/sbin/|/usr/bin/|g" -e "s|/usr/bin/modprobe|/sbin/modprobe|g" asd