Link to home
Start Free TrialLog in
Avatar of Nargzul
Nargzul

asked on

Remove last \n in a file

Hi!

I must use a script, and the problem is that this script absolutly need in parameter a file, that have a liste of computer, one by line, the last line too.

I cannot modify the script.

The problem is that all program I've tried, add  a \n at the end of the programm, and the script think there is a one computer more.

So I need to remove the last \n in file(under linux(ubuntu)), I'm open to all suggestion(awk/sed/any other programm).

Thanks for your help
Avatar of ghostdog74
ghostdog74

use a while read loop
eg



while read line
do
# do something with "$line"
done < file

Open in new window

Avatar of Nargzul

ASKER

Hum, I don't understand how it's can remove the last \n ???
Avatar of Tintin
Just edit the parameter file with whatever editor you like and remove the last blank line.

Avatar of Nargzul

ASKER

I've tried, with vim, gedit, ... but after this, if you make a "cat", you see that the prompt comes a line after, because they all add this line.
can you provide sample file
Avatar of Nargzul

ASKER

No probs
testfile.txt
Can you show us the section of the script that reads the file.  It must be doing something *very* strange indeed to give the behaviour you describe.
Avatar of Nargzul

ASKER

I don't have it, I've a program already compiled in C++, given by a professor, and we must just touch parameter files.
awk '{if (NR == 1) printf("%s",$0); else printf("\n%s",$0)} file
Oops, missed out the trailing quote:

awk '{if (NR == 1) printf("%s",$0); else printf("\n%s",$0)}' file

This prints out the first line without its trailing newline, then precedes each subsequent line with a newline.
Avatar of Nargzul

ASKER

I was thinking your script will works, but I don't understand, there is always a \n at the end of the file :/
ASKER CERTIFIED SOLUTION
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nargzul

ASKER

The problem was that the furnished file has \r too, I've removed these, and thereafter I've used your command, I now it's all ok!

Thanks a lot!