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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
07-26-2015, 05:55 PM
|
#1
|
Member
Registered: Oct 2010
Posts: 62
Rep:
|
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!
|
|
|
07-26-2015, 07:04 PM
|
#2
|
Senior Member
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,197
|
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.
|
|
|
07-26-2015, 11:39 PM
|
#3
|
Member
Registered: Oct 2010
Posts: 62
Original Poster
Rep:
|
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.
|
|
|
07-27-2015, 12:22 AM
|
#4
|
Member
Registered: Oct 2010
Posts: 62
Original Poster
Rep:
|
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'
|
|
|
07-27-2015, 12:44 AM
|
#5
|
Member
Registered: Oct 2010
Posts: 62
Original Poster
Rep:
|
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.
|
|
|
07-27-2015, 01:13 AM
|
#6
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,286
|
Quote:
Originally Posted by RandyTech
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:]]*$'
|
|
|
07-27-2015, 06:06 AM
|
#7
|
Senior Member
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,197
|
I checked and grep works with dos line endings anyway so its not that.
|
|
|
07-27-2015, 07:08 AM
|
#8
|
Member
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
|
Perhaps I am being redundant, but in Vim, I always use the:
Command to show hidden characters. Maybe that will help?
Best regards,
HMW
|
|
|
07-28-2015, 04:39 AM
|
#9
|
Member
Registered: Oct 2010
Posts: 62
Original Poster
Rep:
|
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:
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)
|
|
|
07-28-2015, 04:46 AM
|
#10
|
Member
Registered: Oct 2010
Posts: 62
Original Poster
Rep:
|
Just to be clear, this is what I show in vi when *not* using the :set list
The line definitely ends with a comma. This is hex view on that line:
Code:
36 30 31 2c 36 30 31 2c 0a
|
|
|
07-28-2015, 06:05 AM
|
#11
|
Senior Member
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,197
|
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
|
|
|
07-28-2015, 09:20 AM
|
#12
|
Senior Member
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,797
|
Quote:
Originally Posted by Keith Hedger
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.
|
07-28-2015, 09:35 AM
|
#13
|
Senior Member
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,197
|
Quote:
Originally Posted by rknichols
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
|
|
|
07-28-2015, 10:47 AM
|
#14
|
Senior Member
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,226
|
What shell are you using?
|
|
|
07-28-2015, 12:40 PM
|
#15
|
Senior Member
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,797
|
Quote:
Originally Posted by smallpond
What shell are you using?
|
bash 4.1.2
|
|
|
All times are GMT -5. The time now is 01:02 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|