Link to home
Start Free TrialLog in
Avatar of Watnog
WatnogFlag for Belgium

asked on

hpux ksh add line after pattern

Hi Experts,

In a text a line needs appended when a pattern is found.
The pattern is in format xxx-yyy-zzz and is passed as a variable.

I found this line...
perl -lne 'print $_;print "Text To Insert" if(/PATTERN/);' inputfile > outputfile
That works fine but not with a variable.

my script looks like this:

#/bin/ksh
job=XXX-YYY-ZZZ
echo $job
input="path/to/input"
output="path/to/output"
perl -lne 'print $_;print "TEXT TO INSERT" if(/$job/);' $input > $output

Open in new window


The problem seems to be with the dashes...

If input file is this:
blahblahblah
blahblahblah
blah#XXX-YYY-ZZZ.blah
blahblah

Output is like this:
blahblahblah
TEXT TO INSERT
blahblahblah
TEXT TO INSERT
blah#XXX-YYY-ZZZ.blah
TEXT TO INSERT
blahblah
TEXT TO INSERT


It inserts at every line so it seems...
Any clue?

Thanks!
Avatar of farzanj
farzanj
Flag of Canada image

You are using korn shell not perl so you need to evaluate the KSH variable

perl -lne 'print $_;print "TEXT TO INSERT" if(/eval $job/);' $input > $output

Open in new window

Avatar of Watnog

ASKER

Thanks, No luck however.
The output is identical to input; nothing is inserted...

script:
#!/bin/ksh
job=XXX-YYY-ZZZ
echo $job
input="a"
output="b"
perl -lne 'print $_;print "TEXT TO INSERT" if(/eval $job/);' $input > $output

Open in new window


input:
blahblahblah
blahblahblah
blah#XXX-YYY-ZZZ.blah
blahblah

output:
blahblahblah
blahblahblah
blah#XXX-YYY-ZZZ.blah
blahblah
ASKER CERTIFIED SOLUTION
Avatar of farzanj
farzanj
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of namethis
namethis

#/bin/ksh
export job=XXX-YYY-ZZZ
echo $job
input="path/to/input"
output="path/to/output"
perl -lne 'print $_;print "TEXT TO INSERT" if(/$ENV{job}/);' $input > $output

Open in new window

Avatar of Watnog

ASKER

I tried a whole lot of things, but apparently overlooked the most obvious.
Thank you very much.