LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-01-2005, 05:28 AM   #1
MikHud
Member
 
Registered: Jan 2002
Location: UK
Distribution: Ubuntu
Posts: 45

Rep: Reputation: 15
Embedding perl in bash script


I want to put the following perl 'find and replace' command into a script:-

perl -i.backup -p -e 's/old string/new string/g' *.pattern

- so that I only have to use the name of the script followed by old string and new string.

If I try to embed the above perl command in a bash script I can't figure out how to pass the string parameters to perl. Replacing the strings in the command with $1 and $2 or "$1" and "$2" doesn't work. Can perl access the args to the bash script or would it be best to just put turn this command into a perl script and pass the strings directly to it?

But then as a perl virgin I don't know how to put the perl command into a script and then pass the strings to it.

Any help appreciated.
 
Old 02-01-2005, 12:25 PM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 97
Shell script for you:

Code:
# Program change_file
# Syntax: change_file search_string replace_string file
v_search=$1
v_replace=$2
v_file=$3

perl -i.backup -e"s/${v_search}/${v_replace}/g" -p ${v_file}

# Optional Exctra
rm ${v_file}.backup
If you want to search for more than one word enclose the string in single quotes

EG:

change_file 'more than words' 'can say' target
 
Old 02-01-2005, 12:42 PM   #3
scissors
Member
 
Registered: Aug 2004
Location: Seattle
Distribution: Red Hat es and as, Solaris, Fedora, Debian, Tru64
Posts: 45

Rep: Reputation: 15
I would just make it all shell or all perl to minimize confusion, I would suggest all perl, to give yourself some more practice with it. What is it that you are trying to do? The perl line does not make sense to me. If you are simply trying to make a backup of files by passing the old and new string through, maybe this is what you want:

perl -i.$2 -p *.$1

If you write this all in perl, instead of $1 and $2 we use $ARGV[0] and $ARGV[1].
 
Old 02-01-2005, 01:03 PM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 97
The perl line is creating a backup of the file (extension .backup) and doing a global search and replace on the given strings the output is then stored in the original file.

To do this in shell only:

Code:
# Shell only

cp -p $3 $3.backup
sed 's/$1/$2/g' $3.backup > $3
I've not played with perl enough to translate into a pure perl script.
 
Old 02-02-2005, 06:26 AM   #5
MikHud
Member
 
Registered: Jan 2002
Location: UK
Distribution: Ubuntu
Posts: 45

Original Poster
Rep: Reputation: 15
Smile

Many thanks for your help Disillusionist.

I forgot about the difference between single and double quotes!

Regards
MikHud
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
embedded bash in perl script paul_mat Linux - Software 3 11-21-2005 01:46 PM
Help: Perl or Bash Script help embsupafly Programming 1 08-11-2005 06:01 PM
Need help with perl/bash script to parse PicBasic file cmfarley19 Programming 13 11-18-2004 05:06 PM
writing a bash/ perl script on debian phr00ta Programming 4 01-29-2004 01:38 PM
How to run a bash command in the background from perl script professorfrink Programming 3 11-13-2003 03:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration