SED multiple find and replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED multiple find and replace
# 1  
Old 11-25-2011
SED multiple find and replace

Hi,

searched through the forums and not really found what I am looking for. I am a bit of novice when it comes to anything above basic scripting and not even that when it comes to the sed command.

I have been reading the tutorials online but still struggling to get what I need Smilie


I have a script that has the following output (a small example of the full file):

example.txt
Code:
 
092F 1639163_Cluster01_Prod
0522 1639163_Cluster02_Prod
08C2 1639163_Cluster07_Prod
0957 1639163_Cluster07_Prod
1762 1024477_Cluster07_Prod
1767 1024477_Cluster07_Prod
176C 1024477_Cluster07_Prod
1771 1024477_Cluster07_Prod
1776 1024477_Cluster07_Prod
1EDC 1639163_Cluster07_Prod
1EE4 1639163_Cluster07_Prod
07F0 2048953_Cluster11_Prod
0807 1228838_Cluster11_Prod
175C 1229372_Cluster11_Prod
1B68 1024477_Cluster11_Prod
1E87 1639163_Cluster11_Prod
1D2B 1010831_SRV93012
1D6A 1045688 SRV93013
143E  34856 RS6PUNI01_Cluster
143F  34856 RS6PUNI01_Cluster
1440  34856 RS6PUNI01_Cluster
1441  34856 RS6PUNI01_Cluster
1442  34856 RS6PUNI01_Cluster
1443  34856 RS6PUNI01_Cluster

What I want to do is replace the "_" with a space between the numbers and the server names e.g.
from:
1639163_Cluster11_Prodto:
1639163 Cluster11_Prod


now I have been able to do this by using the command:

sed 's/1639163_/1639163 /g' example.txt

I am not sure if there is a better way of doing this but if there is not how do I do a multiple search and replace for the other sized devices within the one command - e.g.:
sed 's/2048953_/2048953 /g' example.txt
sed 's/1228838_/1228838 /g' example.txt
sed 's/1024477_/1024477 /g' example.txt

etc, etc.

I also want the changes to update the example.txt file - is this possible?

Thanks for your help.

Col
# 2  
Old 11-25-2011
If the field to be changed is always the second one (numbered as whitespace-separated) then you could do:
Code:
awk '{ sub ("_", " ", $2); print}' infile

Alternatively, if your server names have a defined format like Cluster<NN> you could use:
Code:
sed 's/_\(Cluster[0-9][0-9]*\)/ \1/' infile

This User Gave Thanks to CarloM For This Post:
# 3  
Old 11-25-2011
sed is a good choice:
Code:
sed -e 's/ \([0-9][0-9]*\)_/ \1 /'

However, sed does not allow you to edit in place, you will have to do something like:
Code:
sed .... oldfile > newfile
mv newfile oldfile

If I were writing the script, I would do:
Code:
#! /bin/ksh

for file in "${@}"; do
    temp=$(mktemp --tmpdir=$(dirname ${file}))
    sed -e 's/..../..../' ${file} > ${temp}
    mv ${temp} ${file}
done

Now using mktemp may be a bit of overkill, but it guarantees that you are not overwritting an existing file. And the --tmpdir option ensures the mv does not have to copy the file between filesystems.
# 4  
Old 11-25-2011
You use -i option to edit the file directly using sed.

man sed
Quote:
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied)
--ahamed
# 5  
Old 11-25-2011
Quote:
Originally Posted by m.d.ludwig
However, sed does not allow you to edit in place
GNU sed does - -i option.
This User Gave Thanks to CarloM For This Post:
# 6  
Old 11-25-2011
Quote:
Originally Posted by CarloM
If the field to be changed is always the second one (numbered as whitespace-separated) then you could do:
Code:
awk '{ sub ("_", " ", $2); print}' infile

Alternatively, if your server names have a defined format like Cluster<NN> you could use:
Code:
sed 's/_\(Cluster[0-9][0-9]*\)/ \1/' infile

Carlo apologies for being a bit supid here but what does the infile but at the end do - not sure I understand that?

Unfortunately I also have servers called - SRV{followed by 5 numbers} - so maybe not as straight-forward as I was hoping.
# 7  
Old 11-25-2011
infile is just your input file, whatever it's called.
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 and replace from multiple files

Hello everybody, I need your help. I have a php site that was expoited, the hacker has injected into many php files a phishing code that was discovered and removed in order to have again a clean code. Now we need to remove from many php files that malware. I need to create a script that find and... (2 Replies)
Discussion started by: ninocap
2 Replies

2. Shell Programming and Scripting

Find and replace in multiple files

Hi, I have php files in main dir and sub dir's as well. I need to find "new mysqli('localhost', 'System', 'xxxxxx', 'System', '3306');" and replace as "new mysqli('localhost', 'unx_sys', 'yyyy', 'unx_sys', '3306');" I tried like: sed 's/new mysqli\(*\)\;$/new... (1 Reply)
Discussion started by: ashokvpp
1 Replies

3. Shell Programming and Scripting

find and replace - multiple combinations

Hello all, I'm trying to replace all possible combinations of specific character occurrences within a string with another character, that is: For the following string: ABCAACD I want to replace all the possible combinations of the character 'A' with the character 'G', so the output... (3 Replies)
Discussion started by: hagit
3 Replies

4. Shell Programming and Scripting

Replace multiple lines through sed

Hi All, I have a input file as sample below <this is not starting of file> record line1 line2 line3 end line4 line5 record line6 line7 line8 my requirement is this, i want to select a pattern between first record and end, whatever is written between first record and end. and... (0 Replies)
Discussion started by: adgangwar
0 Replies

5. Shell Programming and Scripting

using sed to find and replace multiple numbers

I have looked around and there are several examples of how to use sed, but I don't think any of them help me very much with what I am trying to do. I have a text file like this.... 1! SRCNAM = 00001 ! 1! X = 50.0000, 0.0000,... (10 Replies)
Discussion started by: mercury.int
10 Replies

6. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

7. Shell Programming and Scripting

Find and replace multiple lines

I have a section of text in file A, see below # falkdjf lkjadf lkjadf lkajdf lkajdf lkajdf lkjadf lkjadf 234.234.2.234 lkjlkjlk 234.234.3.234 # Only the first line with "# falkdjf lkjadf lkjadf" is unique in the file. The new section that I want to overwrite the old section above is in... (1 Reply)
Discussion started by: jyang72211
1 Replies

8. Shell Programming and Scripting

sed multiple replace

Hello! I'm using sed to perform a lots of replaces in one text file. I call it this way: sed -f commands.txt in.txt > out.txt commands.txt has about 1000 lines, each one is some variation of: s/from/to/gI And in.txt has about 300 000 lines. So the problem is that operation takes about... (7 Replies)
Discussion started by: backdrift
7 Replies

9. Shell Programming and Scripting

Help with find and Replace using sed

I have to update a paramater (dateMemLimit) present in a file, with a date (YYYYMMDD) equal to 5 days before the sysdate. The parameter will be in the following format. dateMemLimit = 20091201 Please note the blank spaces present between 'dateMemLimit' &'=' and between '='... (4 Replies)
Discussion started by: rajesh8s
4 Replies

10. Shell Programming and Scripting

sed find and replace multiple lines

I am new to linux and would like to modify the contents of a file preferably using a one line. The situation is as follows <start> some lines "I am the string" "replace string" more lines here <end> In the above example,On encountering "I am the string", the "replace string "should be... (6 Replies)
Discussion started by: supersimha
6 Replies
Login or Register to Ask a Question