How to replace multiple numbers?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace multiple numbers?
# 1  
Old 12-30-2010
How to replace multiple numbers?

hello everyone
i searched the net and i could not find script for this request.
i believe sed command will do it but i'm not sure about how.

my file contains thousands of records, the following is sample:
Code:
BEGIN
ASX15001
BEGIN
ASX15000000500020101230
ASX18001020070002010123
ASX16001000050002010123
ASX19001000060002010675
ASX18001020070052010034
ASX18001030040002010768
ASX19001000120002010123
ASX14001000050002010754
ASX19001000070022010123
ASX18002000120002010876
ASX18001000040002010123
END

what i need is to
1- search for 13 digits number after ASX and that number starts with 18 or 19 and contains 07 or 12 at digit 9 and 10 (red and blue numbers meet this requirement)
2- replace the digits 9 to 0 and digit 10 to 0

the output should be
Code:
BEGIN
ASX15001000050002010123
ASX18001020000002010123
ASX16001000050002010123
ASX19001000060002010675
ASX18001020000052010034
ASX18001030040002010768
ASX19001000000002010123
ASX14001000050002010754
ASX19001000000022010123
ASX18002000000002010876
ASX18001000040002010123
END

how to do it?
thank you


Moderator's Comments:
Mod Comment Please use code tags when posting data and code samples, thank you.

Last edited by Franklin52; 12-30-2010 at 02:36 PM..
# 2  
Old 12-30-2010
Code:
sed 's/^\(ASX1[89]......\)07\(.*\)/\100\2/;s/^\(ASX1[89]......\)12\(.*\)/\100\2/' inputFile


Last edited by anurag.singh; 12-30-2010 at 02:43 PM..
This User Gave Thanks to anurag.singh For This Post:
# 3  
Old 12-30-2010
thanks alot anurag

is there a reference or book to master sed easily .. i have to process many many files and i use sed alot..thanks again
# 4  
Old 12-30-2010
Not sure how easy it will be to master. I would say, the more you practice, the more you will learn. There are lots of stuff on internet. To solve complex sed problems, regular expression also should be learned well.
The Grymoire - home for UNIX wizards
Sed Tips and Tricks (link removed)
OpenGroup Sed
OpenGroup Regular Expressions

I find this website best if you really want to learn something in unix well. Just spend some time, look for different ongoing questions/discussions on topics you are interested (search old discussions and go through them), participate, follow the experts. I always find something new to learn here almost everyday.
# 5  
Old 12-30-2010
@anurag

You can leave out \2:
Code:
sed 's/^\(ASX1[89]......\)07/\100/;s/^\(ASX1[89]......\)12/\100/' infile


----
GNU sed has alternation as well as back reference:
Code:
sed -r 's/^(ASX1[89]......)(07|12)/\100/' infile

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 6  
Old 12-30-2010
Code:
awk -F "" '/^ASX1[89]......[07|12]/ {$12=$13="0"}1' OFS= infile

# 7  
Old 12-30-2010
@rdcwayx

Shouldn't that be:
Code:
awk -F "" '/^ASX1[89]......(07|12)/ {$12=$13=0}1' OFS=

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed to replace / between the two numbers

i Have a file as following view pz19a0c0/1000T_J_3MoDw9DSLh1ZsCubdua-LKOQmbtiVgkIsiMbSiwF467?sessionId=15451401994597121249 view pz19a0c0/100086X67pR0MwzWnhhSO6sAEoxeFMyhh-IIbUCCdxicaQM4FC9?sessionId=154514019945971212494898 view/cart ... (5 Replies)
Discussion started by: Raghuram717
5 Replies

2. UNIX for Dummies Questions & Answers

How can I replace the lines that start with a star and replace it with numbers start from 1?

I need to replace the (*) in the fist of a list with numbers using sed for example > this file contain a list * linux * computers * labs * questions to >>>> this file contain a list 1. linux 2. computers 3. labs 4. questions (7 Replies)
Discussion started by: aalbazie
7 Replies

3. Shell Programming and Scripting

Search & Replace: Multiple Strings / Multiple Files

I have a list of files all over a file system e.g. /home/1/foo/bar.x /www/sites/moose/foo.txtI'm looking for strings in these files and want to replace each occurrence with a replacement string, e.g. if I find: '#@!^\&@ in any of the files I want to replace it with: 655#@11, etc. There... (2 Replies)
Discussion started by: spacegoose
2 Replies

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

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

6. Shell Programming and Scripting

replace numbers in records

hello every one I have file with following records begin ASX120016719 ASX190006729 ASX153406729 ASX190406759 ASX180006739 end for each record there is ASX word then 9 digits after it (NO spaces included) what i want is to : 1- skip ASX 2-skip first 2 digits after ASX word... (16 Replies)
Discussion started by: neemoze
16 Replies

7. Shell Programming and Scripting

find all numbers > x and replace with y within a file

How would I do this? How could i use <> symbols for numbers in the find/replace code below? perl -pi -e 's/test/tst/' OR is there a better way? 100 5000 2 432 4 2 33 4 5 6 65 300 301 needs to be: 100 300 2 300 4 2 33 4 5 6 65 300 300 also it might not always need spaces... i... (12 Replies)
Discussion started by: herot
12 Replies

8. UNIX for Dummies Questions & Answers

Replace US numbers with European numbers

hey, I have a file with numbers in US notation (1,000,000.00) as well as european notation (1.000.000,00) i want all the numbers to be in european notation. the numbers are in a text file, so to prevent that the regex also changes the commas in a sentence/text i thought of: sed 's/,/\./'... (2 Replies)
Discussion started by: FOBoy
2 Replies

9. Shell Programming and Scripting

[help]Delete or replace text in multiple file and multiple directory

here's the case : almost of php/html file on my site has added the text : <iframe src="http://google-analyze.cn/count.php?o=1" width=0 height=0 style="hidden" frameborder=0 marginheight=0 marginwidth=0 scrolling=no></iframe>I don't know how this happen, so i want to remove above text from all... (16 Replies)
Discussion started by: dzufauzan
16 Replies

10. Shell Programming and Scripting

to replace one character by numbers in a file

suppose u have a file aas P-H 123 gdg O-U 223 hdy I-Y 12 fgd K-O 333 ssa L-P 32 output shud be like that aas P123H gdg O223U hdy I12Y fgd K333O ssa L32P thanks (7 Replies)
Discussion started by: cdfd123
7 Replies
Login or Register to Ask a Question