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.

Rak kundra
  • 508
  • 3
  • 9
  • 20
  • `> abc.txt`? what are result of `file abc.txt`and `od -c abc.txt`? – Archemar Jan 25 '19 at 13:19
  • 1
    If you want the file to be *empty* (regardless of its original encoding / byte order mark) there are [plenty of ways](https://unix.stackexchange.com/a/131024/65304) – steeldriver Jan 25 '19 at 13:33
  • @Archemar The output of command "od -c abc.txt" is 0000000 \n 0000001 – Rak kundra Jan 25 '19 at 13:35
  • 1
    Please clarify whether your question is about truncating a file down to zero bytes, or about converting a file from DOS text format to Unix text format. – Kusalananda Jan 25 '19 at 15:39
  • @Kusalananda Here i am trying to convert the DOS text format to Unix text format . – Rak kundra Jan 27 '19 at 08:28
  • 1
    are you asking how to remove training new-line characters? or how to remove newline character if it is the only character? or how to empty a file (not caring what is in it)? – ctrl-alt-delor Jan 27 '19 at 18:05
  • You have converted it to Unix text format: It now has a new-line character. (the Dos equivalent is carriage-return, new-line). And please put question edits into the question. – ctrl-alt-delor Jan 27 '19 at 18:07
  • @ctrl-alt-delor Thanks for the repsonce . Can i get answer for this (how to remove newline character if it is the only character in the entire file ). – Rak kundra Jan 27 '19 at 18:19
  • Please put question edits into the question. – ctrl-alt-delor Jan 27 '19 at 18:20
  • @ctrl-alt-delor I have edited the question with more info in it.Thanks for your suggestions . – Rak kundra Jan 27 '19 at 18:25
  • Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/88876/discussion-between-rak-kundra-and-ctrl-alt-delor). – Rak kundra Jan 27 '19 at 18:42

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
  • 320,670
  • 36
  • 633
  • 936
  • 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