LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-26-2015, 05:55 PM   #1
RandyTech
Member
 
Registered: Oct 2010
Posts: 62

Rep: Reputation: 3
grep -E is broken -- cannot find end of line


This is driving me crazy. Looking here at Centos 5 but I have experienced this on various occasions, RH9 up to Centos 6.

Code:
grep ,$ /target/path/filename
grep ",$" /path/sourcefile
grep ',$' /path/sourcefile
grep -E ,$ /path/sourcefile
grep -E ",$" /path/sourcefile
grep -E ',$' /path/sourcefile
From what I understand, one of those lines should give me offending lines that end with comma. I get nothing. Googled all evening trying to find out why the $ anchor is broken. I get nothing

Same problem inside vi editor, yet I have no problem with the ^ anchor in vi or with grep.

Anyone have any clues what might cause this?

... Thanks!
 
Old 07-26-2015, 07:04 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,197

Rep: Reputation: 867Reputation: 867Reputation: 867Reputation: 867Reputation: 867Reputation: 867Reputation: 867
your regex is correct an works for me, dont know if this would matter but are your line endings unix or dos if they are dos that may make a difference.
 
Old 07-26-2015, 11:39 PM   #3
RandyTech
Member
 
Registered: Oct 2010
Posts: 62

Original Poster
Rep: Reputation: 3
Hi Keith ... thanks for the reply. How do I know if it is DOS? When I save out the file using vi "wq!", it just says the filename, line count, file size and "written". I think it would normally tell me if it was dos. Is there a grep workaround for dos file that I can try? ... Thanks.
 
Old 07-27-2015, 12:22 AM   #4
RandyTech
Member
 
Registered: Oct 2010
Posts: 62

Original Poster
Rep: Reputation: 3
Hmmm. Now finding similar issues in other posts, but evidently not same exact issue and still not sure if this is DOS file related. I used the file command and it just says: ASCII English file.
I tried grepping for these but also got no hits.
',\r'
',\n'
',\r\n'
 
Old 07-27-2015, 12:44 AM   #5
RandyTech
Member
 
Registered: Oct 2010
Posts: 62

Original Poster
Rep: Reputation: 3
I opened the file with WinVi to view it in hex mode and lines end with 0a -- not 0d0a so I have to guess this is not a DOS file issue.
 
Old 07-27-2015, 01:13 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,286

Rep: Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165
Quote:
Originally Posted by RandyTech View Post
Same problem inside vi editor,
That might imply the comma is not the last character. Any whitespace or unprintable will break that regex and be hard to see. Should be obvious in a hex dump tho' ...
Try
Code:
grep ',[[:space:]]*$'
 
Old 07-27-2015, 06:06 AM   #7
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,197

Rep: Reputation: 867Reputation: 867Reputation: 867Reputation: 867Reputation: 867Reputation: 867Reputation: 867
I checked and grep works with dos line endings anyway so its not that.
 
Old 07-27-2015, 07:08 AM   #8
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Perhaps I am being redundant, but in Vim, I always use the:
Code:
:set list
Command to show hidden characters. Maybe that will help?

Best regards,
HMW
 
Old 07-28-2015, 04:39 AM   #9
RandyTech
Member
 
Registered: Oct 2010
Posts: 62

Original Poster
Rep: Reputation: 3
Glad to see its got the seniors here stumped so far too. I'm no guru but I've been around the block a few times and this one just makes no sense at all. Good tips from everyone I was not aware of or forgot to check. Definitely no hidden character or space. I tried the ':set list' trick in vi and gave me this:
Code:
601,601,$
And that is exactly what I find using WinVI when I pull the file down and look at it here on my PC. The thing that really boggles me is the ^ anchor is working fine in both grep and vi, yet, the $ counterpart fails in both. How can they not see the $ at the end. Its clear as day (LOL)
 
Old 07-28-2015, 04:46 AM   #10
RandyTech
Member
 
Registered: Oct 2010
Posts: 62

Original Poster
Rep: Reputation: 3
Just to be clear, this is what I show in vi when *not* using the :set list
Code:
601,601,
The line definitely ends with a comma. This is hex view on that line:
Code:
36 30 31 2c  36 30 31 2c  0a
 
Old 07-28-2015, 06:05 AM   #11
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,197

Rep: Reputation: 867Reputation: 867Reputation: 867Reputation: 867Reputation: 867Reputation: 867Reputation: 867
Does sed work as expected if it too doesn't work then it's probably your file if it does then its probably your grep, try
Code:
sed -n '/,$/p'/path/to/file
 
Old 07-28-2015, 09:20 AM   #12
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,797

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Quote:
Originally Posted by Keith Hedger View Post
I checked and grep works with dos line endings anyway so its not that.
Only on MS-DOS and MS-Windows. In a Unix or Unix-like OS, grep does not try to convert line endings, and "$" will not work as expected on files with CR/LF line endings.

Tested and verified with grep 2.6.3 (CentOS 6). I have no problem matching ",$" on files with plain LF line endings. On files with CR/LF line endings, I need to use ",.$" to match lines ending with a comma. And, that's all with or without "-E" and regardless of how the ",$" is or is not quoted in the shell command line.

Last edited by rknichols; 07-28-2015 at 09:22 AM.
 
1 members found this post helpful.
Old 07-28-2015, 09:35 AM   #13
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,197

Rep: Reputation: 867Reputation: 867Reputation: 867Reputation: 867Reputation: 867Reputation: 867Reputation: 867
Quote:
Originally Posted by rknichols View Post
Only on MS-DOS and MS-Windows. In a Unix or Unix-like OS, grep does not try to convert line endings, and "$" will not work as expected on files with CR/LF line endings.

Tested and verified with grep 2.6.3 (CentOS 6). I have no problem matching ",$" on files with plain LF line endings. On files with CR/LF line endings, I need to use ",.$" to match lines ending with a comma. And, that's all with or without "-E" and regardless of how the ",$" is or is not quoted in the shell command line.
You are correct - my bad ! - I thought I had set the line endings to cr/lf but hadn't just rechecked and you are right, sorry
 
Old 07-28-2015, 10:47 AM   #14
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,226

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
What shell are you using?
 
Old 07-28-2015, 12:40 PM   #15
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,797

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Quote:
Originally Posted by smallpond View Post
What shell are you using?
bash 4.1.2
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] grep won't return a number at end of line jgrizich Linux - Newbie 7 10-15-2012 01:20 PM
I want to print a line if i dont find the pattern using grep.! how is this possible? ameylimaye Linux - Newbie 4 09-28-2011 12:57 PM
grep array add to end of line if true unixmonk Red Hat 5 01-27-2011 06:10 PM
Problem with grep matching to end of line icedown Linux - Newbie 12 09-23-2007 06:43 AM
shell script find a line and the next line (grep?) metalx1000 Programming 5 07-24-2007 09:41 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:02 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