[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

perl command line substitution problem

Posted on 2005-04-11
8
Medium Priority
?
302 Views
Last Modified: 2008-02-01
I want to insert a line of text, "AAA" above another line, "BBB" in a text file called MYFILE.

I am using a shell script to call perl as follows:

string1="AAA"
string2="BBB"
perl -i -p -e 's/$string2/$string1\n$string2/' MYFILE

Problem is, $string1 and $string2 do not pass onto perl.  I have tried $ARGV[0] and $ARGV[1], but they are ready as input just like MYFILE.  How do I accomplish this either in a one-line execution like I am tring to do, or with a full blown perl script?  I prefer the single line call...

Thanks
0
Comment
Question by:chrabark
  • 5
  • 3
8 Comments
 
LVL 18

Expert Comment

by:kandura
ID: 13755837
string1="AAA"
string2="BBB"
perl -i -p -e 's/$ENV{string2}/$ENV{string1}\n$ENV{string2}/' MYFILE
0
 

Author Comment

by:chrabark
ID: 13756014
Well, that did not work... no error, but no substitution took place either.

I am running ksh under Unix Tru64 5.1a if that helps...

CODE:

string1="END_TABLE_1"
string2="INSERT INTO users VALUES ('$1', '$2', '$3');"
echo $string1
echo $string2
perl -i -p -e 's/$ENV{string1}/$ENV{string2}\n$ENV{string1}/' test.txt


0
 
LVL 18

Expert Comment

by:kandura
ID: 13756049
what does

    string1="END_TABLE_1" perl -e 'print "string1: $ENV{string1}$/"'

print?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:chrabark
ID: 13756073
string1: END_TABLE_1
0
 

Author Comment

by:chrabark
ID: 13756134
but when I put the perl part of that statement into the script, it returns only "string1:"
0
 

Author Comment

by:chrabark
ID: 13756191
***** here is the test file I am using...

Some
Stuff
Here

BBB

***** here is the full test script I am using...

cp test.bak test.txt
string1="AAA"
string2="BBB"
echo shell string1: $string1
echo shell string2: $string2
perl -e 'print "perl string1: $ENV{'string1'}$/"'
perl -e 'print "perl string2: $ENV{'string2'}$/"'
echo ----------
perl -i -p -e 's/$ENV{string1}/$ENV{string2}/' test.txt
cat test.txt

***** which yields....

shell string1: AAA
shell string2: BBB
perl string1:
perl string2:
----------
Some
Stuff
Here

BBB
0
 
LVL 18

Accepted Solution

by:
kandura earned 500 total points
ID: 13756700
What I think happens here is that the shell won't expand $string1 inside the perl -e string.

Still, shell variables are supposed to be passed around in the environment.
In bash, I have to say

    export string1="AAA"

and then this works:
    perl -e 'print $ENV{string1}'
   
0
 

Author Comment

by:chrabark
ID: 13757085
Okay, I got this to work...

export string1="AAA"
export string2="BBB"
perl -i -p -e 's/$ENV{'string1'}/$ENV{'string2'}/' test.txt

Thanks for your time and assistance !  Much appreciated !
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

829 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