The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #8  
Old 02-02-2008
Registered User
 

Join Date: Jul 2007
Posts: 28
Okay, now I'm having another problem with my script.

If I type:

Code:
echo {12..18}
It will spit out:

12 13 14 15 16 17 18

However, my script looks like this:

Code:
echo "Input first set of nodes" 

read node1 # This is where you insert the string of numbers

result=`echo $node1 | sed -r -e 's/([0-9]+)-([0-9]+)[ ]*/{\1..\2} /g'`

echo $result
I was hoping when it echoed $result, that the numbers that were inclosed in brackets for example {12..18} would then be printed as "12 13 14 15 16 17 18". This is not so, it merely echoes "{12-18}". Is there anyway for me to get this to work?

Thanks again.
Reply With Quote
Forum Sponsor
  #9  
Old 02-02-2008
Registered User
 

Join Date: Jan 2008
Posts: 328
Quote:
Originally Posted by Kweekwom View Post
KevinADC,

Thanks for looking into it. I tried it and got this error:

sed: -e expression #1, char 32: invalid reference \2 on `s' command's RHS


Is there anymore info I can give you to make my situation more clear?

Thanks again.
Actually mine should have been:

Code:
sed -e 's/([0-9]+)(-)([0-9]+)/{\1..\3}/g' file
(\3 instead of \2) but it may still not have worked. Like I said, I am not a sed coder really, juts learn what I have to on occasion. Thought I would give it a shot though as I am trying to pick up on more sed and ksh and similar.
Reply With Quote
  #10  
Old 02-02-2008
Registered User
 

Join Date: Jan 2008
Posts: 328
Quote:
Originally Posted by drl View Post
Hi.

The seds I have used do not do "in-place" processing by default. If they allow it, it's usually with the "-i" option.
Thanks for the correction. I really was not sure. sed is fairly new to me, trying to pick up on it in my spare time. I should have known though, in this regards sed and perl are very very similar, -e -i and etc, the regular expression syntax appears to be identical as well.
Reply With Quote
  #11  
Old 02-02-2008
drl's Avatar
drl drl is offline
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 556
Hi.
Quote:
Originally Posted by Kweekwom View Post
... it merely echoes "{12-18}". Is there anyway for me to get this to work?

Thanks again.
You are welcome; use bash3 and eval for this:
Code:
#!/bin/bash3 -

# @(#) user1    Demonstrate eval.

# echo "Input first set of nodes"

# read node1 # This is where you insert the string of numbers
node1="12-18"

result=`echo $node1 | sed -r -e 's/([0-9]+)-([0-9]+)[ ]*/{\1..\2} /g'`

echo {1..5}

eval echo $result

exit 0
Producing:
Code:
% ./user1
1 2 3 4 5
12 13 14 15 16 17 18
The useful page at http://www.tldp.org/LDP/abs/html/index.html can help you through these questions ... cheers, drl
Reply With Quote
  #12  
Old 02-03-2008
Registered User
 

Join Date: Jul 2007
Posts: 28
Awesome! Thanks drl, it worked perfectly.

Hopefully I only have one more question now.

I do what you suggested, and I want to sort the output of "eval echo" numerically, so I do this:

Code:
#!/bin/bash3 -

# @(#) user1    Demonstrate eval.

# echo "Input first set of nodes"

# read node1 # This is where you insert the string of numbers
node1="435-437,476-492 70-72,76,80-86"

result=`echo $node1 | sed -r -e 's/([0-9]+)-([0-9]+)[ ]*/{\1..\2} /g'`

echo {1..5}

eval echo $result | sort -n

exit 0
But, it doesn't sort them numerically. Is there something I'm doing wrong that is causing this?

Thanks again in advance, and thanks for the link!
Reply With Quote
  #13  
Old 02-03-2008
drl's Avatar
drl drl is offline
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 556
Hi.

The result of the final echo is a single line, so it is already in the correct order as sort looks at it.

What exactly are you looking for? If you were doing this manually, how would you go about it?

Note that you have interspersed a comma (,) at places in the list ... cheers, drl
Reply With Quote
  #14  
Old 02-03-2008
Registered User
 

Join Date: Jul 2007
Posts: 28
Quote:
Originally Posted by drl View Post
Hi.

The result of the final echo is a single line, so it is already in the correct order as sort looks at it.

What exactly are you looking for? If you were doing this manually, how would you go about it?

Note that you have interspersed a comma (,) at places in the list ... cheers, drl
I really don't know how I would go about it doing it manually.

I'm sorry, I didn't mean to have those comma's in there. Even with no comma's in the line, it still doesn't sort.

I guess I need to come at the whole thing from a different angle?

All I basically need it to do is take a list of numbers, like this:

435-437 70-72 76 80-86

and print the ranges and sort all of it numerically.

Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:29 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0