using sed to replace a pattern in list of files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using sed to replace a pattern in list of files
# 1  
Old 08-07-2008
using sed to replace a pattern in list of files

Hi All,

using the below grep command,I get the following output:

$grep -irl "bc" /home/applmgr/amit > file_list.log
$cat file_list.log
/home/applmgr/amit/xyz.log
/home/applmgr/amit/abc.log

Requirement
=========

Need sed utility to replace "bc" with "xy" pattern in the list of files i.e., file_list.log

I am not getting the output while doing this way:

sed -in '1,$s/bc01/xy01/g' file_list.log

could anyone please help correct this..would appreciate the help..

Thanks for your time!

Regards,
# 2  
Old 08-07-2008
Your sample input and output are not coherent. Your sed expression cointains "bc01" even though none of the sample data contains "bc01". Assuming the "01" is an accident or something, the following will do what you describe, i.e. replace "bc" with "xy".

Code:
sed -i 's/bc/xy/g' file_list.log

The -i switch causes sed to perform the replacement in the file, i.e. replacing the file's contents with the new contents. If you have already run the failed command, it will have replaced the file with probably nothing at all, so you will need to run the grep again before you try the new script.
# 3  
Old 08-07-2008
using sed to replace a pattern in list of files

Hi era,

Thanks for the help but it still doesn't work..

Files below consists of "bc01"..

/home/applmgr/amit/xyz.log
/home/applmgr/amit/abc.log

If i use sed this way, it works...

$ sed -i 's/bc/xy/g' xyz.log abc.log

output:
=====

A) cat xyz.log
xy01
6000

B) cat abc.log
xy01
6000
6001


My requirement is :

sed -in '1,$s/bc01/xy01/g' file_list.log --should work for multiple files in file_list.log

where ..

$cat file_list.log
/home/applmgr/amit/xyz.log
/home/applmgr/amit/abc.log

anyone who could suggest the correct way..

Regards,
# 4  
Old 08-07-2008
Code:
xargs sed -i 's/bc01/xy01/g' <file_list.log

The -n option causes sed to remove any line which does not match; I'm guessing you don't want that. If you do, you'll need to add a /p option to the s/// command to print the lines which had substitutions made to them; otherwise, the output will be empty files (it will make the substitution but not write the result anywhere).
# 5  
Old 08-07-2008
thanks era now it works..

take care,

regards,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find pattern and replace using sed

Hi, i want to replace the following lines in such a way that if the word merge exists in first column it must replace the 3rd column as M and if parse exists in first column then the last column must P, if neither it must mark it as X. I have tried the solution using awk, but it is saying... (6 Replies)
Discussion started by: charlie87
6 Replies

2. UNIX for Beginners Questions & Answers

sed replace pattern

I have a file with multiple lines, all in the same format. For each line, I need to replace the sequence of digits after the last : with a new value, but keep the single quote at the end of the line. Example: Input: ( two lines of file) Name: 'text1:200/text2:1.2.3.4' Name2:... (19 Replies)
Discussion started by: Beginner101
19 Replies

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

4. Shell Programming and Scripting

sed find/replace a pattern, but not this one..

I've got a file like so: ...lots of lines, etc. push "route 10.8.0.0 255.255.255.0" push "route 192.168.1.123 255.255.255.0" ...lots of lines, etc. I want to sed find/replace the IP address in the second line, whatever it is, with a new IP address, but I don't want to touch the first line.... (5 Replies)
Discussion started by: DaHai
5 Replies

5. Shell Programming and Scripting

sed command to replace two character pattern with another pattern

Not able to paste my content. Please see the attachment :-( (2 Replies)
Discussion started by: vivek d r
2 Replies

6. Shell Programming and Scripting

Replace everything but pattern in a line using sed

I have a file with multiple lines like this: <junk><PATTERN><junk><PATTERN><junk> <junk><PATTERN><junk><PATTERN><junk><PATTERN><junk> Note that 1. There might be variable number occurrences of PATTERN in a line. 2. <> are just placeholders, they do not form part of the pattern. I need... (4 Replies)
Discussion started by: flatley
4 Replies

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

8. Shell Programming and Scripting

How to replace the last pattern using sed?

myfile: AAAaaa BBBbbb CCCccc AAAeee DDDddd how to replace the last AAA as EEEEE using sed? like this: AAAaaa BBBbbb CCCccc EEEEEeee DDDddd (14 Replies)
Discussion started by: vistastar
14 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

Find a pattern and replace using sed.

Hi I need to help on finding the below pattern using sed <b><a href="/home/document.do?assetkey=x-y-abcde-1&searchclause=photo"> and replace as below in the same line on the index file. <b><a href="/abcde.html"> thx in advance. Mari (5 Replies)
Discussion started by: maridhasan
5 Replies
Login or Register to Ask a Question