I've read numerous awk & sed liners to try and perform what I need and none seem to work. What I am trying to do is simply count the number of CR/LF breaks in my Linux file. This file has plain LF newlines as well, I just need to know how many true records I'm importing.
One such awk line I've tried is
awk '/^M$/{n++}; END { print n+0 }' my_file
or some such. This did not work. Any help would be great. I'm not an awk guru so please go easy.
Using GNU awk, which supports multi-character Record Separator:
awk -v RS='\r\n' 'END{print NR}' file
This sets the record separator to \r\n
and prints the total number of records.
For example:
$ echo $'record 1\r\nrecord\n2\r\nrecord 3' > file
$ awk -v RS='\r\n' 'END{print NR}' file
3
To those that think this answer is incorrect, let me propose another example. Consider the file:
bash-4.2$ cat -vet file
line 1$
line 2$
line 3bash-4.2$
(shell prompts intentionally left in to show the end of the file)
With normal UNIX line endings and with no newline at the end of the file. How many records are there in this file? Personally, I would say that there are 3. However, there are only two newline characters.
You can use this grep
to count all the lines ending with CR/LF
:
grep -c $'\r$' file
Pattern $'\r$'
will match only those lines that are ending with \r\n
and -c
will give you count of those lines.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With