Replacing digits using sed


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replacing digits using sed
# 1  
Old 05-29-2014
Question Replacing digits using sed

How to replace a character followed by a digit using sed? For example lets say I have this file -
Code:
a1       3242134    54235435   3241235
a2       3214345    45325626   3125435
a3       4236577    54365376   6865678
.
.
.
a3000  5432534    32546546   3254365

I want to replace all a1,a2,a3..a3000 with aw. I tried thi:s sed -i 's/^aa*[0-9]/aw/g' file_name. The problem is it replaces a1..a10 but then it starts counting aw0,aw1...aw9, then again aw0..aw9 upto a100. Then it starts aw0,aw1..aw999. How do just I erase these numbers? Thanks a lot!

Last edited by Don Cragun; 05-29-2014 at 11:56 PM.. Reason: Add CODE tags.
# 2  
Old 05-29-2014
Do this print what you want?

Code:
sed -e 's/^a[0-9]\+/aw/'

This User Gave Thanks to Aia For This Post:
# 3  
Old 05-29-2014
Yes!! Thanks man! You're a lifesaver! I almost gave up and was trying to do that using Matlab. Thanks a lot.
# 4  
Old 05-29-2014
Quote:
Originally Posted by Aia
Do this print what you want?

Code:
sed -e 's/^a[0-9]\+/aw/'

This will work with some versions of sed, but it isn't portable.

The following should work with any version of sed:
Code:
sed -e 's/^a[0-9][0-9]*/aw/'

This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 05-29-2014
Quote:
Originally Posted by Don Cragun
This will work with some versions of sed, but it isn't portable.

The following should work with any version of sed:
Code:
sed -e 's/^a[0-9][0-9]*/aw/'

Little clue gave it away
Quote:
sed -i 's/^aa*[0-9]/aw/g' file_name.

Last edited by Aia; 05-30-2014 at 12:07 AM.. Reason: Irrelevant
This User Gave Thanks to Aia For This Post:
# 6  
Old 05-30-2014
Quote:
Originally Posted by Aia
Little clue gave it away


In fact I was going to propose
Code:
sed -e 's/^a[123]\([0-9]\{1,3\}\)\?\+/aw/'

based on the wording of the post, however, I thought not to take the wording at face value since time and time again it is not accurate. So I chose to ask if it was enough.
The standards say that sed uses Basic Regular Expresions. In BREs [0-9]\+ matches a single digit followed by a literal plus sign. Some versions of sed ignore the standard on this and treat [0-9]\+ as an RE that will match one or more digits. The way to match one or more digits portably using standard BRE syntax is [0-9][0-9]* (which matches a single digit followed by zero or more digits). And, you're right in noting that we both missed the -i option the OP used. Looking at what was requested by the OP again, it might have been better to suggest:
Code:
sed -i 's/^a[1-9][0-9]*/aw/'

since it doesn't look like a leading zero should be allowed in the string of digits following the a or A at the start of the line.

Note also that ^a[123]\([0-9]\{1,3\}\)\?\+ (on systems that accept that form of RE) wouldn't do what was wanted even if it was a valid BRE): it precludes a4 through a9, a40 through a99, and a400 through a999 which, presumably, would have been wanted as part of the vertical elipsis between the lines starting with a3 and a3000.

If the OP wanted a leading a or A followed by any string of alphanumerics ending with a digit, that would be:
Code:
sed -i 's/^a[[:alnum:]]*[0-9]/aw/'

or:
Code:
sed -i 's/^a[[:alnum:]]*[0-9] /aw /'

And, of course, both of us are still assuming that the g at the end of the sed substitute command should be ignored and that the anchor at the start of the RE was intended to take precedence over the g flag at the end.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 05-30-2014
Sorry, I edited my post because, I thought it was irrelevant what I was going to propose.

Quote:
Note also that ^a[123]\([0-9]\{1,3\}\)\?\+ (on systems that accept that form of RE) wouldn't do what was wanted even if it was a valid BRE): it precludes a4 through a9, a40 through a99, and a400 through a999 which, presumably, would have been wanted as part of the vertical elipsis between the lines starting with a3 and a3000.
I would have been wrong as well because I believe it would have done a3000 - a3999

