possible sed usage


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting possible sed usage
# 1  
Old 09-27-2008
possible sed usage

Sorry about the title. I'm assuming I want sed anyway...

Here's the deal:

I have hundreds of files in a folder and they all are named with the same format.

$NAME-$VERSION-$ARCH-$BUILD

This is Slackwares' /var/log/packages directory BTW...

$BUILD is what I'm focusing on. It could be just "1" or it could be "1foo" or it could be "1foo357" or "1foo357bar"

I'm trying to isolate just the "1" regardless of what comes after it. I came to the conclusion that just identifying the last hyphen and then grabbing the very next number would be the easiest way.

This is what worked great until I started adding custom build tags like foo and foo357:

Code:
installed=$(ls /var/log/packages/$NAME-[[:digit:].]* | sed 's/^.*\(.\)$/\1/')

so if $build = 1foo357bar, then the above gives me "r"... The number I want will ALWAYS be after the last hyphen. There is a small chance that the number could reach double digits but it would be rare.

Any ideas? Thanks much for your help. I'm really stumped for some reason.
# 2  
Old 09-27-2008
this sed will do what you wanted..
Code:
 
sed 's/\(-[^-]*-[0-9]\)\(.*\)/\1/g'


Last edited by vidyadhar85; 09-27-2008 at 03:24 AM..
# 3  
Old 09-27-2008
Thanks for the reply but it's not quite what I need.

Code:
root@darkstar:~# ls /var/log/packages/glib2-2.18.1-i486-1gnome224jag 
/var/log/packages/glib2-2.18.1-i486-1gnome224jag
root@darkstar:~# installed=$(ls /var/log/packages/glib2-[[:digit:].]* | sed 's/\(-[^-]*-[0-9]\)\(.*\)/\1/g')
root@darkstar:~# echo $installed
/var/log/packages/glib2-2.18.1-i486-1   # installed != 1

root@darkstar:~# ls /var/log/packages/mozilla-firefox-3.0.2-native-1gnome224jag 
/var/log/packages/mozilla-firefox-3.0.2-native-1gnome224jag
root@darkstar:~# installed=$(ls /var/log/packages/mozilla-firefox-[[:digit:].]* | sed 's/\(-[^-]*-[0-9]\)\(.*\)/\1/g')
root@darkstar:~# echo $installed
/var/log/packages/mozilla-firefox-3  # installed != 1

As you can see (I forgot to mention), $NAME can also contain a hyphen sometimes so counting them out in the sed doesn't work half the time. $VERSION should always contain digits but may also contain letters. $ARCH could have either numbers and/or letters (native or i486). And $BUILD can also have both.

If I'm working with a package name like "xpaint-2.7.8.1-i486-2", then my old sed works fine.
Code:
root@darkstar:~# installed=$(ls /var/log/packages/xpaint-[[:digit:].]* | sed 's/^.*\(.\)$/\1/')
root@darkstar:~# echo $installed
2  # installed = 2

Thanks again. Much appreciated.
# 4  
Old 09-27-2008
This gives the number(s) after the last hyphen:

Code:
sed 's/.*-\([0-9]*\).*/\1/'

Regards
# 5  
Old 09-27-2008
Thanks Franklin. It seems to work altho from reading it, I don't know why it's grabbing numerals from $BUILD instead of $VERSION.... Smilie I thought sed worked from first to last so my understanding from that command is "print the first digits after the first hyphen".

But, it's worked with what I've thrown at it so far. I'll see if I can't wrap my head around it more.
# 6  
Old 09-27-2008
In sed .* is designed to be "greedy" and it always tries to match the longest possible string.

Regards
# 7  
Old 09-28-2008
Quote:
Originally Posted by Franklin52
In sed .* is designed to be "greedy" and it always tries to match the longest possible string.

Regards
Yes, that's why I usually prefer using perl or ruby on the command line.

e.g.
Code:
ruby -ne 'if /^<i>(.*?):/ then puts $1; end'

Once you are used to perls or ruby's regexs, you can get stuck on sed and grep.

Code:
sed -e 's/oldtext/newtext/g'
perl -p -i.bak -e 's/oldtext/newtext/g'

alternative to grep:
Quote:
perl -ne 'print if /pattern/' [file] [< file]
That said, there are great things you can do with sed.
Google for "perl one liners" and "sed one liners". http://sed.sourceforge.net/sed1line.txt

Last edited by sentinel; 09-28-2008 at 01:35 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command usage

Hi, Can anyone let me know the sed command usage requirement: sed 's/standard/standard_and/' <new.txt>new.txt here it needs to search for the pattern "standard" in the file new.txt and it should replace as "standard_and" in the same file new.txt Note: new.txt is having a separator... (8 Replies)
Discussion started by: srikanth_sagi
8 Replies

2. Shell Programming and Scripting

sed usage with Variable

Gurus, I am trying to display a match (single character) from beginning of the line in a file using a variable. I tried using sed ... not sure where am doing it wrong... sed -n "/^\$variable/p" FileName.shor sed -n "/^\${variable}/p" FileName.shBoth of the above are not working.....Thanks... (4 Replies)
Discussion started by: Kevin Tivoli
4 Replies

3. Shell Programming and Scripting

'sed' usage error.

Friends, I came across a wierd scenario while using sed. When I use the below cmd sequence at OS prompt it works fine: sed 's/# TMOUT=120/TMOUT=900/g' /etc/profile > /etc/profile.011211 However when I use the same in a script it throws the following error: ==Error== sed: 0602-404... (4 Replies)
Discussion started by: thisissouvik
4 Replies

4. Shell Programming and Scripting

sed usage

Hi, I want to replace some character whenever there is a space using sed. Input file name: aaa command i am trying is sed 's/^$/A/g' aaa (2 Replies)
Discussion started by: rakeshbharadwaj
2 Replies

5. Shell Programming and Scripting

sed usage

Hi, I want to read a command value and replace the command value in another file. Anybody know how to write it??? For example: File A: Command1 = test File B: Command1 = Command2 = Command3 = I want to write a shell script to read content from File A and replace Command1... (3 Replies)
Discussion started by: linboco
3 Replies

6. Shell Programming and Scripting

Sed usage

How can i use sed to change "Linux Cpu (EDF).sh" to "LinuxCpuEDF.sh"? I want to replace the spaces and brackets. (4 Replies)
Discussion started by: proactiveaditya
4 Replies

7. AIX

How to monitor the IBM AIX server for I/O usage,memory usage,CPU usage,network..?

How to monitor the IBM AIX server for I/O usage, memory usage, CPU usage, network usage, storage usage? (3 Replies)
Discussion started by: laknar
3 Replies

8. Shell Programming and Scripting

sed usage

I'd like to extract the temps from the following command via a series of sed statements but the actual syntax is beyond me. $ nc localhost 7634 ... (2 Replies)
Discussion started by: audiophile
2 Replies

9. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

10. Linux

sed usage

Hi , I have a question. How do I replace 2 words in one line like this IN CLO07 INDEX IN CLOIX07 to IN CLO07_S02 INDEX IN CLOIX07_S02 But one thing to remember is that there are lots of words like CLODM001 . So the only matching pattern is "IN CLO" sample file... (4 Replies)
Discussion started by: capri_drm
4 Replies
Login or Register to Ask a Question