Operating System - HP-UX
1826070 Members
3928 Online
109690 Solutions
New Discussion

Re: sed substitution using reg exp commands not working on HP-UX. Need help.

 
SOLVED
Go to solution
sree sakamuri
Advisor

sed substitution using reg exp commands not working on HP-UX. Need help.

Hello Guys

As part of script to strip off the absolute paths attached to the system commands(like rm, cp, grep etc), i used sed to do the job.

The sed commands i used, which are working fine on Linux, aren't working on HP-UX.

1) main code to strip off the abs paths of the cmds. **IMP**IMP**

sed -e 's#/usr.[^ ]*/\|/sbin.[^ ]*/\|/bin.[^ ]*/##g' $DEST_DIR/$1 > $DEST_DIR/new_$1
mv $DEST_DIR/new_$1 $DEST_DIR/$1

2)
sed -e "1i'$top'" $DEST_DIR/$1 > $DEST_DIR/new_$1
mv $DEST_DIR/new_$1 $DEST_DIR/$1

3)
sed -e '2i\export PATH=$PATH:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin/' $DEST_DIR/$1 > $DEST_DIR/new_$1
mv $DEST_DIR/new_$1 $DEST_DIR/$1

when the script is run, 1) is not returning any error but simply not doing the job. but 2) and 3) are returning errors as below.

error for 2)
sed: Function 1i'#!/usr/bin/ksh' cannot be parsed.

error for 3)
sed: Function 2i\export PATH=$PATH:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin/ cannot be parsed.

Please let me know where i am doing it wrong.
Any help is appreciated.

FYI : the actual script is attached.

Regards
19 REPLIES 19
James R. Ferguson
Acclaimed Contributor
Solution

Re: sed substitution using reg exp commands not working on HP-UX. Need help.

Hi:

You need to place the text to be inserted on a separate line in your 'sed' script, like:

# sed -e "1i\
$top"

See the manpages for 'sed'.

Regards!

...JRF...
sree sakamuri
Advisor

Re: sed substitution using reg exp commands not working on HP-UX. Need help.

Hello James

Thanks for your reply.

Tried your suggestion but no luck.

Still the same error...

i used as below,
sed -e "1i\
$top" $DEST_DIR/$1 > $DEST_DIR/new_$1

Please correct me.

Regards
James R. Ferguson
Acclaimed Contributor

Re: sed substitution using reg exp commands not working on HP-UX. Need help.

Hi (again) Sree:

Sorry, try this:

top=mystartpoint
echo "/path/to/file" | sed -e '1i\
'$top

Regards!

...JRF...
sree sakamuri
Advisor

Re: sed substitution using reg exp commands not working on HP-UX. Need help.

Hello James

Thanks for your quick reply.

Unfortunately this too doesn't work

the code i tried was,

echo $DEST_DIR/$1 | sed -e "1i\
"$top > $DEST_DIR/new_$1
sed: Function 1i#!/usr/bin/ksh cannot be parsed.

Please correct me if i am wrong.

Regards
James R. Ferguson
Acclaimed Contributor

Re: sed substitution using reg exp commands not working on HP-UX. Need help.

Hi (again):

Change the double quotes to single quotes as I wrote in my second post:

echo $DEST_DIR/$1 | sed -e '1i\
'$top > $DEST_DIR/new_$1

Regards!

...JRF...
Steven Schweda
Honored Contributor

Re: sed substitution using reg exp commands not working on HP-UX. Need help.

> The sed commands i used, which are working
> fine on Linux, aren't working on HP-UX.

Have you tried using GNU "sed" on the HP-UX
system?

http://www.gnu.org/software/sed/
ftp://ftp.gnu.org/gnu/sed/
sree sakamuri
Advisor

Re: sed substitution using reg exp commands not working on HP-UX. Need help.

Hi James

I changed to single quotes and used it on 3) of my original query. But now i am getting a different error.

code i used,

echo $DEST_DIR/$1 | sed -e '2i\
'export PATH=\$PATH:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin/ > $DEST_DIR/new_$1

sed: Cannot find or open file PATH=$PATH:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin/.


Sorry for the change as for 2) is solved using another workaround.

Yes, I followed GNU sed.

THanks & Regards
James R. Ferguson
Acclaimed Contributor

Re: sed substitution using reg exp commands not working on HP-UX. Need help.

Hi (again):

> Sorry for the change as for 2) is solved using another workaround.

Do you mean that my suggestion didn't work for you and that you did something different? To see what you "workaround" was would be nice.

> Yes, I followed GNU sed.

What does that mean; that you installed or *used* GNU 'sed' in lieu of the HP-UX 'sed'?

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: sed substitution using reg exp commands not working on HP-UX. Need help.

Hi (again):

