sed command problem

hi

I am using "sed" command to find and replace a text in a file.
if the searched string is in the last line with no newline character in the end, it doesn't retrive this line. What is the solution to this?
i am using sed as:

sed -e "s/abc/ABC/g" test.txt

where i am replacing abc with ABC

why are you using -e ?
It should work without that.

Tine

Seems that sed only works on complete lines, so perhaps use awk instead...

awk '{gsub("abc","ABC")};1' test.txt

The side effect is that the last line will be properly terminated.

hi tine,
i have tried it w/o -e option also but it doesnt work ..

thanx

hi ygor,
my unix OS is Sun Solaris 5.8 and it does not support command gsub .
H awk cant be used i guess
neways
thanx a lot

Add a newline to the end of the file. You can see the missing new line using od. Do the following:

echo >> test.txt && sed -e "s/abc/ABC/g" test.txt

Cheers,

Keith

On Solaris use nawk

I don't think sed not able to substitute this change in Sun 5.8 and I tried that is not problem in mine.

Could you pls. show your file by:
cat test.txt
and then
sed -n l test.txt (note: that is lower letter of L rather than pipe)

I think your are missing the key point. There is no trailing newline. If you cat the file:

$ cat good_test.txt
abc abc
abc abc

Look at the file good_test.txt (with od -c):

$ od -c good_test.txt
0000000 a b c a b c \n a b c a b c \n
0000020

Let's make it a bad behaving file and strip out the newlines:

$ cat good_test.txt | tr '\012' " " > bad_test.txt

Look at the same file stripped of the newline (with od -c):

$ cat bad_test.txt | od -c
0000000 a b c a b c a b c a b c
0000020

Cheers,

Keith

can we have a solution to this
all that we have here is the workaround

from the above discussion

  1. No solution with sed so far
  2. awk has side effects
  3. forcefully changing the file to have a newline and then sed'ing it (equi to the side effect of awk)

is there some way that we do not change the file and still get the desired result

lets say i do not have a option, but I have to keep the file intact with the last line without the newline character, then how do i substitue on this line without having the newline character at the end.

This will update the file and keep the incomplete last line

nawk '{gsub("abc","ABC");a[m=NR]=$0}END{for(x=1;x<=m;x++)
printf (x==m)?a[x]:a[x]"\n">FILENAME}' test.txt

Refer this

perl -i -pne "s/abc/ABC/g" filename

cortesy Wintellect, thanks pal !!!
Refer this also