Using sed to search and replace data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using sed to search and replace data
# 1  
Old 06-12-2011
Lightbulb Using sed to search and replace data

Hi,
Kindly need your expertise in this problem.
I have to search and replace data. The problem is, the data is in the same format but slightly different content. What I need is sed commands that can work for those "slightly different content".

input:
Code:
BGM+UNIV:1+CTRLSRGF-ARIV+STS+110327:1701+20110327170022'UNH.....'BGM+UNIV:4+EZHPIP:ZZZ+APL:ZZZ+110207:0430+20110207043010'UNH..

and
Code:
....'KMV+123:123456789:203'DKCSGSIN:139:6+TER.....'KMV+1:123456789:204'.....

output:
Code:
BGM+UNIV:1+CTRLSRGF-ARIV+STS+110327:1701+''''''''170022'UNH.....'BGM+UNIV:4+EZHPIP:ZZZ+APL:ZZZ+110207:0430+''''''''043010'UNH..

and
Code:
....'KMV+123:''''''''':203'DKCSGSIN:139:6+TER.....'KMV+1:''''''''':204'.....

Regardless of "'KMV+123:" or "'KMV+1:"(there might be others like "'KMV+25", "'KMV+7'"...), 123456789 will be replaced with '''''''''
Also, regardless of "BGM+UNIV:1+CTRLSRGF-ARIV+STS+110327:1701+20110327170022'" or "'BGM+UNIV:4+EZHPIP:ZZZ+APL:ZZZ+110207:0430+20110207043010'", the text in blue will be replaced.
Thanks for any idead.
# 2  
Old 06-13-2011
Code:
% echo "BGM+UNIV:1+CTRLSRGF-ARIV+STS+110327:1701+20110327170022'UNH.....'BGM+UNIV:4+EZHPIP:ZZZ+APL:ZZZ+110207:0430+20110207043010'UNH.." \
| sed  "s/\(:[0-9]\{4\}+\)[0-9]\{8\}/\1'''''''''/g"
BGM+UNIV:1+CTRLSRGF-ARIV+STS+110327:1701+'''''''''170022'UNH.....'BGM+UNIV:4+EZHPIP:ZZZ+APL:ZZZ+110207:0430+'''''''''043010'UNH..

Code:
% echo "....'KMV+123:123456789:203'DKCSGSIN:139:6+TER.....'KMV+1:123456789:204'....." \
| sed "s/\(KMV+[^:]*:\)[0-9]\{9\}/\1'''''''''/g"
....'KMV+123:''''''''':203'DKCSGSIN:139:6+TER.....'KMV+1:''''''''':204'.....

# 3  
Old 06-13-2011
Can you explain what is meant by
Code:
sed  "s/\(:[0-9]\{4\}+\)[0-9]\{8\}/\1'''''''''/g

?
Thanks.
# 4  
Old 06-13-2011
Sorry my English is not very good. But there are a lot of good tutorials about regular expressions in the Internet.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search and replace data

FILENAME: (Orig File) lab computer11node1 { hardware ok 19:50:56:bd:03:c2; MAC-ADDRESS 10.1.1.1; } lab computer11node2 { hardware ok 19:50:56:bd:03:c3; MAC-ADDRESS 10.1.1.2; } FILENAME2: computer11node1 11:50:56:81:33:c5 computer11node2 11:50:56:81:33:c6 computer11node3 11:50:56:81:33:c7... (5 Replies)
Discussion started by: kenshinhimura
5 Replies

2. Shell Programming and Scripting

Need help with search and replace using SED

Hi guys, thanks for accepting me in your forum .. I am trying to clean some hacked PHP files using SSH .. I am using this command: find . -type f -print0 | xargs -0 sed -i '/god_mod/d' <?php ... (3 Replies)
Discussion started by: wisam74us
3 Replies

3. UNIX for Dummies Questions & Answers

search and replace with sed

Hi All I have a simple text file and I want to be able to replace any alpha character and comma combination with any other symbol of my choice here is the text file I want to replace: pear apple ban,ana grape ",g1234," te,st1 here is how it should look afterwards: pear... (4 Replies)
Discussion started by: greg_b
4 Replies

4. Shell Programming and Scripting

Sed - search and replace help.

Hi everyone, basically I am been cleaning data by using simple sed commands So what i have below has been working for me. sed 's/="//g' trade.csv > tradeb.csv sed 's/"//g' tradeb.csv > trade2.csv but now i don't want to remove all the quotes just the ones if i encounter this ... (1 Reply)
Discussion started by: raz0r
1 Replies

5. Shell Programming and Scripting

awk/sed to search & replace data in first column

Hi All, I need help in manipulating the data in first column in a file. The sample data looks like below, Mon Jul 18 00:32:52 EDT 2011,NULL,UAT Jul 19 2011,NULL,UAT 1] All field in the file are separated by "," 2] File is having weekly data extracted from database 3] For eg.... (8 Replies)
Discussion started by: gr8_usk
8 Replies

6. UNIX for Dummies Questions & Answers

How to use 'sed' to search and replace?

Hello - I have a very large file in which a certain numbers are repeated. I find that using vi to edit the entire file is useless. How should i use sed to find a replace such as this text: To replace: 145.D25.D558 With: 215.22.45.DW I tried this command: sed... (4 Replies)
Discussion started by: DallasT
4 Replies

7. Shell Programming and Scripting

search and replace with data from another file

Can someone help me in solving the problem below. I have the following two files template_file ------------ ...other data.. ...other data.. FILE_NAME= ...other data.. ...other data.. list_file ---------- <file_name1> <file_name2> <file_name3> I need to produce another output... (3 Replies)
Discussion started by: paruthiveeran
3 Replies

8. Shell Programming and Scripting

sed search and replace

hi, im new for sed, anyone can help me to these in sed command my output file.txt "aaa",a1,bbb "ddd",a1,ccc "eee",a1,www need to change a1, to "a1"," output i need "aaa","a1","bbb "ddd","a1","ccc "eee","a1","www thanks in advance fsp (2 Replies)
Discussion started by: fspalero
2 Replies

9. UNIX for Dummies Questions & Answers

Search/Replace with Sed

Is there a way to use the sed command to 1) search a specified pattern 2) in the line where that pattern is found, replace from character N to character N+4 with a new 4-character string. Thks in advance! (5 Replies)
Discussion started by: mvalonso
5 Replies

10. Shell Programming and Scripting

Search and replace sed or tr

Hi folks, I need to search and replace specific text in a file and replace it. I have a text file that does not have any newlines or carriage returns. All newlines have been removed. Here is what I need to do. Find the exact string “DH” (quotes included) and replace it with \n”DH” (basically... (6 Replies)
Discussion started by: bridgeje
6 Replies
Login or Register to Ask a Question