Read file and replace a particular line if found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read file and replace a particular line if found
# 1  
Old 05-15-2013
Read file and replace a particular line if found

Hi All
There is another challenge which stand in front of me. And want all to have the experience with that

I have a file in Unix say a.txt. What I was trying is to read the file line by line and matching the line to particular pattern, and if that pattern found I want to replace that line with that particular pattern.

I know there are several ways to do, but I want to do it in that way only due to some large process constraints

Regards
Adi
# 2  
Old 05-15-2013
is your search pattern and replace pattern remain same?

I mean say you have "Hello World I am..." search pattern is Hello now if found you want that line to be replaced by just Hello? or something else?

Please clarify
# 3  
Old 05-15-2013
Sorry I miss that.
Suppose my file contain data is as follow
Code:
Hello I am great
My best website is Unix.com
Hello my name is adisky
Code tag button

and my search pattern will be the one of the line from file only
Code:
Code tag button

and if the pattern found it should replace with
Code:
Code tag button;Code tag button

Regards
ADI
# 4  
Old 05-15-2013
simple and straight fwd would be

Code:
 
sed 's/Code tag button/&;&/g'  filename

# 5  
Old 05-15-2013
and suppose If my line in a files is
Code:
A;B;C;D;E;F;

and my search pattern is
Code:
 A;B;C;D;E;F;

and if found it should replace it like
Code:
A;B;C;D;E;F;G;H;I;J;K

# 6  
Old 05-15-2013
hmmm it would be easy if you can provide exact file Smilie

Code:
 
sed 's/A;B;C;D;E;F;/&G;H;I;J;K/g' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace string2 by string3 where string1 is found in line

I found this in the forum that searches a file for string1, substitute all occurrences of string2 with string3. (Title: Replace string2 by string3 where string1 is found in line) >> sed -i '/string1/s/string2/string3/g' TextFile How will I perform the same sed command and only substitute... (3 Replies)
Discussion started by: apalex
3 Replies

2. Shell Programming and Scripting

Read each line in fileA, replace in all files with matching filename

Hello, I supposed that I asked the similar question but I could not have found it when I search on our forum. EXYU+: EXYU%3A+HRT+1 EXYU%3A+HRT+2 EXYU%3A+HRT+3 EXYU_url: #SERVICE 1:0:1:0:0:0:0:0:0:0:http%3A//xxxx%3Ayyyy@mmmm%3A55555/stream/channelname/YYYY?profile=htsp7777.ts #SERVICE... (2 Replies)
Discussion started by: baris35
2 Replies

3. Shell Programming and Scripting

Replace string2 by string3 where string1 is found in line

Hello, My aim is to search string1 in all lines. When found, find and replace string2 by string3 if possible. TextFile: Here is my first line Second line with string1 & string2 Not last line but it contains string1 Expected output: Here is my first line The second line with string1 &... (6 Replies)
Discussion started by: baris35
6 Replies

4. Shell Programming and Scripting

Read line by line and replace string.

Hi, I currently have a problem that I need to read a file line by line. After I read it line by line there are some commands in which I have to change a specific string.(In my case, I have to make a script that changes all the passwords into hash value) Here is a sample input... (3 Replies)
Discussion started by: thebennnn
3 Replies

5. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

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

6. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

7. UNIX for Dummies Questions & Answers

Replace line with found unknown pattern

Hi, I have a file with the following content: --------- a 3242 tc_5 gdfg4 random text a 3242 tc_6 gdfg4 random text a 3242 tc_7 gdfg4 random text a 3242 tc_4 gdfg4 --------- I want to replace the lines containing tc_? (tc_5, tc_6 etc. even with unknown numbers) with the found... (5 Replies)
Discussion started by: joas
5 Replies

8. Shell Programming and Scripting

Replace all found files with an empty file?

Hi all, I need to replace certain robots.txt files with an empty file. I use the following to find the files I want to empty out: find /Sites -type f -name "robots.txt" -exec grep -il "STAGE" {} \; (finds all robots.txt files which contain the string 'STAGE') Now what do I add to this... (3 Replies)
Discussion started by: Evert
3 Replies

9. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

10. Shell Programming and Scripting

How to replace a line below where the pattern found

Hi All, I have a file say abc.xml. In this file, I need to search for a pattern “SAP_GATEWAY_HOST”; if this pattern found and the next line also contain the pattern “nwprc03.cos” then I need to replace this pattern “nwprc03.cos” with some other pattern “nwdrc03.apjp”. $ cat abc.xml... (3 Replies)
Discussion started by: Ritesh.patni84
3 Replies
Login or Register to Ask a Question