tricky sed problem

tricky sed problem

am 30.11.2007 20:05:56 von zmrzlina

Hi,

line="HFC125MMR= 0.000e+00,"
sed "/&RUNCNST/ a\
$line
" CNTLATM >CNTLATM.tmp

What I want is to insert the above string $line into a text file below
the value &RUNCNST

What I have above works EXCEPT that I need it to offset $line by one
whitespace.
i.e.
The output from above is

[snip]
&RUNCNST
HFC125MMR= 0.000e+00,
MPARWTR=1.0000e-03,
ANVIL_FACTOR=2.5000,
[snip]

What I want it the above, except for one white space in front of
HFC125MMR. I have tried various combinations using \ (like \$line,
but that just writes $line as a string instead a variable) but I can't
seem to get it right.

Any help is much appreciated. I have spent a long time googling and
trying to do this on my own.

Thank you

Re: tricky sed problem

am 30.11.2007 20:55:53 von Maxwell Lol

zmrzlina@volny.cz writes:

> Hi,
>
> line="HFC125MMR= 0.000e+00,"
> sed "/&RUNCNST/ a\
> $line
> " CNTLATM >CNTLATM.tmp

try:

line="HFC125MMR= 0.000e+00,"
sed "/&RUNCNST/ a\
\ $line
" CNTLATM >CNTLATM.tmp

Re: tricky sed problem

am 30.11.2007 21:09:34 von Ed Morton

On 11/30/2007 1:05 PM, zmrzlina@volny.cz wrote:
> Hi,
>
> line="HFC125MMR= 0.000e+00,"
> sed "/&RUNCNST/ a\
> $line
> " CNTLATM >CNTLATM.tmp
>
> What I want is to insert the above string $line into a text file below
> the value &RUNCNST
>
> What I have above works EXCEPT that I need it to offset $line by one
> whitespace.
> i.e.
> The output from above is
>
> [snip]
> &RUNCNST
> HFC125MMR= 0.000e+00,
> MPARWTR=1.0000e-03,
> ANVIL_FACTOR=2.5000,
> [snip]
>
> What I want it the above, except for one white space in front of
> HFC125MMR. I have tried various combinations using \ (like \$line,
> but that just writes $line as a string instead a variable) but I can't
> seem to get it right.
>
> Any help is much appreciated. I have spent a long time googling and
> trying to do this on my own.
>
> Thank you
>

For anything but simple substituions, use awk rather than sed:

line="HFC125MMR= 0.000e+00,"
awk -v line="$line" '{print} /&RUNCNST/{ printf " %s\n",line }' CNTLATM >CNTLATM.tmp

Ed.

Re: tricky sed problem

am 01.12.2007 00:29:30 von Edward Rosten

On Nov 30, 1:09 pm, Ed Morton wrote:

> For anything but simple substituions, use awk rather than sed:

In general, except that (portably), awk has strictly regular
expressions (ie no backreferences), where as sed does not.

-Ed