Try:

echo $DEST_DIR/$1 | sed -e '2i\
'"export PATH=\$PATH:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin/" > $DEST_DIR/new_$1

That is, double-quote the "export PATH= ..."
so that 'sed' doesn't think that this is a file which it is to open an process.

Regards!

...JRF...
sree sakamuri
Advisor

Re: sed substitution using reg exp commands not working on HP-UX. Need help.

Hello James

It certainely doesn't mean your suggestion didn't work. the whole point of 2) was , when stripping off the abs paths from the script, the code shouldn't touch the header (like #!/usr/bin/ksh). I was using something complex like grepping the header into a variable and then deletign it only to be inserted again at the top from the variable content. But in HP-UX was facing problem doing the same.

But found a workaround, simple one liner, where we can make the sed to ignore a particular line when substituting.

like,
1!s/oldline/newline/g

so used it.


The answer about using GNU sed was for steven.

Sorry to confuse you.

Regards
sree sakamuri
Advisor

Re: sed substitution using reg exp commands not working on HP-UX. Need help.

Hello James

I tried your change and below is the code executing in debug mode. It truncated the whole file with a single line in it. pls check the below update.

echo $DEST_DIR/$1 | sed -e '2i\
'"export PATH=\$PATH:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin/" > $DEST_DIR/new_$1
+ sed -e 2i\
export PATH=$PATH:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin/
+ echo /u08/iBS/release/new_scripts_sree/test_file.sh
+ 1> /u08/iBS/release/new_scripts_sree/new_test_file.sh
mv $DEST_DIR/new_$1 $DEST_DIR/$1
+ mv /u08/iBS/release/new_scripts_sree/new_test_file.sh /u08/iBS/release/new_scripts_sree/test_file.sh

$ cat test_file.sh
/u08/iBS/release/new_scripts_sree/test_file.sh
$


Regards
sree sakamuri
Advisor

Re: sed substitution using reg exp commands not working on HP-UX. Need help.

Hello James.

I made a small change to your code and it worked. :)

Thanks a lot.

the code i used,

sed -e '2i\
'"export PATH=\$PATH:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin/" $DEST_DIR/$1 > $DEST_DIR/new_$1


I appreciate all your help.

Best Regards & THanks
Steven Schweda
Honored Contributor

Re: sed substitution using reg exp commands not working on HP-UX. Need help.

> The answer about using GNU sed was for steven.
>
> Sorry to confuse you.

It confused me, too.

My point was that if you want a "sed" on an
HP-UX system to work exactly like the (GNU)
"sed" on a GNU/Linux system, then it might
make some sense to use GNU "sed" on the HP-UX
system.

> Yes, I followed GNU sed.

I don't know what that means.
sree sakamuri
Advisor

Re: sed substitution using reg exp commands not working on HP-UX. Need help.

Hi Steven

Now i understand exactly what you mean.

Thanks for clarifying. Yes. THat is a important point to note. Can you kindly let me know how to make sure that both Linux and HP-UX are using GNU sed?

When i said i am following GNU sed, what i meant was, that i followed the sed coding rules laid out by GNU. But not exactly...

Sorry if i am still driving you nuts. :)

Regards
James R. Ferguson
Acclaimed Contributor

Re: sed substitution using reg exp commands not working on HP-UX. Need help.

Hi :

> I made a small change to your code and it worked. :)
> Thanks a lot.
> I appreciate all your help.

If you are happy with the answers you received, please read:

http://forums13.itrc.hp.com/service/forums/helptips.do?#28

Since you are new to the Forums, "welcome!".

Regards!

...JRF...
sree sakamuri
Advisor

Re: sed substitution using reg exp commands not working on HP-UX. Need help.


Will do it for sure.

You deserve it.

Best Regards
Steven Schweda
Honored Contributor

Re: sed substitution using reg exp commands not working on HP-UX. Need help.

> [...] how to make sure that both Linux and
> HP-UX are using GNU sed?

On a GNU/Linux system, I'd expect "sed" to be
GNU "sed".

sed --version

On HP-UX, you'd need to install GNU "sed".
After it's installed, you can run whichever
"sed" you wish to run by specifying an
appropriate path. For example

/usr/bin/sed
/usr/local/bin/sed

If you say only "sed", then what you get
depends on your PATH. Depending on your
shell:

which sed
type sed

echo $PATH

man
sree sakamuri
Advisor

Re: sed substitution using reg exp commands not working on HP-UX. Need help.


Hello Steven

Many Thanks for your reply.

That will make my life easier going forward.
Will keep a note of this for future reference.

Best Regards
sree sakamuri
Advisor

Re: sed substitution using reg exp commands not working on HP-UX. Need help.


Please refer to the updates for solution.