• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1126
  • Last Modified:

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
0
Nargzul
Asked:
Nargzul
  • 6
  • 3
  • 2
  • +2
1 Solution
 
ghostdog74Commented:
use a while read loop
eg



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

Open in new window

0
 
NargzulAuthor Commented:
Hum, I don't understand how it's can remove the last \n ???
0
 
TintinCommented:
Just edit the parameter file with whatever editor you like and remove the last blank line.

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
NargzulAuthor Commented:
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.
0
 
omarfaridCommented:
can you provide sample file
0
 
NargzulAuthor Commented:
No probs
testfile.txt
0
 
TintinCommented:
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.
0
 
NargzulAuthor Commented:
I don't have it, I've a program already compiled in C++, given by a professor, and we must just touch parameter files.
0
 
simon3270Commented:
awk '{if (NR == 1) printf("%s",$0); else printf("\n%s",$0)} file
0
 
simon3270Commented:
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.
0
 
NargzulAuthor Commented:
I was thinking your script will works, but I don't understand, there is always a \n at the end of the file :/
0
 
simon3270Commented:
i have a test inptfile wih two lines in it:

/home/user> od -c testfile.in
0000000   l   i   n   e   1  \n   l   i   n   e   2  \n
0000014
/home/user> awk '{if (NR == 1) printf("%s",$0); else printf("\n%s",$0)}' testfile.in > testfile.out
/home/user> od -c testfile.out
0000000   l   i   n   e   1  \n   l   i   n   e   2
0000013
/home/user>

The first "od" shows the newline after "line2" in the original file.  i then run the script and send the output to a new file.  The second "od" command shows that the newline after "line2" has been removed in the new file.  If you want the newline to be removed from the original file then just do:

  awk '{if (NR == 1) printf("%s",$0); else printf("\n%s",$0)}' testfile.in > testfile.out
  mv testfile.out testfile.in
0
 
NargzulAuthor Commented:
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!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 6
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now