![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
Sed Question?
Hi,
Here's what I'm trying to do. I have a set of numbers like this: 12-13 15-18 23-28 36-38 42-43 53-56 70-72 76 80-86 93-110 119-128 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? Or, is there a better way of accomplishing this? Thanks! |
| Forum Sponsor | ||
|
|
|
|||
|
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.
Code:
sed -e 's/([0-9]+)(-)([0-9]+)/{\1..\2}/g' file
|
|
||||
|
Hi.
If you are stuck with a sed that does not recognize "-r" such as on Solaris, you can use: Code:
#!/bin/bash -
# @(#) s1 Demonstrate sed extended regular expression match, substitution.
SED=/usr/xpg4/bin/sed
SED=sed
echo "(Versions displayed with local utility \"version\")"
# version >/dev/null 2>&1 && version =o $(_eat $0 $1) $SED
uname -rs
version >/dev/null 2>&1 && version bash $SED
FILE=${1-data1}
echo
echo " Input file $FILE:"
cat $FILE
echo
echo " As processed by sed:"
# $SED -r -e 's/([0-9]+)-([0-9]+)[ ]*/{\1..\2} /g' $FILE
$SED -e 's/\([0-9][0-9]*\)-\([0-9][0-9]*\)[ ]*/{\1..\2} /g' $FILE
exit 0
Code:
$ ./s1
(Versions displayed with local utility "version")
SunOS 5.10
GNU bash 3.00.16
sed (local) - no version provided.
Input file data1:
12-13 15-18 23-28 36-38 42-43 53-56 70-72 76 80-86 93-110 119-128
As processed by sed:
{12..13} {15..18} {23..28} {36..38} {42..43} {53..56} {70..72} 76 {80..86} {93..110} {119..128}
|
|
|||
|
Quote:
Code:
sed 's/\([0-9][0-9]*\)-\([0-9][0-9]*\)/{\1..\2}/g' file
|