1753846 Members
8084 Online
108807 Solutions
New Discussion юеВ

using sed on hp-ux

 
SOLVED
Go to solution
chrkc
New Member

using sed on hp-ux

I have a file named "test.txt" in which I have the contents as

test1
test2
test3
BEGIN
test4
test5
test6
END
test7
test8
test9
BEGIN
test10
test11
END
----

Now I want to have the file modified in such a way that what ever is there in between BEGIN and END should be deleted provided BEGIN statement should exist.

The output should be
---
test1
test2
test3
BEGIN
test7
test8
test9
BEGIN

using sed as
sed '/^BEGIN/,/^END/{//p;d;}' test.txt | sed '/^END/d'
I was able to perform well enough on other linux envirinments but not on hp-ux.
This statement is deleting BEGIN also.

Please help me on this

Thanks
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor
Solution

Re: using sed on hp-ux

Hi:

Well, Perl makes this trivial:

# perl -ne 'if (/^BEGIN/../^END/) {next unless /^BEGIN/};print' text.txt

Regards!

...JRF...
chrkc
New Member

Re: using sed on hp-ux

Hi James,
Many thanks for the solution. I got the answer as needed.

Lets say if we have ;[BEG_INCLUDE]%I .... ;[END_INCLUDE] instead of BEGIN...END respectively.

For example
test1
test2
test3
;BEG_INCLUDE]%I Begin
test4
test5
test6
;[END_INCLUDE]
test7
test8
test9
;[BEG_INCLUDE]%I Begin
test10
test11
;[END_INCLUDE]

When I tried with your perl statement it said
# perl -ne (/^\;\[BEG_INCLUDE\]\%I/../^\;\[END_INCLUDE\]/) {next unless /^\;\[BEG_INCLUDE\]\%I/};print' test_include.txt
ksh: syntax error: `(' unexpected


# perl -ne (/^;[BEG_INCLUDE]%I/../^;[END_INCLUDE]/) {next unless /^;[BEG_INCLUDE]%I/};print' test_include.txt
ksh: syntax error: `(' unexpected

Thanks in advance
James R. Ferguson
Acclaimed Contributor

Re: using sed on hp-ux

Hi (again):

You used:

# perl -ne (/^;[BEG_INCLUDE]%I/../^;[END_INCLUDE]/) {next unless /^;[BEG_INCLUDE]%I/};print' test_include.txt

...but it needs to be:

# perl -ne 'if (/^;[BEG_INCLUDE]%I/../^;[END_INCLUDE]/) {next unless /^;[BEG_INCLUDE]%I/};print' test_include.txt

...that is, you dropped the leading single quote after the options '-ne' and you lost the 'if'.

Regards!

...JRF...
chrkc
New Member

Re: using sed on hp-ux

Oops my apologies.
Just overlooked at the syntax.

Thanks a bunch!!

I got the solution.

Thanks
Steven Schweda
Honored Contributor

Re: using sed on hp-ux

> [...] well enough on other linux
> envirinments but not on hp-ux [...]

If you prefer the way that GNU "sed" works,
then it should be possible to install and use
GNU "sed" on your HP-UX system, too.

http://www.gnu.org/software/sed/
Dennis Handly
Acclaimed Contributor

Re: using sed on hp-ux

You may want to just give up on sed to do this type of complex task. Use perl or awk where you have a procedural language.

It appears that your immediate problem is that your "//" doesn't particularly match anything predictable. Though sed(1) says:
a /regular expression/ in the style of ed(1) modified thus:
And ed(1) says:
The null RE (for example, //) is equivalent to the last RE encountered.

This implies your command is:
sed '/^BEGIN/,/^END/{/^END/p;d;}' ...

So change it to:
sed '/^BEGIN/,/^END/{/^BEGIN/p;d;}' ...

Which matches what JRF had to use in perl.
chrkc
New Member

Re: using sed on hp-ux

Hi Dennis,

Thanks for the idea.
Yes it worked perfectly with the sed you have suggested.

Actually, I wrote a script that actually deletes the block of code in between
;[BEG_INCLUDE] %i filename.txt
...
...
...
;[END_INCLUDE]
and replaces the string
;[BEG_INCLUDE] %i filename.txt to
%i filename.txt

The script which I wrote as

sed '/^\;\[BEG_INCLUDE\]\%I/,/^\;\[END_INCLUDE\]/{//p;d;}' $pgm_name | sed '/^\;\[END_INCLUDE\]/d' > $Temp_Name

sed 's/;\[BEG_INCLUDE\]\%I/\%I/g' $Temp_Name > $Wrk_Name

These two statements performs the above said operation and was infact working on CentOS and all other Linux flavours but not at all on HP-UX korn shell.

But now I understood better on sed and many thanks for the solution.

Thanks