08-02-2011
Passing in regular expression as a parameter to sed
Hi All,
Im using Bash.
I have a pipe delimited config file which contains 3 columns,
1st column = location of a file to be chnaged
2 column = expression to find within file
3rd column = string to replace.
A script will loop through the contetnts of this file and apply the changes using sed.
The issue I have is that i cannot seem to get the metacharacters to work when passed in from the config file into the sed expression.
Example:
The below exmaple works fine for the string in the file to be changed:
string to be changed:
<rmi-port>13093</rmi-port>
line in config file:
samplefile.xml|<rmi-port>[0-9][0-9]|<rmi-port>18
via sed in the script, the resultant change becomes
<rmi-port>18093</rmi-port>
The problem occurs when I use simple metacharacters, no changes take place:
example using ^ for start of the : samplefile.xml|^<rmi-port>[0-9][0-9]|<rmi-port>18
The above will not work. It has to be an issue with the way the regular expression is being interpreted by the sed command within the script.
The sed command issued is:
sed -e "s/${search_string}/${replace_string}/g" $dir_file_backup > $dir_file
Any help is greatly aprreciated!
Kind Regards
Satnam
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
Hello everybody, I have a problem with creating a script which allows a single parameter to be passed. Sorry if I'm not expert, I'm new at this and .
The code is
if ; then
echo "Something"
exit
fi
...does other things...
grep -i $1 $TEMPUSERS > $USERSFILE
COUNT=`wc -l... (3 Replies)
Discussion started by: Gnappo
3 Replies
2. Shell Programming and Scripting
Hi!
I'm trying to write a regexp but I have no luck...
I have a string like this:
param1=sometext¶m2=hello¶m3=bye
Also, the string can be simply:
param2=hello
I want to return the value of param2: "hello".
How can I do this?
Thanks. (3 Replies)
Discussion started by: GagleKas
3 Replies
3. Shell Programming and Scripting
Hi All,
Below is a sample code:
print "Enter the Name: ";
my $Name = <>;
print "Word is $Name";
open (FH,"AIDNameList.txt");
while (<FH>)
{
my $line;
print "Word is $Name";
for $line(<FH>)... (12 Replies)
Discussion started by: jisha
12 Replies
4. Programming
Hi all,
How am I read a file, find the match regular expression and overwrite to the same files.
open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat";
open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat";
while (<DESTINATION_FILE>)
{
# print... (1 Reply)
Discussion started by: jessy83
1 Replies
5. Shell Programming and Scripting
Hi All,
I have a sftp session log where I am transferring multi files by issuing "mput abc*.dat". The contents of the logfile is below -
#################################################
Connecting to 10.75.112.194...
Changing to: /home/dasd9x/testing1
sftp> mput abc*.dat
Uploading... (7 Replies)
Discussion started by: k_bijitesh
7 Replies
6. UNIX for Dummies Questions & Answers
Hello experts,
I am exploring parameter expansion, and trying to cut the fields in a URL.
Following is the requirement:
I have
// abc.nnt /dir1/dir2/dir3/dir4/somefile.java
What i need to get is the path after dir3, and dir3 will be passed.
output that i need is... (1 Reply)
Discussion started by: gjarms
1 Replies
7. UNIX for Advanced & Expert Users
Hello All,
I'm trying to extract the lines between two consecutive elements of an array from a file.
My array looks like:
problem_arr=(PRS111 PRS213 PRS234)
j=0
while } ]
do
k=`expr $j + 1`
sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt
---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies
8. Shell Programming and Scripting
I am trying to test if I can replace a regular expression in 'nawk' with a variable.
Please let me know why it is not working. I am using ksh88i on solaris8
I am trying use this test as a building block to filter active external DNS connections.
Ideally I want to pass variable defined... (4 Replies)
Discussion started by: kchinnam
4 Replies
9. Shell Programming and Scripting
I am learning SED and just following the shell scripting book, i have trouble understanding the grep and sed statement,
Question : 1
__________
/opt/oracle/work/antony>cat teledir.txt
jai sharma 25853670
chanchal singhvi 9831545629
anil aggarwal 9830263298
shyam saksena 23217847
lalit... (7 Replies)
Discussion started by: Antony Ankrose
7 Replies
10. UNIX for Advanced & Expert Users
Hello,
i need to pass the variable in place of pwd. how to display variable in the bleow syntax.
suppose, passwd="test", then 'id/$passwd.....
FEDFlagResult=`sqlplus -S 'id/pwd@(DESCRIPTION= (ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1643))(CONNECT_DATA= (SID=peta1)))'<< EOF
select... (4 Replies)
Discussion started by: balareddy
4 Replies