Pattern Matching - serach and replace script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Pattern Matching - serach and replace script
# 1  
Old 04-07-2009
Pattern Matching - serach and replace script

My requirement is to replace a a particular pattren in a script from A to B.

I am not sure if this can be done through sed command or through awk .

The file sv.inc is
Code:
[+] window DialogBox  AddConnection
   [ ] tag "~ActiveApp/[DialogBox]Add Connection - Provider Type?URL"

I would wnat the file to be displaced as below after it matched the pattern
Code:
 [+] window DialogBox  AddConnection
   [ ] tag stAddConnection

If you note the pattern "~ActiveApp/[DialogBox]Add Connection - Provider Type?URL" has been replaced by stAddConnection

Please help

Last edited by Yogesh Sawant; 04-07-2009 at 05:42 AM.. Reason: added code tags
# 2  
Old 04-07-2009
using Perl:
Code:
perl -pi -e 's/(tag\s*)(.*)/\1 stAddConnection/' sv.inc

# 3  
Old 04-07-2009
If you want to try out using sed :
Code:
sed 's/\(tag\)\(.*\)/\1 stAddConnection/' sv.inc

rgds,

Srini

Last edited by Yogesh Sawant; 04-09-2009 at 04:39 AM.. Reason: added code tags
# 4  
Old 04-07-2009
I have the follwiing piece of code :

Code:
for i in `cat valFile`
do
   cat tag | while read j
        do
                echo $j
                echo $i
perl -pi -e 's/(j\s*)(.*)/\1 i/' sv.inc > test
       done
done


This does not work as i get a balank test file. while the script runs successfully.

Can someone help me please

Last edited by Yogesh Sawant; 04-09-2009 at 04:39 AM.. Reason: added code tags
# 5  
Old 04-07-2009
added code tags

Hi there,

A Correction. This will work.

Code:
for i in `cat valFile`
do
cat tag | while read j
do
echo $j
echo $i
cat sv.inc|perl -pi -e 's/('$j'\s*)(.*)/\1 '$i'/' > test
done
done


Last edited by Yogesh Sawant; 04-09-2009 at 04:41 AM.. Reason: Code updated.
# 6  
Old 04-07-2009
Thanks...

I get this error after runinng the script
Code:
Substitution pattern not terminated at -e line 1.


Last edited by Yogesh Sawant; 04-09-2009 at 04:38 AM.. Reason: added code tags
# 7  
Old 04-07-2009
Hi Sandeep,

BTW, it works fine for me though. Please see below:

-------------------------------------------------------------------------------
bash-3.00$ cat sample.pl
for i in `cat valFile`
do
cat tag | while read j
do
echo $j
echo $i
cat sv.inc|perl -pi -e 's/('$j'\s*)(.*)/\1 '$i'/' > test
done
done

bash-3.00$ cat valFile
stAddConnection
bash-3.00$ cat tag
tag
bash-3.00$ ./sample.pl
tag
stAddConnection
bash-3.00$ cat test
[+] window DialogBox AddConnection
[ ] tag stAddConnection
bash-3.00$ cat sv.inc
[+] window DialogBox AddConnection
[ ] tag "~ActiveApp/[DialogBox]Add Connection - Provider Type?URL"
bash-3.00$
-------------------------------------------------------------------------------


Perl Gurus,

could you please shed some light on this stated error during execution of a perl script ?

rgds,

Srini

Last edited by Srinivas_Hari; 04-07-2009 at 09:57 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace String matching wildcard pattern

Hi, I know how to replace a string with another in a file. But, i wish to replace the below string pattern EncryptedPassword="{gafgfa}]\asffafsf312a" i.e EncryptedPassword="<any random string>" To EncryptedPassword="" i.e remove the random password to a empty string. Can you... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Shell Programming and Scripting

Replace pattern matching

Can anyone help me with sed or awk to do a bulk replace of the below requirements. "REC_ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY ( START WITH +7486 INCREMENT BY +1 MINVALUE +7467 MAXVALUE... (6 Replies)
Discussion started by: ilugopal
6 Replies

3. Shell Programming and Scripting

sed - Exact pattern matching and replace

Hi Team, I am facing a problem as under, Suppose I have a file (test.txt) with the below content (all braces and slashes are included in the contents of the file) Now I want to append few words below matched line, I have written the below sed: sed '/option/a insert text here' test... (2 Replies)
Discussion started by: ankur328
2 Replies

4. Shell Programming and Scripting

Pattern matching and replace in shell script

Hi I want to find a line in a file which contains a word and replace the patterns. Sample file content temp.xml ==================== <applications> <application> Name="FirstService" location="http://my.website.selected/myfirstService/V1.0/myfirst.war" ... (1 Reply)
Discussion started by: sakthi.99it
1 Replies

5. Shell Programming and Scripting

Script to Serach pattern and give number of occurrences

Hi, I want a script which search for a pattern "good" in a huge file and provide me number of occurences of such pattern in a file. lets say i have a file test.txt contents as below good is good but good is sometime bad and sometime good you are very good and good is always good ... (7 Replies)
Discussion started by: sv0081493
7 Replies

6. Shell Programming and Scripting

sed to replace the matching pattern with equal number of spaces

Hi I have written a shell script which used sed code below sed -i 's/'"$Pattern"'/ /g' $FileName I want to count the length of Pattern and replace it with equal number of spaces in the FileName. I have used $(#pattern) to get the length but could not understand how to replace... (8 Replies)
Discussion started by: rakeshkumar
8 Replies

7. UNIX for Dummies Questions & Answers

Serach pattern in one field and replace in another

Hi all, I have a TAB separated file like this: sample.rpt: 54 67 common/bin/my/home {{bla bla bla}} {bla bla} Replace Me 89 75 bad/rainy/day/out {{ some bla} } {some bla} Dontreplace Me ...... ...... I wish to do a regexp match on the 3rd... (2 Replies)
Discussion started by: newboy
2 Replies

8. Shell Programming and Scripting

Need help to replace a perl pattern matching

My example file is as given below: conn=1 uid=oracle conn=2 uid=db2 conn=3 uid=oracle conn=4 uid=hash conn=5 uid=skher conn=6 uid=oracle conn=7 uid=mpalkar conn=8 uid=anarke conn=9 uid=oracle conn=1 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.10.5.6 to 10.18.6.5 conn=2... (4 Replies)
Discussion started by: sags007_99
4 Replies

9. UNIX for Dummies Questions & Answers

Serach a pattern

Hi, I am trying to find a particular patter in multiple UNIX files (also contain system files,hidden files and normal files) i am now using CMD: egrep -ali 'pattern' * i am not getting the required result, i just need files path and finename Naveen (3 Replies)
Discussion started by: Naveen_5960
3 Replies

10. Shell Programming and Scripting

serach and replace a specific pattern or value in a xml file

can some one help me with a perl command i have to search and replace a version from a xml-file so i use in a ksh script a command like this ssh $GLB_ACC@$GLB_HOST "/usr/contrib/bin/perl -pi -e "s/$curVersion/$new_Version/g" $Dest_dir/epi.xml" this command worked so far, but the problem... (1 Reply)
Discussion started by: kiranreddy1215
1 Replies
Login or Register to Ask a Question