Quick Sed Question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Quick Sed Question
# 1  
Old 09-26-2011
Quick Sed Question

Just want to know why when I do the following in sed, the required is not extracted.

Code:
echo "ab01cde234" | sed 's/[0-9]*$//'

result: ab01cde (Which is correct)

Code:
echo "ab01cde234" |sed 's/.*\([0-9]*\)$/\1/'

result: blank (was expecting 234)
or
Code:
echo "ab01cde234" |sed 's/.*\([0-9]\)*$/\1/'

result: blank (was expecting 234)

Am i missing something here? Also note that 234 can be has many digits, just need to extract the last section of the digits.

Thanks

Last edited by pludi; 09-26-2011 at 07:38 AM..
# 2  
Old 09-26-2011
you can try thisSmilie
Code:
echo "ab01cde234" |sed 's/.\{7\}\([0-9]*\)$/\1/'
234

.* --> covers all string and sed does not consider your remaining exp that ( \([0-9]*\)$ )

regards
ygemici
# 3  
Old 09-26-2011
Thanks ygemici, but the {7} is doing a fixed set of characters at the beginning of the string, is there any way just to extract the last set of digits, without fixing the initial character set.

Thanks
Ed
# 4  
Old 09-26-2011
Code:
echo "ab01cde234" |sed 's/.*[^0-9]//'

The above matches the longst string which ends in something which is not a member of [0-9]. It substitutes the match with "nothing". Thus it returns the rest of the line.

More precise would be to force it to start from the start of the line.

Last edited by pludi; 09-26-2011 at 08:24 AM..
# 5  
Old 09-26-2011
this one?
Code:
echo "ab01cde234" | sed 's/.*[a-z]\([0-9]*\)$/\1/g'

--ahamed
# 6  
Old 09-26-2011
Thks guys, the last two did the trick.

Many Thanks for your help on this.
# 7  
Old 09-26-2011
Quote:
Originally Posted by eo29
Thanks ygemici, but the {7} is doing a fixed set of characters at the beginning of the string, is there any way just to extract the last set of digits, without fixing the initial character set.

Thanks
Ed
Hi Ed
sed does not parse your expression for dynamic strings
if you has fixed string then it can be write with sed.
however you can use grep for your needs more sensiblely Smilie

Code:
echo "ab01cde234"|grep -o "[0-9]*$"
234

if you want to use sed , maybe you can try like this
Code:
echo "ab01cde234" | sed 's/.*[a-zA-Z]\([0-9][0-9]*\)[A-Za-z]*/\1/g'
234

or
Code:
echo "abca121a1b01cde23412zxxx1212aa" | sed 's/.*[a-zA-Z]\([0-9][0-9]*\)[A-Za-z]*/\1/g'
1212


Quote:
Originally Posted by ahamed101
this one?
Code:
echo "ab01cde234" | sed 's/.*[a-z]\([0-9]*\)$/\1/g'

--ahamed
this expression only make this examples not global!.
for example
Code:
echo "ab01cde234AA" | sed 's/.*[a-z]\([0-9]*\)$/\1/g'
ab01cde234AA

regards
ygemici
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Quick sed/awk question

Hi fellow linux-ers, I have a quick question for you. I have the following text, which I would like to modify: 10 121E(121) 16 Jan 34S 132E 24 Feb 42 176E(176) 18 Sep 21S 164E 25 May 15 171W(-171) 09 Jul How can I do the following 2 modifications using sed and/or awk? 1. in 1st column,... (1 Reply)
Discussion started by: lucshi09
1 Replies

2. Shell Programming and Scripting

Quick Question on sed command in shell script

Hello, I have the following line in one of my shell scripts. It works fine when the search string($SERACH_STR) exists in the logfile($ALERTLOG) but if the search string does not exist this line errors out at run time. Is there a way to make this line return 0 if it is not able to find the... (4 Replies)
Discussion started by: luft
4 Replies

3. Shell Programming and Scripting

quick sed question

hey, Im just wondering is there away to get sed to read from a variable eg it doesn't seem to work, i really need to be able to recursively change the same data set... (2 Replies)
Discussion started by: vbm
2 Replies

4. Shell Programming and Scripting

a quick SED question

I have a line EXTDIR=`echo $i | sed 's/\-tar.gz//'` which looks for files ending in -tar.gz, i would like to increase the functionality so that it looks for .tar.gz files as well as -tar.gz. Do i put the - in square brackets with a dot ? like this EXTDIR=`echo $i | sed 's/\tar.gz//'` ... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

5. UNIX for Dummies Questions & Answers

quick question

from command prompt I did grep two words on a same line for eg: grep abc | grep xyz and I got tht particular line, but I want to know when I vi that file how to directly search for that particular line? I appreciate if any one can provide answer, thanks in advance (2 Replies)
Discussion started by: pkolishetty
2 Replies

6. UNIX for Dummies Questions & Answers

Another quick question

Hi guys sed -e "s/$<//g" the $< can allow me to assign an input value to the variable right? do the double quotes check the previous context? (1 Reply)
Discussion started by: hamoudzz
1 Replies

7. UNIX for Dummies Questions & Answers

quick question

hi guys trying to understand what this line means sed is a stream editor and i understand that, i have a file already selected i want to edit so i use -e sed -e the next stesp is s/$* s is a subsititute replacement sed -e s/$*//g $ is in reference of the last line /g makes it... (2 Replies)
Discussion started by: hamoudzz
2 Replies

8. UNIX for Advanced & Expert Users

Quick VI question

This "SHOULD" be a simple question, but looking through several books has turned up nothing, so I turn once again to the experts!! How do you vi a file so that you can see special characters. I believe my /etc/passwd file is being corrupted during an upgrade process, however the files... (6 Replies)
Discussion started by: Recon
6 Replies

9. UNIX for Dummies Questions & Answers

Quick Question

Hello There! I am trying to write this SIMPLE script in Bourne Shell but I keep on getting syntax errors. Can you see what I am doing wrong? I've done this before but I don't see the difference. I am simply trying to take the day of the week from our system and when the teachers sign on I want... (7 Replies)
Discussion started by: catbad
7 Replies

10. UNIX for Dummies Questions & Answers

A Quick Question

Wat is the difference between the cp mv ln etc in /usr/sbin/static and cp mv ln functions in /usr/bin (4 Replies)
Discussion started by: DPAI
4 Replies
Login or Register to Ask a Question