Replace a particular pattern in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a particular pattern in a file
# 1  
Old 11-24-2010
Replace a particular pattern in a file

Hi ,
I have a requirement where i have this file

abcd
$$xyz=123
$$zef=789

$$mno=123
$$pqr=456

I need to replace $$pqr from 456 to a new value which will be present in a variable to me.
I am not aware of sed and awk functionality.
Can somebody please show me how to replace this 456 with a new value .
# 2  
Old 11-24-2010
perl -e -pi s/456/'$var'/g infile
# 3  
Old 11-24-2010
If pqr can have any numeric value you can use:

Code:
sed 's/\(^$$pqr=\)[0-9][0-9]*$/\1'"$VAR"'/' infile

# 4  
Old 11-24-2010
alternative in sed..
Code:
sed '/$$pqr/s/[0-9]\+/'$VAR'/' inputfile > outfile

# 5  
Old 11-24-2010
And another one with awk:
Code:
awk -v var=$variable -F= '/\$\$pqr/{$2=var}1' OFS=\= infile

# 6  
Old 11-24-2010
Code:
sed '/$$pqr=/'"s/=.*/=$var/" infile

Code:
sed "/\$\$pqr=/s/=.*/=$var/" infile

# 7  
Old 11-24-2010
Or using Perl -

Code:
$
$ cat f42
abcd
$$xyz=123
$$zef=789
 
$$mno=123
$$pqr=456
$
$
$ typeset -x VAR="999"
$
$ # Perl one-liner to substitute the value
$ perl -lne 'if (/^(\$\$pqr=).*/){print $1,$ENV{"VAR"}} else {print}' f42
abcd
$$xyz=123
$$zef=789
 
$$mno=123
$$pqr=999
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and Replace Pattern in file

Ok, so how many times have you received this request? I have been looking through the forum for examples and I see the use of tr, awk and sed to perform similar functions but not sure how to use the tools in this scenario and could use a push in the right direction. GOAL: Search for line... (9 Replies)
Discussion started by: djzah
9 Replies

2. Shell Programming and Scripting

Replace pattern from nth field from a file

$ cat /cygdrive/d/Final2.txt 1,A ,Completed, 07.03_23.01 ,Jun 30 20:00 2,BBB,Pending,, 3,CCCCC,Pending,, 4,DDDDD,Pending,, 5,E,Pending,, 6,FFFF,Pending,, 7,G,Pending,, In the above file 4th field is date which is in MM.DD_HH.MIN format and I need to convert it to as it is there in 5th... (1 Reply)
Discussion started by: Amit Joshi
1 Replies

3. Shell Programming and Scripting

Pattern replace from a text file using sed

I have a sample text format as given below <Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text> <Text Text_ID="10155645315851111_10155645317023456" From="1626711840902323"... (3 Replies)
Discussion started by: my_Perl
3 Replies

4. Shell Programming and Scripting

Read from one file-Replace a pattern in another with the current one

Hi Friends, I have a text file like this cat main.txt I like this website cat website > new_website grep website > hello_website cat replace.txt hello unix apple Now, for each line read in 2.txt, I want the pattern "website" in 1.txt to be replaced with it. Basically, I... (9 Replies)
Discussion started by: jacobs.smith
9 Replies

5. Shell Programming and Scripting

script to grep a pattern from file compare contents with another file and replace

Hi All, Need help on this I have 2 files one file file1 which has several entries as : define service{ hostgroup_name !host1,!host5,!host6,.* service_description check_nrpe } define service{ hostgroup_name !host2,!host4,!host6,.* service_description check_opt } another... (2 Replies)
Discussion started by: namitai
2 Replies

6. UNIX for Dummies Questions & Answers

search a pattern across all file and replace it to all files.

Hi All, HP-UX oradev3 B.11.11 U 9000/800 I have few files, which contain texts. Now I need to search a pattern across all files and replace to that pattern with new one. For example, I have following files they have, which contain something like abc@abc.com, now I need to search text acorss... (6 Replies)
Discussion started by: alok.behria
6 Replies

7. Shell Programming and Scripting

how to find the pattern inside the file and replace it

hello everybody, I have a group of file eg- sample1 sample2 sample3 sample4 each file contain this :- cat sample1 SEQ_NUM,1,UPESI1 My requirement is to change the value-UPESI1 to UPE10 in file which contain this pattern -UPESI1. any help is appreciated. (2 Replies)
Discussion started by: abhigrkist
2 Replies

8. Shell Programming and Scripting

grep a pattern and replace a value in it and write to the same file.

I have some complication with this, I have a file like below for DEV_1 till DEV_10. and the db values are set accordinly which are not unique. For example DEV1,DEV4,DEV6 can have the same target DB name. I waned to identify for DEV_2 and then replace the TARGET_DATABASE value with the new DB... (6 Replies)
Discussion started by: yesmani
6 Replies

9. Shell Programming and Scripting

replace a column in a file if it matches certain pattern

Hi, I want to replace a column in a file if it matches certain pattern. Can you help me on this. Here is the file content. 000000 1111111 2222222 011111 0123445 1234556 023445 1111111 2343455 if second column contains 1111111 i need to replace it with 0000000 Can you... (6 Replies)
Discussion started by: Krrishv
6 Replies

10. Shell Programming and Scripting

find and replace a pattern in a file

Hi I am having 2 files file1.c and file2.c Now i want to find all the occurances of pattern "abc" in file1.c, file2.c and replace with pattern "def" using shell script without using sed and with using sed. Thanks in advance... raju (1 Reply)
Discussion started by: krishnamaraju
1 Replies
Login or Register to Ask a Question