Very new to UNIX but not new to programming. Using Terminal on MacBook. For the purposes of managing and searching word lists for crossword construction, I'm trying to get handy with the Grep command and its variations. Seems pretty straightforward but getting hung up early on with what I thought should be a simple case.
When I enter
grep "^COW" masternospaces.txt
I get what I want: a list of all the words starting with COW.
But when I enter
grep "COW$" masternospaces.txt
I expect to get a list of words ending with COW (there are many such words), and nothing is returned at all.
The file is a plain text file, with every line just a word (or a word phrase with no spaces) in all caps.
Any idea what could be happening here?
Best Answer
As @steeldriver mentionned, the problem is likely to be caused by a different line ending style than what
grep
is expecting.To check the line endings
You can use
hexdump
to check exactly how your line endings are formatted. I suggest you use my favorite format :With the output, check the line endings :
0a
->LF
,0d
->CR
. A very quick example would give something like this :Note the line endings in dos format :
0d 0a
.To change the line endings
You can see here or here for various methods of changing line endings using various tools, but for a one-time thing, you could always use vi/vim :
To grep without changing anything
If you just want
grep
to match no matter the line ending, you could always specify line endings like this :If a blank line is shown, you can check that you indeed matched something by using the
-v
option ofcat
:My personal favorite
You could also both grep and standardize the output using
sed
:where
^M
is obtained by typingCtrl-V Ctrl-M
on your keyboard.Hope this helps!