How to replace a character followed by a digit using sed? For example lets say I have this file -
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.
In fact I was going to propose
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:
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:
or:
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:
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
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)