using sed to find and replace multiple numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using sed to find and replace multiple numbers
# 1  
Old 06-15-2012
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....
Code:
    1! SRCNAM = 00001  !
    1! X =  50.0000,  0.0000, 25.0000,0.2279E-07,0.0000E+00,0.3697E-07,0.0000E+00,0.0000E+00,0.3212E-08,0.4051E-10  !  !END!
   10! SRCNAM = 00010  !
   10! X =  50.0000,  0.0000, 25.0000,0.2296E-07,0.0000E+00,0.3757E-07,0.0000E+00,0.0000E+00,0.3254E-08,0.4123E-10  !  !END! 
  100! SRCNAM = 00100  !
  100! X =  50.0000,  0.0000, 25.0000,0.1563E-06,0.0000E+00,0.2575E-06,0.0000E+00,0.0000E+00,0.2224E-07,0.2836E-09  !  !END!
 1000! SRCNAM = 01000  !
 1000! X =  50.0000,  0.0000, 25.0000,0.1563E-06,0.0000E+00,0.2575E-06,0.0000E+00,0.0000E+00,0.2223E-07,0.2836E-09  !  !END!

...and I would like to replace the entry "1" and "00001" with "1001" and "01001", respectively...and replace the entry "10" and "0010" with "1010" and "01010", respectively...and replace "100" and "00100" with "1100" and "01100", respectively...and replace "1000" and 01000" with "2000" and "02000", respectively.

The result I want would look like...
Code:
  1001! SRCNAM = 01001  !
  1001! X =  50.0000,  0.0000, 25.0000,0.2279E-07,0.0000E+00,0.3697E-07,0.0000E+00,0.0000E+00,0.3212E-08,0.4051E-10  !  !END!
  1010! SRCNAM = 01010  !
  1010! X =  50.0000,  0.0000, 25.0000,0.2296E-07,0.0000E+00,0.3757E-07,0.0000E+00,0.0000E+00,0.3254E-08,0.4123E-10  !  !END! 
  1100! SRCNAM = 01100  !
  1100! X =  50.0000,  0.0000, 25.0000,0.1563E-06,0.0000E+00,0.2575E-06,0.0000E+00,0.0000E+00,0.2224E-07,0.2836E-09  !  !END!
  2000! SRCNAM = 02000  !
  2000! X =  50.0000,  0.0000, 25.0000,0.1563E-06,0.0000E+00,0.2575E-06,0.0000E+00,0.0000E+00,0.2223E-07,0.2836E-09  !  !END!

I plan to put the correct code to do this in a shell script and iterate through each line.

Is using sed the right way to go?

Any help is appreciated.

Last edited by mercury.int; 06-15-2012 at 01:35 PM..
# 2  
Old 06-15-2012
Some clarification, please

Are you only doing for the lines like:
Code:
SRCNAM = 00001

I did not see any "1" as you mentioned in your request; but did see this "00001"
# 3  
Old 06-15-2012
The "1", "10", "100", "1000" all lie in the first few character positions for every line and I would like to change them all in addition to the cases with:

SRCNAM = 00001, SRCNAM = 00010, ect.

Does this answer your question?

Thanks for the help.

Gary.
# 4  
Old 06-15-2012
Are those the only five rules for substitution?

And, from
Code:
1! SRCNAM = 00001  !

to this
Code:
1000! SRCNAM = 01000  !

does not seem to make sense given your instructions:
Quote:
...and I would like to replace the entry "1" and "00001" with "1001" and "01001"
# 5  
Old 06-15-2012
Ah, yes, that does not make any sense. I made a mistake in my initial post. I have corrected it.

Gary.
# 6  
Old 06-15-2012
If only those five substitutions, would five sed commands be fine?
# 7  
Old 06-15-2012
There are more lines to change, but I just wanted to get a flavor of the type of commands that I would need to put in a loop in a shell script. Five sed commands would be more than enough to get me going.

Thanks.

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

Sed/awk to find negative numbers and replace with 1?

Greetings. I have a three column file, and there are some numbers in the second column that are <1. However I need all numbers to be positive, thus need to replace all those numbers with just one. I feel like there must be a simple way to use awk to find these numbers and sed to replace but can't... (5 Replies)
Discussion started by: Twinklefingers
5 Replies

3. UNIX for Dummies Questions & Answers

Selective Replacements: Using sed or awk to replace letters with numbers in a very specific way

Hello all. I am a beginner UNIX user who is using UNIX to work on a bioinformatics project for my university. I have a bit of a complicated issue in trying to use sed (or awk) to "find and replace" bases (letters) in a genetics data spreadsheet (converted to a text file, can be either... (3 Replies)
Discussion started by: Mince
3 Replies

4. Shell Programming and Scripting

sed&awk: replace lines with counting numbers

Dear board, (I am trying to post this the 3rd time, seems there's some conflicts with my firefox with this forum, now use IE) ------ yes, I have searched the forum, but seems my ? is too complicated. ------------origianl file --------------- \storage\qweq\ertert\ertert\3452\&234\test.rec... (4 Replies)
Discussion started by: oUo
4 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

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 :wall: ... (10 Replies)
Discussion started by: colinwilson1303
10 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. Shell Programming and Scripting

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: BEGIN ASX15001 BEGIN ASX15000000500020101230 ASX18001020070002010123... (10 Replies)
Discussion started by: neemoze
10 Replies

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

10. Shell Programming and Scripting

how to find a sum of multiple numbers

I have a command which returns some numbers as follows: $ls -l ${dbname}.ix* | awk '{print $5 }' 929792 36864 57344 73728 53248 114688 How can I find the sum of those numbers by piping this output into 'awk' or some other editor/command? Thanks a lot -A (3 Replies)
Discussion started by: aoussenko
3 Replies
Login or Register to Ask a Question