Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script to count CR/LF in a text file

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.

like image 980
jiveturkey Avatar asked Dec 04 '22 04:12

jiveturkey


2 Answers

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.

like image 85
Tom Fenech Avatar answered Dec 21 '22 09:12

Tom Fenech


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.

like image 27
anubhava Avatar answered Dec 21 '22 11:12

anubhava