perl:: search for tow pattern and replace the third pattern


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
perl:: search for tow pattern and replace the third pattern

Hi

i want to search two pattern on same line and replace onther pattern..

INPut file
Code:
aaaa bbbbb nnnnnn ttttt
cccc bbbbb nnnnnn ppppp
dddd ccccc nnnnnn ttttt
ffff bbbbb oooooo ttttt

now i want replace this matrix like.. i want search for "bbbbb","nnnnnn" and search and replace for "ttttt" to--->ppppp
i dont want like
Code:
"perl -i.bkup -p -e 's/ttttt/ppppp/ if /bbbbb nnnnnn/' filename"

(pls sugest in that i can use to if condition in same and how)
output file
Code:
aaaa bbbbb nnnnnn ppppp
cccc bbbbb nnnnnn ppppp
dddd ccccc nnnnnn ttttt
ffff bbbbb oooooo ttttt


Thanks in advace .. pls help me

Last edited by radoulov; 11-27-2009 at 04:38 PM.. Reason: Please use code tags.
# 2  
Quote:
Originally Posted by nitindreamz
... i want search for "bbbbb","nnnnnn" and search and replace for "ttttt" to--->ppppp
i dont want like
"perl -i.bkup -p -e 's/ttttt/ppppp/ if /bbbbb nnnnnn/' filename"
...
You want to search for "bbbbb nnnnnn", but you don't want to use an if condition. How else can you search for that pattern ?

tyler_durden
# 3  
Hi
actuly i want to use if condition but for two string seach pattern

"perl -i.bkup -p -e 's/ttttt/ppppp/ if /bbbbb && /nnnnnn/' filename"

i want to search the both like if /bbbbb && /nnnnnn string separately(but it is not working Smilie) .. and want to replace ttttt with ppppp


thanks for reply Smilie

Thanks
nitin
# 4  
Quote:
Originally Posted by nitindreamz
...
i want to search the both like if /bbbbb && /nnnnnn string separately
...and want to replace ttttt with ppppp
...
Code:
$
$ cat -n f3
     1  aaaa bbbbb nnnnnn ttttt
     2  xxxx nnnnnn bbbbb ttttt
     3  cccc bbbbb nnnnnn ppppp
     4  dddd ccccc nnnnnn ttttt
     5  ffff bbbbb oooooo ttttt
$
$ perl -i.bkup -pe 's/ttttt/ppppp/ if /bbbbb/ && /nnnnnn/' f3
$
$ cat -n f3
     1  aaaa bbbbb nnnnnn ppppp
     2  xxxx nnnnnn bbbbb ppppp
     3  cccc bbbbb nnnnnn ppppp
     4  dddd ccccc nnnnnn ttttt
     5  ffff bbbbb oooooo ttttt
$
$

Note that since you are not checking for the order of those two strings, a line that has "nnnnnn" to the left of "bbbbb" will also be affected (line # 2 for example).

tyler_durden
# 5  
MySQL

Thank you .. its working.. Smilie
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #460
Difficulty: Medium
The 64-bit timestamps used by NTP consist of a 32-bit part for seconds and a 32-bit part for fractional second.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

sed - Search and replace within pattern

Hi Guys! Unix newbie here! Have a requirement for which I have been scouting the forums for a solution but has been out of luck so far :( I have a file which contains the following:- TEST1|TEST2|"TEST3|1@!2"|TEST5 My sed command should result in either one the following output:-... (6 Replies)
Discussion started by: hishamzz
6 Replies

3. Shell Programming and Scripting

Search for a pattern and replace. Help needed

I have three variables $a, $b and $c $a = file_abc_123.txt $b = 123 $c = 100 I want to search if $b is present in $a. If it is present, then i want to replace that portion by $c. Here $b = 123 is present in "file_abc_123.txt", so i need the output as "file_abc_100.txt' How can this be... (3 Replies)
Discussion started by: irudayaraj
3 Replies

4. Shell Programming and Scripting

Search and replace - pattern-based

Hey folks! I am new to shell-scripting, but I have a problem that I would like to solve using a script. I create very large html forms, used for randomized trials. In these forms, each question is supplied with a variable that looks something like this: PROJECT_formNN Where NN is the question... (1 Reply)
Discussion started by: Roevhat
1 Replies

5. UNIX for Dummies Questions & Answers

pattern containg ' search and replace

Hi guys I'm new to this forum so please help me in this I have a file where i need to replace a pattern value=' ' with the pattern value='abc' and moreover that abc value must be passed from some variable say i assign name=abc and use name as the value to replace instead of the direct string... (10 Replies)
Discussion started by: sundarj
10 Replies

6. Shell Programming and Scripting

search a pattern and replace the whole line

Hi All, I have a requirement where I have to find a pattern in a file and comment the whole line containing the search pattern. Any ideas in shell is welcome. Thanks in advance, Regards, Arun (3 Replies)
Discussion started by: arun_maffy
3 Replies

7. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

8. Shell Programming and Scripting

search replace pattern containing slashes

Need help in finding pattern then replacing pattern that contains multiple slashes .. ex . <imgp src="Attention_web.eps.jpg" align="left"> <imgp src="NewToday062308.eps.jpg"> replace with <imgp src="/ww2/adpay/liner/Attention_web.eps.jpg" align="left"> <imgp... (2 Replies)
Discussion started by: aveitas
2 Replies

9. Shell Programming and Scripting

SED Search Pattern and Replace with the Pattern

Hello All, I have a string "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031", and I just want to extract LLSV1, but I dont get the expected result when using the sed command below. # echo "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031" | awk '{print... (4 Replies)
Discussion started by: racbern
4 Replies

10. Shell Programming and Scripting

Search for a Pattern and Replace

Hello All, Can you help me with this , I need to search a pattern replace it with the new pattern in all the files in a directory. what would be the easiest way to do that? Thanks in advance. :) Sam, (6 Replies)
Discussion started by: sbasetty
6 Replies

Featured Tech Videos