Sponsored Content
Full Discussion: Replacing digits using sed
Top Forums UNIX for Dummies Questions & Answers Replacing digits using sed Post 302903833 by Don Cragun on Thursday 29th of May 2014 11:57:22 PM
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:
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
All times are GMT -4. The time now is 03:22 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy