sed: appending line to end of file

sed: appending line to end of file

am 29.11.2005 16:38:41 von cb

Hi,
I apologise for this newbie question.

I went through a couple of sed documentation on "appending a line to a
file" using the /a command but I could not get the syntax working.

Can someone show me how I can add a line to the end of file in sed?

$> cat test.log
line1
line2

Add "line3" to the end of test.log. Final output:

line1
line2
line3

thanks.

charlin

Re: sed: appending line to end of file

am 29.11.2005 16:40:28 von Lars Kellogg-Stedman

> Can someone show me how I can add a line to the end of file in sed?

Can you show us what you've tried so far?

-- Lars

--
Lars Kellogg-Stedman <8273grkci8q8kgt@jetable.net>
This email address will expire on 2005-11-23.

Re: sed: appending line to end of file

am 29.11.2005 17:00:55 von cb

$> sed '/$ /a\
> line3' test.log
line1
line2

Re: sed: appending line to end of file

am 29.11.2005 17:30:17 von Lars Kellogg-Stedman

On 2005-11-29, cb wrote:
> $> sed '/$ /a\
>> line3' test.log
> line1
> line2

'$' means "the last line in the file". /$ / means "the end of a line
followed by a space", which is never going to match. In sed, anything
between // is a regular expression to search for. If you want absolute
addresses (line numbers, "end of file", etc), you don't use //. For
example:

$ a\
line3

You might find the "Addresses" section of the sed man page helpful.

-- Lars

--
Lars Kellogg-Stedman <8273grkci8q8kgt@jetable.net>
This email address will expire on 2005-11-23.

Re: sed: appending line to end of file

am 29.11.2005 17:50:06 von Sine Nomine

On 11/29/2005 04:30 PM, Lars Kellogg-Stedman wrote:
> On 2005-11-29, cb wrote:
>> $> sed '/$ /a\
>>> line3' test.log
>> line1
>> line2
>
> '$' means "the last line in the file". /$ / means "the end of a line
> followed by a space", which is never going to match. In sed, anything
> between // is a regular expression to search for. If you want absolute
> addresses (line numbers, "end of file", etc), you don't use //. For
> example:
>
> $ a\
> line3

echo line3 >> test.log
might also be a simpler solution...

Re: sed: appending line to end of file

am 29.11.2005 18:24:00 von cb

thanks all.

Re: sed: appending line to end of file

am 30.11.2005 01:17:13 von dan.rickhoff

cb wrote:
> Can someone show me how I can add a line to the end of file in sed?
>
> $> cat test.log
> line1
> line2
>
> Add "line3" to the end of test.log. Final output:
>

Using sed:

sed -i -e '$a\
line 3
' test.log

cat test.log
line 1
line 2
line 3

Regards,
Dan

Re: sed: appending line to end of file

am 30.11.2005 02:41:55 von Bruce Barnett

Lars Kellogg-Stedman <8273grkci8q8kgt@jetable.net> writes:

> '$' means "the last line in the file". /$ / means "the end of a line
> followed by a space", which is never going to match.


Not quite true.

The regular expression "$ " matches a dollarsign followed by a space.
Try
echo "$ " | sed 's/$ /X/'

In general, the principle is that if the "meta-character" is in the
"wrong" place, then it becomes a regular character instead of a
special character.



Cheers!

--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.

Re: sed: appending line to end of file

am 01.12.2005 15:09:04 von Geoff Clare

Bruce Barnett wrote, on Wed, 30 Nov 2005:

> Lars Kellogg-Stedman <8273grkci8q8kgt@jetable.net> writes:
>
>> '$' means "the last line in the file". /$ / means "the end of a line
>> followed by a space", which is never going to match.
>
> Not quite true.
>
> The regular expression "$ " matches a dollarsign followed by a space.
> Try
> echo "$ " | sed 's/$ /X/'
>
> In general, the principle is that if the "meta-character" is in the
> "wrong" place, then it becomes a regular character instead of a
> special character.

For sed you are right. (Also ed, ex, and plain grep.)

For awk or grep -E you would be wrong and Lars right. The behaviour
of the ^ and $ anchors differs between BREs and EREs.

--
Geoff Clare