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
# 8  
Old 06-15-2012
How to do for one line

Code:
$ sed 's/   1! SRCNAM = 00001/1001! SRCNAM = 01001/' <sample15.txt
 1001! SRCNAM = 01001  !
    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!

# 9  
Old 06-15-2012
Thanks. I thought of doing this exact command, but I would need to loop from SRCNAM = 00001 to SRCNAM = 01001 and I wasn't sure if this was possible. I know I could loop from SRCNAM = 1 to SRCNAM = 1001, but the leading zeros complicate what I am trying to do.

Instead, is there a similar sed command that could specify the range of the placement of characters to change?

i.e.
Code:
 sed -n 1p| sed 's/^.\(....\)\(.*\)/\1001\2/g'

# 10  
Old 06-15-2012
Or alternatively try awk?
Code:
awk '{$1+=1000} /SRCNAM/{sub(/[0-9]+/,0 $1,$2)}1' FS=\! OFS=\! infile

# 11  
Old 06-15-2012
Thanks scrutinizer! That worked perfectly.
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