Operating System - HP-UX
1829614 Members
2047 Online
109992 Solutions
New Discussion

sed '/<regexp>/,//p where <regexp> contains "/"

 
SOLVED
Go to solution
Paul F Rose
Advisor

sed '/<regexp>/,//p where <regexp> contains "/"

I'm trying to generate a list of entries from a specific filename to the end of file.

Here is my test script:

# cat ./x
ls > x.tmp1
ls -d `pwd`/* > x.tmp2
echo "grep tcpdump x.tmp1"
grep tcpdump x.tmp1
echo "\ngrep tcpdump x.tmp2"
grep tcpdump x.tmp2
echo ""

set -v
sed -n "/tcpdump/,//p" x.tmp1
echo ""
sed -n "?/home/x40894/tcpdump?,??p" x.tmp2

And here is the output:

root@(pacsarch) [/home/x40894]
# ./x
grep tcpdump x.tmp1
tcpdump

grep tcpdump x.tmp2
/home/x40894/tcpdump

sed -n "/tcpdump/,//p" x.tmp1
tcpdump
trimfile.sh
unmount_archives.sh
vgpaths
x
x.tmp
x.tmp1
x.tmp2
xx
xxx
yy
yyy
echo ""

sed -n "?/home/x40894/tcpdump?,??p" x.tmp2
sed: ?/home/x40894/tcpdump?,??p is not a
recognized function.

As you can see, I can't get the sed command to parse correctly if the delimiter is not "/".

I've tried every variation I can think of, and I've tried various patterns of "\" escapes with no luck either.

????
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor

Re: sed '/<regexp>/,//p where <regexp> contains "/"

Hi:

# X="/tmp/some/path/and/file"

First with a "toothpick" syndrome:

# echo $X|sed -e 's/\//_/pg'
_tmp_some_path_and_file

Now, more readable with a '?' as a delimiter:

# echo $X|sed -e 's?/?_?pg'
_tmp_some_path_and_file

Regards!

...JRF...
Paul F Rose
Advisor

Re: sed '/<regexp>/,//p where <regexp> contains "/"

JRF:

Thanks for your speedy reply. Unfortunately, it didn't exactly solve my problem, but it did point me to a workaround:

sed -n -e 's?/?~?g' -e '/~home~x40894~tcpdump/,//p' x.tmp2 | sed -e 's?~?/?g'
/home/x40894/tcpdump
/home/x40894/trimfile.sh
/home/x40894/unmount_archives.sh
/home/x40894/vgpaths
/home/x40894/x
/home/x40894/x.tmp
/home/x40894/x.tmp1
/home/x40894/x.tmp2
/home/x40894/xx
/home/x40894/xxx
/home/x40894/yy
/home/x40894/yyy

I'm not really pleased with having two executions of sed, but

sed -n -e 's?/?~?g' -e '/~home~x40894~tcpdump/,//p' -e 's?~?/?g' x.tmp2
~home~x40894~tcpdump
~home~x40894~trimfile.sh
~home~x40894~unmount_archives.sh
~home~x40894~vgpaths
~home~x40894~x
~home~x40894~x.tmp
~home~x40894~x.tmp1
~home~x40894~x.tmp2
~home~x40894~xx
~home~x40894~xxx
~home~x40894~yy
~home~x40894~yyy

doesn't do what I thought.
James R. Ferguson
Acclaimed Contributor
Solution

Re: sed '/<regexp>/,//p where <regexp> contains "/"

Hi (again):

> I'm trying to generate a list of entries from a specific filename to the end of file

If that's the basic question, then delimiters aside:

# cat myfile
alpha
bravo
charlie
delta
echo
foxtrot
golf

# sed -n '/delta/,$p' myfile
delta
echo
foxtrot
golf

...or:

# cat myfile2
/home/alpha/1
/home/bravo/2
/home/charlie/3
/home/delta/4
/home/echo/5
/home/foxtrot/6
/home/golf/7

# sed -n '/\/home\/delta\/4/,$p'

...compared to:

# sed -n '/\/home\/delta\/3/,$p'

...which yields nothing because there is no starting match in the range to print. The '$' denotes the end-of-file.

Regards!

...JRF...
Paul F Rose
Advisor

Re: sed '/<regexp>/,//p where <regexp> contains "/"

JRF:

I haven't been able to get back to this for a while.

Your method of escaping the '/' chars in the instead of the sed program worked like a charm and I find it quite "swellegant".

THX.