How to search and replace a particular line in file with sed command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to search and replace a particular line in file with sed command
# 1  
Old 05-20-2009
Data How to search and replace a particular line in file with sed command

Hello,

I have a file and in that, I want to search for a aprticular word and then replace another word in the same line with something else.

Example: In file abc.txt, there is a line
<host oa_var="s_hostname">test</host>

I want to search with s_hostname text and then replace test with prod.

How do I achieve this with the sed or some other command.

Please help.

Thanks in advance.

Sam
# 2  
Old 05-20-2009
using perl to search and replace

Hope this helps, I created a file prodtest for example

Code:
$ perl -pe 'if ( $_ =~ /hostname/ ) { s/test/prod/ }' prodtest

st oa_var="s_hostname">prod</host>
<host oa_var="s_name">test</host>
<host oa_var="s_hostname">qa</host>
<host oa_var="s_hostname">prod</host>
<host oa_var="s_host">test</host>


$ cat prodtest

st oa_var="s_hostname">test</host>
<host oa_var="s_name">test</host>
<host oa_var="s_hostname">qa</host>
<host oa_var="s_hostname">test</host>
<host oa_var="s_host">test</host>


$ perl -i -pe 'if ( $_ =~ /hostname/ ) { s/test/prod/ }' prodtest


$ cat prodtest

st oa_var="s_hostname">prod</host>
<host oa_var="s_name">test</host>
<host oa_var="s_hostname">qa</host>
<host oa_var="s_hostname">prod</host>
<host oa_var="s_host">test</host>

# 3  
Old 05-30-2009
Hi Doutdes,

Thanks for your response and apologize for delayed reply.

In the meantime, I came up with following solution.

cat abc.txt | sed "/s_hostname/s/test/prod/g" > xyz.txt

Thanks,

Samir
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to search and replace string in column in file with command sed?

how to search and replace string in column in file with command sed or other search "INC0000003.in" and replace column 4 = "W" $ cat file.txt INC0000001.in|20150120|Y|N|N INC0000002.in|20150120|Y|N|N INC0000003.in|20150120|Y|N|N INC0000004.in|20150120|Y|N|Noutput... (4 Replies)
Discussion started by: ppmanja3
4 Replies

2. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

3. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

4. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

5. Shell Programming and Scripting

search and replace with sed command

hi, suggest me in the below script.. if In above I wanna replace "" with "]". (2 Replies)
Discussion started by: divya bandipotu
2 Replies

6. Shell Programming and Scripting

SED Question: Search and Replace start of line to matching pattern

Hi guys, got a problem here with sed on the command line. If i have a string as below: online xx:wer:xcv: sdf:/asdf/http:https-asdfd How can i match the pattern "http:" and replace the start of the string to the pattern with null? I tried the following but it doesn't work: ... (3 Replies)
Discussion started by: DrivesMeCrazy
3 Replies

7. Shell Programming and Scripting

Does Sed Search/Replace Work For Multiple Matches On The Same Line?

Hello, I would like to delete all the footnotes in all my htm files. Hence, I have to delete the whole font tag pairs, i.e. deleting everything between the begin/end font tags. I create a testfile, of which data parts of all four lines are the same except for the number of font tag pairs,... (3 Replies)
Discussion started by: cibalo
3 Replies

8. UNIX for Dummies Questions & Answers

SED command to search for numeric and replace

Hi I am very new to linux and scripting. I need to replace numbers abc with number xyz inputting from a reference file. I used the following command - sed "s/$grd/$lab/" , where $grd and $lab comes from reference file. The problem is the above line doesnt take care of space..... (1 Reply)
Discussion started by: ajayh
1 Replies

9. Shell Programming and Scripting

sed search and replace command

Hi, I would like to seek help on how i can arrive on this result. sample.txt: product_code IN (param001) and product_type IN (param004) product_code IN (param002) and product_type IN (param005) product_code IN (param003) and product_type IN (param006) I would like to change the param001... (1 Reply)
Discussion started by: janzper
1 Replies

10. Shell Programming and Scripting

sed search and replace in next line

Hello, I am hoping someone can provide some guidance on using context based search and replace to search for a pattern and then do a search and replace in the line that follows it. For example, I have a file that looks like this: <bold>bold text </italic> somecontent morecontent... (3 Replies)
Discussion started by: charissaf67
3 Replies
Login or Register to Ask a Question