sed question

Josh Miller joshua at itsecureadmin.com
Fri Aug 19 18:28:18 UTC 2011


On 08/19/2011 11:06 AM, Rob DeSanno wrote:
...
> What I am trying to do is to change the "-" character to something else ONLY
> when it is next to a number [0-9]. What makes this a challenge for me is
> that it can also exist next to a letter but never more than once next to a
> number.
>
> The line I am using is below and works fine for what I need expect that the
> first digit of the number at the end is cut off in the table I am trying to
> build.
>
> '102.1'>Columbus-612
> '104.1'>Fredericksburg-628
> '108.1'>Garden-Grove-736
> '101.1'>Mission-Viejo-1514
> '104.1'>Fort-Worth-418
> '1030.1'>Ft.-Lauderdale-304
> '1050.1'>Hamilton-713
>
> sed -e "s/^'/\<tr\>\<td\>/" $stores_down | sed -e "s/'>/\<\/td\>\<td\>/" | *sed
> -e "s/-[0-9]/\<\/td\>\<td\>/"* | sed -e "s/$/\<\/td\>\<\/tr\>/">>  $log
...

Hi Rob,

You're matching the hyphen and the digit following it, without capturing 
the digit to include in the resulting output.

Try this, I've split the sed command into a sed script (file.sed) and 
the input file into another file (file.txt):

 > cat file.txt
'102.1'>Columbus-612
'104.1'>Fredericksburg-628
'108.1'>Garden-Grove-736
'101.1'>Mission-Viejo-1514
'104.1'>Fort-Worth-418
'1030.1'>Ft.-Lauderdale-304
'1050.1'>Hamilton-713

 > cat file.sed
# for each line beginning with a single quote, end the table and row
s/^'/\<tr\>\<td\>/
# for each '>, replace with cell divider
s/'>/\<\/td\>\<td\>/
# for each hyphen digit, replace hyphen with cell divider
s/-\([0-9]\)/\<\/td\>\<td\>\1/
# for each line ending, replace with cell/row ending
s/$/\<\/td\>\<\/tr\>/


 > sed -f file.sed file.txt
<tr><td>102.1</td><td>Columbus</td><td>612</td></tr>
<tr><td>104.1</td><td>Fredericksburg</td><td>628</td></tr>
<tr><td>108.1</td><td>Garden-Grove</td><td>736</td></tr>
<tr><td>101.1</td><td>Mission-Viejo</td><td>1514</td></tr>
<tr><td>104.1</td><td>Fort-Worth</td><td>418</td></tr>
<tr><td>1030.1</td><td>Ft.-Lauderdale</td><td>304</td></tr>
<tr><td>1050.1</td><td>Hamilton</td><td>713</td></tr>



HTH,
-- 
Josh Miller
Open Source Solutions Architect
http://itsecureadmin.com/




More information about the redhat-list mailing list