|
Home > Archive > Unix Shell > November 2006 > sed, using line number to replace text
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
sed, using line number to replace text
|
|
| peter_sands@techemail.com 2006-11-20, 7:22 pm |
| Hi,
I am converting a .csv file into an HTML file, all looks good apart
from one little problem. I need to make some sed inserts ( well I think
sed will do it ),
For every occurance of the word 'CLAIMS', within that line there is an
amount figure. I need to make that number bold as well, so I need to
insert a <B>, before the number. Using 'grep -n ' I can extract the
line numbers, see below:
183:<TR ALIGN=CENTER><TH>33003</TH><TD><B>CLAIMS
EUC</B></TD><TD></TD><TD>
</TD><TD></TD><TD></TD><TD></TD><TD></TD><TD>21781</TD>
340:<TR ALIGN=CENTER><TH>33003</TH><TD><B>CLAIMS
EUX</B></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD>7892</TD>
But how can I in sed use that line number , to change insert a tag.
What I need to end up with is something like... <B>7892</B>, like so::
<TR ALIGN=CENTER><TH>33003</TH><TD><B>CLAIMS EUX</B></TD><TD></TD><TD
></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD><B>7892</B></TD>
Or is there a better way of doing it.
Thanks
Pete
| |
| Chris F.A. Johnson 2006-11-20, 7:22 pm |
| On 2006-11-20, peter_sands@techemail.com wrote:
> Hi,
> I am converting a .csv file into an HTML file, all looks good apart
> from one little problem. I need to make some sed inserts ( well I think
> sed will do it ),
> For every occurance of the word 'CLAIMS', within that line there is an
> amount figure. I need to make that number bold as well, so I need to
> insert a <B>, before the number. Using 'grep -n ' I can extract the
> line numbers, see below:
>
> 183:<TR ALIGN=CENTER><TH>33003</TH><TD><B>CLAIMS
> EUC</B></TD><TD></TD><TD>
></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD>21781</TD>
>
> 340:<TR ALIGN=CENTER><TH>33003</TH><TD><B>CLAIMS
> EUX</B></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD>7892</TD>
>
> But how can I in sed use that line number , to change insert a tag.
> What I need to end up with is something like... <B>7892</B>, like so::
><TR ALIGN=CENTER><TH>33003</TH><TD><B>CLAIMS EUX</B></TD><TD></TD><TD
sed -e '183 s/[0-9]*/<b>&</b>/' \
-e '340 s/[0-9]*/<b>&</b>/' \
[vbcol=seagreen]
> Or is there a better way of doing it.
sed '/CLAIMS/ s/[0-9]*/<b>&</b>/'
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
| |
| Radoulov, Dimitre 2006-11-20, 7:22 pm |
|
<peter_sands@techemail.com> wrote in message
news:1164053012.767458.105960@m7g2000cwm.googlegroups.com...
> Hi,
> I am converting a .csv file into an HTML file, all looks good apart
> from one little problem. I need to make some sed inserts ( well I think
> sed will do it ),
> For every occurance of the word 'CLAIMS', within that line there is an
> amount figure. I need to make that number bold as well, so I need to
> insert a <B>, before the number. Using 'grep -n ' I can extract the
> line numbers, see below:
>
> 183:<TR ALIGN=CENTER><TH>33003</TH><TD><B>CLAIMS
> EUC</B></TD><TD></TD><TD>
> </TD><TD></TD><TD></TD><TD></TD><TD></TD><TD>21781</TD>
>
> 340:<TR ALIGN=CENTER><TH>33003</TH><TD><B>CLAIMS
> EUX</B></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD>7892</TD>
[...]
What I need to end up with is something like... <B>7892</B>, like so::
<TR ALIGN=CENTER><TH>33003</TH><TD><B>CLAIMS EUX</B></TD><TD></TD><TD
></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD><B>7892</B></TD>
[...]
sed 's/\(CLAIMS[^0-9]*\)\([0-9]*\)/\1<B>\2<\/B>/' infile
Regards
Dimitre
| |
| Michael Tosch 2006-11-21, 7:21 pm |
| Chris F.A. Johnson wrote:
> On 2006-11-20, peter_sands@techemail.com wrote:
>
> sed -e '183 s/[0-9]*/<b>&</b>/' \
> -e '340 s/[0-9]*/<b>&</b>/' \
>
>
> sed '/CLAIMS/ s/[0-9]*/<b>&</b>/'
>
Correction:
sed '/CLAIMS/ s/[0-9]*/<b>&<\/b>/'
--
Michael Tosch @ hp : com
| |
| Chris F.A. Johnson 2006-11-21, 7:21 pm |
| On 2006-11-21, Michael Tosch wrote:
> Chris F.A. Johnson wrote:
....[vbcol=seagreen]
>
> Correction:
> sed '/CLAIMS/ s/[0-9]*/<b>&<\/b>/'
Right. Or:
sed '/CLAIMS/ s|[0-9]*|<b>&</b>|'
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
|
|
|
|
|