Detecting pattern at the end of a line with grep

If I do:

$ ls -R
.:
4Shared/  Cloud/

./4Shared:
UFAIZLV2R7.part3.rar

./Cloud:
UFAIZLV2R7.part2.rar.part
UFAIZLV2R7.part1.rar.part
UFAIZLV2R7.part4.rar.part

If I want to list the .rar files only, and I use grep, it will show me too the .rar.part files, what is not my wish.
I am solving this using find or ls **/*.rar as told in this thread and they work fine, but I would like to learn if it is possible to do it via grep.

I have tried (thinking about EOL):

ls -R | grep ".rarn"

with no results.
I think that the problem lies in discover if the greping is found at the end of the line, but I am not sure.

Any help out here, please?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The $ anchor matches the end of a line.

ls -R | grep '.rar$'

You can also use find for this:

find . -name '*.rar'

Method 2

In addition to your question please note that .rar does not only match “.rar” but matches every single character (including .) before the rar. In this case probably not a problem but . must be escaped in regexes.

ls -R | grep ".rar$"

Method 3

You can also instruct grep to look for your string starting at a word boundary. There is such a boundary between . (a non-word character) and r (a word character). Depending on your grep implementation, the word boundary operator can be b or possibly < or [[:<:]] (boundary left of a word only), > or [[:>:]] (right).

$ ls -R | grep 'brar$'

Example

Say I have this sample data.

$ ls -1
afile.rar
xrar
UFAIZLV2R7.part1.rar.part
UFAIZLV2R7.part2.rar.part

This command would find only the file with the .rar extension.

$ ls -R | grep 'brar$'
afile.rar

How this works?

The metacharacter b is an anchor like the caret and the dollar sign. It matches at a position that is called a “word boundary”. This match is zero-length.

Situations where this won’t work

If you have files that are named blah-rar these will get detected as well.

$ ls -R | grep 'brar$'
afile-rar
afile.rar

That’s because characters other than a alphanumerics are typically considered boundary characters, and so would slip past this approach.

Method 4

Use single quotes to make the $ work as end-of-line. If you want to grep with some variable also, use combination of double and single quotes as below:

grep "$var"'$'

My previous post was deleted saying it is duplicate. Let me explain how this is different.

The other posts mention either full use of double quotes "", or full use of single quotes ''. They both have their own limitations. Following explains it.

The problem with all double quotes is following:
grep "pattern$" gives following error: Illegal variable name.

And using all single quotes works, but if you want variable substitution, all single quotes will not work. For example:

If I have string A_BOOK, including other strings in a file FILE.

$ cat FILE
A_BOOK
B_BOOK_NOT_LAST
C_BOOK

If I set the BOOK to a variable BK

set BK = BOOK

If I grep with all double quotes, I get the following error: grep "${BK}$" FILE*: 1st $ for variable substitution, 2nd for end of pattern
(Illegal variable name).

If I grep with all single quotes, the variable substitution does not happen.
grep '${BK}$' FILE returns nothing

If I use a combination of double and single quotes, I get what I expect. Double quotes for variable substitution, and single quotes for end of pattern.

$ grep "${BK}"'$'  # << gives expected output
A_BOOK
C_BOOK

Method 5

Just do :

ls -1R -I"?" -I"??" -I"???" -I"*[!.][!r][!a][!r]"

You don’t need grep at all.

NOTE: The above works… except it still gets at least afile-rar and I don’t understand why. I’ll leave it here, but I’m not proud of it. In any case, as others have said:

find . '*.rar'

Method 6

If after following the above and nothing works, it could be due to line-endings. To fix, do: dos2unix pr0n.txt and do your grep again.

Method 7

Use pattern followed by $ in double quotes.

Example: I want to find the line that ends with semicolon in a file hey.txt

grep ";$" hey.txt

Output : prints the lines that end with semicolon.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x