Search for a pattern and replace. Help needed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for a pattern and replace. Help needed
# 1  
Old 11-02-2011
Search for a pattern and replace. Help needed

I have three variables $a, $b and $c
Code:
$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 done.

Thanks in advance

Moderator's Comments:
Mod Comment Please use code tags <- click the link!

Last edited by zaxxon; 11-02-2011 at 06:24 AM.. Reason: code tags
# 2  
Old 11-02-2011
Which shell or scripting language do you use and what have you tried so far?
# 3  
Old 11-02-2011
Data

perl. I tried using pattern matching.

---------- Post updated at 04:44 AM ---------- Previous update was at 04:26 AM ----------

can you give an idea on this plz

---------- Post updated at 04:44 AM ---------- Previous update was at 04:44 AM ----------

can you give an idea on this plz

---------- Post updated at 04:58 AM ---------- Previous update was at 04:44 AM ----------

Smilie
# 4  
Old 11-02-2011
Hi irudayaraj,

Try:
Code:
$ cat script.pl
use warnings;                                                                                                                                                                                                                                
use strict;                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                             
my $a = "file_abc_123.txt";                                                                                                                                                                                                                  
my $b = 123;                                                                                                                                                                                                                                 
my $c = 100;                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                             
if ( $a =~ m/$b/ ) {                                                                                                                                                                                                                         
        $a =~ s/$b/$c/;                                                                                                                                                                                                                      
}                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                             
printf "%s\n", $a;                                                                                                                                                                                                                           
$ perl script.pl                                                                                                                                                                                                           
file_abc_100.txt

Regards,
Birei
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search pattern then find and replace

If condition satisfy, want to find pattern and modify two Fields in Modify.txt Input.txt SOURCE1 SOURCE2 SOURCE3 SOURCE4 SOURCE5 SOURCE6 Modify.txt SOURCE1|SLA|2016/12/11 11:12:11 PM|HMM|11-11-16| SOURCE2|SLA|2016/13/11 11:12:11 PM|HMM|10-11-16| SOURCE3|SLA|2016/14/11 11:12:11... (7 Replies)
Discussion started by: Joselouis
7 Replies

2. Shell Programming and Scripting

sed replace after pattern help needed

Hello, I have a file with multiple lines like this: /film/4295/"_class="titre_article">50/50I would like to change all occurence of / after > with _ to have this: /film/4295/"_class="titre_article">50_50Thank you edit: This could also be change all / starting with the 4th occurrence... (2 Replies)
Discussion started by: patx
2 Replies

3. Shell Programming and Scripting

search & replace pattern

Hi, My problem is that I have to search a changing pattern and replace it with the wild card char "*" i/p: 99_*_YYYYMMDD_SRC.txt.tar.gz o/p: 99_*_*_SRC.txt.tar.gz The problem is that YYYYMMDD pattern is not static. It could be YYYYMMDDHHMI or could be YYYYMMDDHHMISS. Can... (10 Replies)
Discussion started by: dips_ag
10 Replies

4. Shell Programming and Scripting

Help needed :Search and Replace a string pattern with empty in an xml file in unix

Search and Replace a string pattern with empty in an xml file in unix: My xml file would be like this : <Accounts><Name>Harish</Name><mobile>90844444444444445999 </mobile><TRIG>srcujim-1</TRIG></Accounts><Accounts><Name>Satish</Name><mobile>908999</mobile><TRIG>ettertrtt-1</TRIG></Accounts> ... (1 Reply)
Discussion started by: harish_s_ampeo
1 Replies

5. Shell Programming and Scripting

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 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... (4 Replies)
Discussion started by: nitindreamz
4 Replies

6. Shell Programming and Scripting

Complex Search/Replace Multiple Files Script Needed

I have a rather complicated search and replace I need to do among several dozen files and over a hundred occurrences. My site is written in PHP and throughout the old code, you will find things like die("Operation Aborted due to....."); For my new design skins for the site, I need to get... (2 Replies)
Discussion started by: UCCCC
2 Replies

7. UNIX for Advanced & Expert Users

search a replace each line- help needed ASAP

can someone help me with the find and replace command. I have a input file which is in the below format: 0011200ALN00000000009EGYPT 000000000000199900000 0011200ALN00000000009EGYPT 000000000000199900000 0011200ALN00000000008EGYPT 000000000000199800000 0011200ALN00000000009EGYPT ... (20 Replies)
Discussion started by: bsandeep_80
20 Replies

8. 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

9. 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
Login or Register to Ask a Question