This is the grep command I used

grep -ic address log*

The output is 0. I know for a fact that there are more than 60 occurrences of the word address in the log file. But I still get 0.

Now, I copied the contents of the log into a new file log2

 grep -ic address log2 

outputs 65! I copied the contents by selecting all and copy pasting instead of the cp command. I checked and made sure the file permissions were all fine.

ls -l show this

-rwxrwxrwx 1 root    root              91694 Jul 26 16:18 log

-rw-r--r-- 1 root    root              45220 Jul 30 14:16 log2*

The only discrepancy I can see is that the first log is twice the size of its copy. Is this a format issue?

When I open the file in vim it shows

[converted][dos] as the format.

How can I fix this?

Any help is appreciated.

  • What does "grep -ai address log2" give you? – TheFiddlerWins Jul 30 '18 at 16:28
  • @TheFiddlerWins grep -ai address log2 outputs a list of lines containing "address" However, since log2 is the copy of the original log and since grep -ic address log2 returns 65 I am not really concerned about the copy. – searcot jabali Jul 30 '18 at 16:50
  • @TheFiddlerWins grep -ai address log returns nothing. – searcot jabali Jul 30 '18 at 16:51
  • To convert a DOS style file to plain text, you can use dos2unix. – Thomas Jul 30 '18 at 17:57
  • @Thomas: Tried dos2unix earlier. No change. – searcot jabali Jul 31 '18 at 6:29

As your file log is twice as big as log2, but should be an exact copy, it is likely that is it encoded in UCS-2. Did the file originate on Windows?


file log log2

The output of file in the comment confirms this.

Use this for transparent grep

recode ucs2..utf8 < log | grep ...

Or this to convert the file

recode ucs2..utf8 log
grep ... log
  • Ralf:- log: Little-endian UTF-16 Unicode text, with very long lines, with CRLF, CR line terminators log2: ASCII text, with very long lines – searcot jabali Jul 31 '18 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.