Last edited by Aia; 05-30-2014 at 01:12 AM..
 
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 / awk script to delete the two digits from first 3 digits

Hi All , I am having an input file as stated below 5728 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r03_q_reg_20_/Q 011 611 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r04_q_reg_20_/Q 011 3486... (4 Replies)
Discussion started by: kshitij
4 Replies

2. UNIX for Beginners Questions & Answers

Using sed or awk to replace digits in files

Hello; I am not good at file and stream editing. I need to replace a few digits in two files. The lines in files looks like this: Line in the first file, /dw300/data/obe/2019273.L800JR.1909.273 Line in second file, 1|2019273.L800JR.1909.273 I will write a function to connect to... (7 Replies)
Discussion started by: duke0001
7 Replies

3. Shell Programming and Scripting

Find filenames with three digits and add zeros to make five digits

Hello all! I've looked all over the internet and this site and have come up a loss with an easy way to make a bash script to do what I want to do. I have a file with a naming convention as follows: 2012-01-18 string of words here 123.jpg 2012-01-18 string of words here 1234.jpg 2012-01-18... (2 Replies)
Discussion started by: Buzzman25
2 Replies

4. Shell Programming and Scripting

Sed only digits not in numbers

Hi, I have a text file with an array of numbers such as : 123 1 456 45 9817 1 45 I would like to replace the digit "1" in a text file with "A". So it looks like this: 123 A 456 45 9817 A 45 If I use sed 's/1/A/g', I get A23 A 456 45 98A7 A 45 I... (3 Replies)
Discussion started by: jejeking
3 Replies

5. Shell Programming and Scripting

sed inside sed for replacing string

My need is : Want to change docBase="/something/something/something" to docBase="/only/this/path/for/all/files" I have some (about 250 files)xml files. In FileOne it contains <Context path="/PPP" displayName="PPP" docBase="/home/me/documents" reloadable="true" crossContext="true">... (1 Reply)
Discussion started by: linuxadmin
1 Replies

6. Shell Programming and Scripting

SED: replacing

Hello, I was looking around, but could not find the answer, so I hope you ppl can help me. I want simply to replace text.:rolleyes: I found out SED would be good for this task.:b: So I tried: :confused: 1.) find text in a line and replace this particular line: for finding... (3 Replies)
Discussion started by: unknown7
3 Replies

7. Shell Programming and Scripting

stripping out digits from a string with sed

i want to parse a string and only display the digits in that string... How would i accomplish this with sed command. For example. input string: " 033434343 dafasdf" output string: 03343434 Thanks (2 Replies)
Discussion started by: timmylita
2 Replies

8. Shell Programming and Scripting

help: single digits inflated to 2 digits

Hi Folks Probably an easy one here but how do I get a sequence to get used as mentioned. For example in the following I want to automatically create files that have a 2 digit number at the end of their names: m@pyhead:~$ for x in $(seq 00 10); do touch file_$x; done m@pyhead:~$ ls file*... (2 Replies)
Discussion started by: amadain
2 Replies

9. Shell Programming and Scripting

sed: removing any and all trailing digits?

We have a large number of oracle database related scripts that utilize the environment variables $ORACLE_SID and $DBNAME. In a single instance database the $ORACLE_SID is the same as the database name $DBNAME. So we have simply set DBNAME = $ORACLE_SID. However, now that we are clustering with RAC,... (5 Replies)
Discussion started by: Squeakygoose
5 Replies

10. Shell Programming and Scripting

Replacing in SED

I want to change the false in Node 1 to true. How do I do that? <Node1> <Usage>false</Usage> <Url>ABC</Url> </Node1> <Node2> <Usage>false</Usage> <Url>DEF<Url> </Node2> (8 Replies)
Discussion started by: superprogrammer
8 Replies
Login or Register to Ask a Question