Visit Our UNIX and Linux User Community

append newline to files with incomplete last line

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers append newline to files with incomplete last line
# 1  
Old 04-14-2004
append newline to files with incomplete last line

Hi all,

Is there any way I can check a file for the linefeed character at the end of the file, and append one only if it is missing (ie. Incomplete last line)?

Need to do this because I need to write a script to process files FTP-ed over from various machines, which may or may not be running UNIX. The files are not supposed to have any trailing blank lines after the last line. I also need to compare the number of lines in the file with a corresponding file which contains the number of lines expected in the first file, and process the file only if the numbers are equal.

My problem now is that files created on the Windows platform always have this "Incomplete last line" error when opened with vi after doing a dos2unix operation. This causes wc to count one less line than what is in the file, and the script fails when comparing the number of lines.

I tried using
  echo >> $filename
but while it solves the incomplete last line problem with the Windows-created files, it also appends a new line to the UNIX-created files.

# 2  
Old 04-14-2004
Perhaps use awk...

awk 1 infile > outfile

Previous Thread | Next Thread
Test Your Knowledge in Computers #786
Difficulty: Medium
In 2019, Bill Gates had a net worth estimated to be over $200 billion USD.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace newline with comma and append quotes

Hi, I have below requirement. Apple Orange Banana Required O/p in bash 'Apple,Orange,Banana' Can you please help. Please wrap your samples, codes in CODE TAGS as per forum rules. (3 Replies)
Discussion started by: Rtk
3 Replies

2. Shell Programming and Scripting

Append Multiple files with file name in the beginning of line

Hi, I have multiple files having many lines like as bvelow: file Name a.txt abc def def xyz 123 5678 file Name b.txt abc def def xyz 123 5678 I would like to append files in the below format to a new file: file Name c.txt (7 Replies)
Discussion started by: rramkrishnas
7 Replies

3. UNIX for Advanced & Expert Users

Help in adding a string at the end of each line and append files vertically

hi, i need a help in the script , need to append a string at the end of each line of a files , and append the files into a single file vertically. eg file1 has the following columns abc,def,aaa aaa,aa,aaa files 2 has the following rows and columns abc,def,aaa aaa,aa,aaa i... (3 Replies)
Discussion started by: senkerth
3 Replies

4. Shell Programming and Scripting

Append variable texts to the beginning of each line in all files in a directory

I am writing a code to append some numbers in the beginning of each line in all the files present in a directory. The number of files are really huge. The files are numbered as 1.sco, 2.sco, 4.sco (Note: 3.sco is missing). The files currently look like this: 1.sco 2 3 5 6 6 7My task is to... (4 Replies)
Discussion started by: shoaibjameel123
4 Replies

5. Shell Programming and Scripting

how to append blank line in multiple files in unix

Dear All- Please suggest a single unix command which can append blank line in multiple files. I need to achieve this using a single unix command and not a script Please do advice Regards, Suresh (2 Replies)
Discussion started by: sureshg_sampat
2 Replies

6. Shell Programming and Scripting

Incomplete last line

I need help with appending the last line to the file only if the last line is incomplete. I tried awk 1 filename > new_filename but it appends to every file and I only want to append to the file if the last line is incomplete (3 Replies)
Discussion started by: smee
3 Replies

7. Shell Programming and Scripting

incomplete last line

How do I find through script if a contains "incomplete last line". If it does then only insert a new line character as:: echo "" >> filename.txt (4 Replies)
Discussion started by: PRKS
4 Replies

8. Shell Programming and Scripting

Sed append newline character

Hi All, I am new to Shell scripting.. I have a task to parse the text file into csv format. more then half the things has done. But the problem is when I use the sed command in shell script. it appends newline character at the end of the line. and so when I open the file in CSV it's format... (3 Replies)
Discussion started by: Gaurang033
3 Replies

9. Shell Programming and Scripting

Append newline at the file end

Hi All, Is there any way to append a newline character at the end of a file(coma-separated file), through shell script? I need to check whether newline character exists at the end of a file, if it does not then append it. Regards, Krishna (1 Reply)
Discussion started by: KrishnaSaran
1 Replies

10. Shell Programming and Scripting

append newline to existing variables

firstly, i check is the variable empty or not, if so vesselNameList=`echo $vesselName` if not vesselNameList="${vesselNameList}""\n"`echo "$vesselName"` and it produces this result BUNGA TERATAI 3 5055\ JADE TRADER 143W\ MOL SPLENDOR 0307A BUNGA TERATAI 3 5055\ JADE... (1 Reply)
Discussion started by: finalight
1 Replies

Featured Tech Videos