sed not see last row

Hi Everyone!

I have a problem my script. I'm useing sed to find the separate character in my text

sed 's/"[^"]*"//g' ./myfile.txt

My problem:
When the last row is doesn't has enter in the end of line, the sed is not work. So if i have a 30 rows text i will see 29 rows separators.

Please help me why not work. Thank You!

Can u show what is ur input and what output you are expecting?

Its not work if you make a text file by vi editor. Try to make a file in windows whit this text:

"","","","","","","","",""

After you try again but whit 2 rows, like this:

"","","","","","","","",""
"","","","","","","","",""

and try the sed script both.

i dont get it what you are trying to achieve. what is the expected output?

[oracle@server ~]$ cat infile3
"","","","","","","","",""
"","","","","","","","",""
"","","","","","","","",""
"","","","","","","","",""
[oracle@server ~]$ sed 's/"[^"]*"//g' infile3
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,

input-output:

alma@app2:/home/snayper$ cat aaa.txt
"","","","","","","","",""
"","","","","","","","",""alma@app2:/home/snayper$ sed 's/"[^"]*"//g' ./aaa.txt
,,,,,,,,
alma@app2:/home/snayper$

Without doing any special, tried with GNU sed and the sed that comes with AIX, same result:

myuser@somehost$ cat aaa.txt
"","","","","","","","",""
"","","","","","","","",""myuser@somehost$
myuser@somehost$ sed 's/"[^"]*"//g' ./aaa.txt
,,,,,,,,
,,,,,,,,myuser@somehost$

Please show the output of the following command:

od -c aaa.txt

... and tell us your OS and version of sed you are using.

There seems to be a little bit of a language barrier. With this example, I now understand what you were saying in your first message in this thread.

The sed utility (and most editors and text processing utilities meant to run on most UNIX and Linux systems) only have defined behavior when the input file is a text file. In particular sed commands work on input lines. When the input file contains text that is not terminated by a <newline> character (or as you said "doesn't has enter in the end of line"), the input is not a text file because it does not end with a <newline> character. By definition, a line ends with a <newline> character. The sed utility isn't processing the last "row" in your input file because it is not a line. As zaxxon has pointed out, this will work on some systems, but the standards do not require it to work.

On many versions of vi, if you try to edit a file that is missing the terminating <newline> character with the command:

vi aaa.txt

the status line at the bottom of the screen when you open the file will show something like:

"aaa.txt" [noeol]

meaning it did not find an End-Of-Line character at the end of the file. If you then enter the command :w followed by hitting the Enter (or return or carriage-return) key on your keyboard, vi will then add the missing <newline> character to the end of the file and sed will work as you expect.

If you have a list of files that you know are missing the trailing <newline>, you could also do something like:

for f in filename1 filename2 ...
do      echo >> "$f"
done

where filename1 , filename2 , and ... are replaced by the names of the files you want to fix. If you give this script the name of a file that already has a <newline> at the end of the file, it will add an empty line to the end of the file.

od -c output:

0000000   "   "   ,   "   "   ,   "   "   ,   "   "   ,   "   "   ,   "
0000020   "   ,   "   "   ,   "   "   ,   "   "  \n   "   "   ,   "   "
0000040   ,   "   "   ,   "   "   ,   "   "   ,   "   "   ,   "   "   ,
0000060   "   "   ,   "   "
0000065

can i do anything without change the original text? I mean, i can't write that file.

You have used three different names for your file: ./myfile.txt , aaa.txt , and now output . So, it looks like you have made (that is, written) a few copies of your file.

But, if you have an input file that ends with an incomplete line that you can't write and you just want to run your sed script on that file, the following will work:

(cat aaa.txt;echo)|sed 's/"[^"]*"//g'
1 Like

Thank You!

Thats not my file. My original file is read only. Your work around is very good! Thank You!