Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Losing last line with sed

1 view
Skip to first unread message

ll...@my-deja.com

unread,
Oct 23, 2000, 3:00:00 AM10/23/00
to
When i'm using sed on a file that there is no cariage return on the
last line, i'm losing the last line in the new file !!!

EX:test.txt = 1
2
3
4
5

script test.sh = sed -ne "
s/2/aaa/g
p
" test.txt >test2.txt

test2.txt = 1
aaa
3
4

When i create the file test.txt i didn't but a cariage return on the
last line.

Is there a way do to this ?

Note:I'm doing a "for file " in the script and a don't want to modify
all file with a cat of a cariage return in all file.

Thanks in advance.


Sent via Deja.com http://www.deja.com/
Before you buy.

Ken Pizzini

unread,
Oct 24, 2000, 1:41:42 AM10/24/00
to
On Mon, 23 Oct 2000 21:18:44 GMT, ll...@my-deja.com <ll...@my-deja.com> wrote:
>When i'm using sed on a file that there is no cariage return on the
>last line, i'm losing the last line in the new file !!!

Yep. A bug/feature(???) of many sed implementations. The
POSIX.2 answer to this plight is that sed only is designed
to work with "text files", where a text file is a file which
meets certain criteria, one of which is that every line
(including the last) is terminated by a newline character.

>Is there a way do to this ?

Three leap to mind:
* use GNU sed --- it handles this situation just fine
* use perl in a sed-like fashion:
perl -pe 's/2/aaa/g' test.txt >test2.txt
* tweak your invocation:
{ cat test.txt; echo ''; } | sed 's/2/aaa/g' >test2.txt

--Ken Pizzini

0 new messages