But I want to echo the ranges of numbers. Is there any way I can get sed to replace for example, "12-13 15-18" with "{12..13} {15..18}" so it will echo the entire range?
I'm not really a sed coder but I think it is the same as perl in this situation with the excpetion that sed does inplace editing by default (I could be wrong). The problem is you have not described your input besides saying it has those sequence of ranges you posted.
sed -e 's/([0-9]+)(-)([0-9]+)/{\1..\2}/g' file
Anyway, see if it works, backup your file first. If I am totally wrong someone will correct me.
Okay, now I'm having another problem with my script.
If I type:
echo {12..18}
It will spit out:
12 13 14 15 16 17 18
However, my script looks like this:
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?
(\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.
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.
#!/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
I do what you suggested, and I want to sort the output of "eval echo" numerically, so I do this:
#!/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?
OK, let us assume that we can easily get rid of the commas.
Then we are left with one line that contains a list of numbers, each separated from the next by a blank character. We have an arranging program -- sort -- that orders lines. What transformation would get those two ideas together? What do we need to do? ... cheers, drl
The tee allows you to look at the intermediate form of the data. See the man pages for details on the rest. The tr especially often varies from system to system.
That's part of the design of *nix -- put general tools together to solve specific problems.
Wow drl, that's really cool. It works just as I need it to! I'll definitely delve deeper into those commands so I can understand them better. Thanks so much for your help!
If you get a chance, awk, as in the solution that ghostdog74 posted, is worthwhile to learn. It breaks up input lines into fields, which can then be re-arranged, deleted, assigned to, etc. Very useful, and, once you get the idea of:
pattern-part { action-part }
you can do a lot with very little work ... cheers, drl