Watnog
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:
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!
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
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!
ASKER
Thanks, No luck however.
The output is identical to input; nothing is inserted...
script:
input:
blahblahblah
blahblahblah
blah#XXX-YYY-ZZZ.blah
blahblah
output:
blahblahblah
blahblahblah
blah#XXX-YYY-ZZZ.blah
blahblah
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
input:
blahblahblah
blahblahblah
blah#XXX-YYY-ZZZ.blah
blahblah
output:
blahblahblah
blahblahblah
blah#XXX-YYY-ZZZ.blah
blahblah
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
#/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
ASKER
I tried a whole lot of things, but apparently overlooked the most obvious.
Thank you very much.
Thank you very much.
Open in new window