Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

hpux ksh add line after pattern

Posted on 2012-09-13
5
Medium Priority
?
788 Views
Last Modified: 2012-09-14
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!
0
Comment
Question by:Watnog
  • 2
  • 2
5 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 38395223
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
 

Author Comment

by:Watnog
ID: 38395288
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
 
LVL 31

Accepted Solution

by:
farzanj earned 2000 total points
ID: 38395546
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
 
LVL 2

Expert Comment

by:namethis
ID: 38395914
#/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
 

Author Closing Comment

by:Watnog
ID: 38397818
I tried a whole lot of things, but apparently overlooked the most obvious.
Thank you very much.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question