I have dos file by default populating with CRLF as line breakers. But , some times I get LF instead of CRLF. I need to figure where exactly change is occurring for line breaks.And I don't want to replace LF with CRLF.Just to look at the change of record with line breaker

  • Does the answer have to be in python, as you've tagged? – Jeff Schaller Jan 10 '18 at 20:38
  • If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you! – Jeff Schaller Jan 14 '18 at 15:53

Given the following short example:

$ cat -et file

(where cat -et is used to display the line endings) then

$ awk '{last = $0} NR==1 {cr = /^M$/ ? 0 : 1; next} cr == 0 && /^M$/ {print last; cr = 1} cr == 1 && !/^M$/ {print last; cr = 0}' file | cat -et

where the ^M are entered as actual CR characters using Ctrl+V Enter for example.

Here's a somewhat literal attempt at a python translation:


import re

p = re.compile(r'.*\r$')

with open('file', 'r', newline='') as f:
    for idx, line in enumerate(f):
        last = line
        m = p.match(line)
        if (idx==0):
            cr = 0 if m else 1
        if (cr==0) and m:
            print(last, end='')
            cr = 1
        elif (cr==1) and not m:
            print(last, end='')
            cr = 0

If your system has cat with the -e switch, you can use it to see the carriage returns. Here, the first line has a CR (shown as ^M), the second one doesn't.

$ cat -e crlf

(Though if some joker wrote a literal caret + M combination in your file, it'll look just the same as a CR.)

Alternatively, use less -U (provided -r is not given), it shows carriage returns similarly as ^M, but in reverse color.

  • but this won't work for only to show the change overs from CRLF to LF and vice-versa. – Kiran Jan 10 '18 at 20:24
  • @Kiran, ah, of course you wanted the find the transitions programmatically, sorry. – ilkkachu Jan 10 '18 at 20:26

With GNU grep:

grep --binary -v $'\r'$ dosfile

Sample input (with a joker on line 3 holding a caret and an M):

$ cat -e dosfile
line3 joker ^M^M$

$ od -c dosfile
0000000   l   i   n   e   1  \r  \n   l   i   n   e   2  \r  \n   l   i
0000020   n   e   3       j   o   k   e   r       ^   M  \r  \n   l   i
0000040   n   e   4  \r  \n   l   i   n   e   5  \r  \n   l   i   n   e
0000060   6  \n   l   i   n   e   7  \n

$ grep --binary -v $'\r'$ dosfile
  • @ Jeff . Thank you so much . Finally able to see the transitional change overs. – Kiran Jan 10 '18 at 20:57
  • It just occurred to me that you could add the -n flag to grep, to indicate the line numbers -- as a helpful point of reference, if there's similar lines/data in the file. – Jeff Schaller Jan 11 '18 at 1:37

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.