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!
WatnogAsked:
Who is Participating?
 
farzanjCommented:
Sorry : Try this:
#/bin/ksh
job=XXX-YYY-ZZZ
echo $job
input="tt"
output="output1"
perl -lne 'print $_;print "TEXT TO INSERT" if(/'$job'/);' $input > $output

Open in new window

0
 
farzanjCommented:
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

0
 
WatnogAuthor Commented:
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
0
 
namethisCommented:
#/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

0
 
WatnogAuthor Commented:
I tried a whole lot of things, but apparently overlooked the most obvious.
Thank you very much.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.