Newline at the end of every line in a file.

How would u write a program in perl , sed or awk that would assure a there is a newline at the end of every line of a text file including the last line.

Thanks
tomatocansAsked:
Who is Participating?
 
erikfriendConnect With a Mentor Commented:
here is code that reads in lines from a text file and checks the line for either a newline or linefeed at the end.

if is has a newline/linefeed you could just write that line to an output file.

if it is missing the newline/linefeed you could add a newline with $_ .= "\n\r";  then write the fixed line to the output file.



open (SOMEFILE, "somefile.txt");

while (<SOMEFILE>) {
  if (/[\n\r]$/) {
    #line is ok
  }
  else {
    #line is missing linefeed or newline
  }
}

close(SOMEFILE);
0
 
ozoCommented:
perl -i -pe 'BEGIN{$\="\n"}chomp' file
0
 
tomatocansAuthor Commented:
Thanks
0
All Courses

From novice to tech pro — start learning today.