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 Display Modes
  #1 (permalink)  
Old 02-02-2008
Registered User
 

Join Date: Jul 2007
Posts: 28
Stumble this Post!
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!
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-02-2008
Registered User
 

Join Date: Jan 2008
Posts: 307
Stumble this Post!
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
Anyway, see if it works, backup your file first. If I am totally wrong someone will correct me.
Reply With Quote
  #3 (permalink)  
Old 02-02-2008
Registered User
 

Join Date: Jul 2007
Posts: 28
Stumble this 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.
Reply With Quote
  #4 (permalink)  
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: 508
Stumble this 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.

If your sed allows "-r", here's one way:
Code:
#!/bin/bash -

# @(#) s1       Demonstrate sed extended regular expression match, substitution.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1) 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

exit 0
Producing:
Code:
% ./s1
(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0
GNU sed version 4.1.2

 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}
See man sed for details ... cheers, drl

Last edited by drl; 02-02-2008 at 04:01 PM. Reason: Spelling mistake.
Reply With Quote
  #5 (permalink)  
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: 508
Stumble this Post!
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
Which yields:
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}
It seemed to have worked with both versions of Solaris sed ... cheers, drl
Reply With Quote
  #6 (permalink)  
Old 02-02-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 541
Stumble this Post!
Quote:
Originally Posted by Kweekwom View Post
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!
Code:
sed 's/\([0-9][0-9]*\)-\([0-9][0-9]*\)/{\1..\2}/g' file
Reply With Quote
  #7 (permalink)  
Old 02-02-2008
Registered User
 

Join Date: Jul 2007
Posts: 28
Stumble this Post!
Thank you drl and everyone else for your help, it worked great!

Thanks again!
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:50 AM.


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

Content Relevant URLs by vBSEO 3.2.0