LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-10-2022, 07:22 PM   #1
__John_L.
LQ Newbie
 
Registered: Feb 2021
Posts: 14

Rep: Reputation: Disabled
grep question re: counting DOS newlines


I've a file, temp.txt, that has DOS newline characters at the end of each line.

I thought the following would work:

Code:
grep -c -E "\r\n" temp.txt
however, the following warnings are returned:

Code:
grep: warning: stray \ before r
grep: warning: stray \ before n
Thanks in advance for any clarification you can provide.
 
Old 11-10-2022, 07:55 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 23,677

Rep: Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184
Try:
Code:
grep -c $'\015' temp.txt
015 is the octal code for carriage return.

If each line only has one carriage return you can just count the number of lines in the file with wc.
 
1 members found this post helpful.
Old 11-10-2022, 07:56 PM   #3
BudiKusasi
Member
 
Registered: Apr 2017
Distribution: Artix, MX
Posts: 327

Rep: Reputation: Disabled
On Linux always read info by --help

...

-M, --multiline run in multiline mode
-m, --max-count=number stop after <number> matched lines
-N, --newline=type set newline type (CR, LF, CRLF, ANYCRLF, ANY, or NUL)
-n, --line-number print line number with output lines

...


...

-C, --context=number set number of context lines, before & after
-c, --count print only a count of matching lines per FILE
-D, --devices=action how to handle devices, FIFOs, and sockets
-d, --directories=action how to handle directories

...
 
Old 11-11-2022, 12:38 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,387
Blog Entries: 1

Rep: Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665
Instead of counting, you can remove them:
Code:
tr -d '\r' dos.txt >unix.txt
 
Old 11-11-2022, 02:11 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 19,248

Rep: Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525
if you want to count the lines use: wc -l
if you want to remove \r use: dos2unix
if you want to fix the grep warning issue: please provide more info, what os is it and which grep version is it, for me the command worked perfectly (did not give any warning).
you cannot use grep to find \n, because grep will split the file into lines and those lines do not contain this \n (and will search in these lines).
 
2 members found this post helpful.
Old 11-12-2022, 10:18 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,139

Rep: Reputation: 953Reputation: 953Reputation: 953Reputation: 953Reputation: 953Reputation: 953Reputation: 953Reputation: 953
Yes, a RE always ends at the end of the line, to be matched as a $.
So it would be
Code:
grep -c '\r$'
And your grep version might not recognize \r

awk works:
Code:
awk '/\r$/{++c}END{print c+0}'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Explain ark.intel.com CPU counting and linux counting? postcd Linux - Hardware 9 06-27-2014 02:24 PM
passing dos and linux newlines through the same script eamesj Programming 5 05-28-2014 10:18 AM
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 03:07 PM
Dos to unix newlines Rotwang Linux - General 3 08-22-2005 02:45 PM
A perfectionistic question: Newlines in the HCL and Reviews page Bebo LQ Suggestions & Feedback 2 05-25-2004 03:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration