deleting newline from last line in file 
Author Message
 deleting newline from last line in file

Hi,

how can I delete a trailing newline in the last line of a file?

I tried
awk '{line = $0; print $0} END {printf("%s", $0)}'  <file>

but that obviously printed the last line twice...

Any suggestions are appreciated.

Thanks,
Noa.



Fri, 21 May 2004 16:20:42 GMT  
 deleting newline from last line in file

Quote:

> how can I delete a trailing newline in the last line of a file?

> I tried
> awk '{line = $0; print $0} END {printf("%s", $0)}'  <file>

awk '{a = $0; while (getline > 0) {print a; a = $0}} END {printf("%s", a)}'

seems to work for me. If by trailing new line you meant an *empty* line at
the end of the file, then maybe:

awk '{a = $0; while (getline > 0) {print a; a = $0}}
END {if (length(a) > 0 ) print a}'

Glenn



Fri, 21 May 2004 17:24:53 GMT  
 deleting newline from last line in file

Quote:

> how can I delete a trailing newline in the last line of a file?

How about

awk 'NR>1{print line} {line=$0} END{printf "%s",line}'

--
Tapani Tarvainen



Fri, 21 May 2004 21:14:57 GMT  
 deleting newline from last line in file

Quote:

> how can I delete a trailing newline in the last line of a file?

    NR > 1 { print line } { line = $0 } END { printf("%s", line) }

This works as expected on empty files and files containing only
one line. It does not, however, eliminate the final newline of each
of multiple files specified on the command line. It only truncates
the terminal newline of the last file read (i.e., the final line
of all input).

(It should go without saying--but, unfortunately, cannot in this
forum--that this script assumes the built-in variables FS and OFS
have their default values.)

Your post is, by the way, an excellent example of how to properly
ask a question in comp.lang.awk. Your question was succinct and
clear, but, as with even the most exacting verbal communication,
subject to possible interpretation and, thereby, misunderstanding.
But you also included an awk script that, though it did not do
exactly what you wanted it to do, it did clarify exactly what you
were *trying* to do. So despite the two or more conceivable
interpretations of your question as it was worded, there is no
question of your intent by virtue of the awk script you posted.

Whole flame wars could be averted if people's intentions were made
clear by posting code. Code is like a picture, and a picture's worth
a thousand words.

--
Jim Monty

Tempe, Arizona USA



Sat, 22 May 2004 04:55:41 GMT  
 deleting newline from last line in file

Quote:

>> how can I delete a trailing newline in the last line of a file?

awk 'NR>1{printf "\n"} {printf "%s,$0}'


Sat, 22 May 2004 06:50:22 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Deleting the last lines in a text file with tcl

2. I NEED TO DELETE A FILE THE LAST 3 MONTHS (directory)

3. Direct access files - deleting last record

4. read the last line in file

5. join last 2 lines of a file

6. print last line of input file

7. how to specify last line of file ?

8. How to read the x last lines of a text file

9. How to read the last line of http:// file

10. File: How to read the last line?

11. Best way to get Last line of file?

12. last 20 lines of a file on WinDoze ?

 

 
Powered by phpBB® Forum Software