Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

remove final \n from file

Status
Not open for further replies.

kasparov

Programmer
Feb 13, 2002
203
GB
Hi

I have a file which came out of our application - the final line of this file *should* end with \r\f.

Unfortunately I needed to edit the file (in vi) which puts an additional \n on the end of the file - so now it ends \r\f\n. My question is how can I remove the final \n.

I know I could use tr to remove (or change) all \n's & I expect I could use sed to remove this char but I don't know how to - any ideas?

Thanks, Chris
 
For info - we needed to do this pretty quickly & we discovered that the final char could be removed in Notepad - so I ftp'd it to Windows & back.

But a solution which a colleague found was:

Code:
echo `cat <myfile>|tail -1`\\c

This edits the final line OK - so if we needed to automate it we could run head on the file for all except the last line, then run this command & then cat them together.

Hopefully we won't need to do it again though ...

Chris
 
Something like this perhaps:

$ head -$(ls -l sourcefile | awk '{print $5-1}')c sourcefile > destfile

Although head on some operating systems appends a \n even when used in byte count mode... the above worked on Linux with the GNU head.

Annihilannic.
 
Here's a way if you want to remove the terminal byte:

Code:
dd if=inputfile ibs=1 count=$(expr "$(wc -c foo)" : "\([^ ]*\)*" - 1) of=outputfile

Cheers,
ND [smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top