1

We have a file named as abc.txt and its a empty file. Due to some junk or control M characters, the file size is populated as 5 byte file. In order to have clean file we are using dos2unix command to remove the unknown character's. After using dso2unix command We can see the file size as 1 byte as it only contains a new line character .Can i know how to remove newline character if it is the only character in the entire file ?

Work Around :

cat abc.txt 

cat -v abc.txt
M-oM-;M-?^M

ll abc.txt
-rw-r--r-- 1 ORAPRD ADMIN 5 Jan 25 07:08 abc.txt

dos2unix abc.txt
dos2unix: converting file abc.txt to Unix format ...

ll abc.txt
-rw-r--r-- 1 shpprd ADMIN 1 Jan 25 07:09 abc.txt

cat -v abc.txt

od -c abc.txt
0000000 \n 
0000001

Does any one have solution for this?

Thank you.

1 Answers1

0

With dos2unix abc.txt you have successfully converted the DOS text file to a Unix text file.

The file still contains a newline character. This is a totally valid thing to have in a text file. It's simply an empty line of text.

If you want to totally truncate the file, then

>abc.txt

in the shell would do that.


To remove empty lines at the end of a file, see "How to remove multiple newlines at EOF?".

Kusalananda
  • 333,661
  • Thanks your response . After conversion of the file using dos2unix it remain's with a newline character and when i try to load it into database its loading as a new empty record because it has a new line character. Is there any way around in unix to know "how to remove newline character if it is the only character in the entire file" .So that i can use it after converting the file using dos2unix command . – Rak kundra Jan 27 '19 at 18:17
  • 2
    @Rakkundra It's difficult to answer that particular question because we don't know what you would expect the file to look like if it did not contain a lone newline. Would a file containing a single other character (with no newline) be valid, or would it be enough to check the file size and discard the file it it was only one byte long? Is the file ordinarily a multi-line text file? There are no examples. – Kusalananda Jan 27 '19 at 18:40
  • @Rakkundra Also, if the Unix file has an empty line in it, then the DOS file would have had one as well. That means that whatever it was that created the file should be fixed, really. – Kusalananda Jan 27 '19 at 18:44
  • @ The file abc.txt is a dynamic file and we will receive the file from Monday to Friday .Some times we will receive the data in the file and sometimes not. Here are the examples [ File with data : a|b|c|d\n 12|34|56|7\n ] .If there is no data in the file it will be empty but sometimes we will receive the empty file with the unseen junk M-oM-;M-?^M . In this case if the entire file contains only a new line character then we can remove the new line character else if it contains the data we will not touch the file at all . – Rak kundra Jan 27 '19 at 19:05
  • This works for me actually #!/bin/bash file="abc.txt" if ! grep -q "[[:print:]]" $file; then >$file fi – Rak kundra Jan 27 '19 at 19:53