Reputation: 11
I want to grep through a large file for lines not ending in "/" (no quotes). Here is a sample file:
Folder1/clicker7Mac/
Folder1/clicker7Mac/clicker7.17.0_Mac/Resources/
Folder2/
Folder2/file of interest1.pdf
Folder2/Grades/
Folder2/Grades/another file of interest.pdf
Folder2/Grades/other files with unknown but not-slash extension
Folder2/Final_exam/
Folder2/Final_exam/a 3rd file.pdf
Folder2/Grades/an excel file of interest.xlsx
Folder2/Grades/Package_for_someone/
Folder2/HW/
Folder2/HW/HW1/
I want only the lines ending in files, not the lines that are directory paths. I.e. here I want output:
Folder2/file of interest1.pdf
Folder2/Grades/another file of interest.pdf
Folder2/Grades/other files with unknown but not-slash extension
Folder2/Final_exam/a 3rd file.pdf
Folder2/Grades/an excel file of interest.xlsx
I've searched stack exchange and unix forums and have tried the following (note I have inverted the search because it's easier to test):
grep '.*/' testfile
grep '\w*/\b' testfile
(greps all words and slashes, takes files as well; grep -v yeilds nothing)
grep "[a-z.0-9]"'/\b' testfile
(last character before all slashes; grep -v yeilds nothing)
grep '\l*/\b' testfile
grep '\>/' testfile
(all slashes (not just the end slash); grep -v yeilds nothing)
grep -F '/\b' testfile
(fgrep or grep -F treats as literal string and yeilds nothing; grep -v yeilds everything)
Any solutions please?
Upvotes: 1
Views: 148
Reputation: 41460
If awk
is an option:
awk -F'/' '$NF' file
Folder2/file of interest1.pdf
Folder2/Grades/another file of interest.pdf
Folder2/Grades/other files with unknown but not-slash extension
Folder2/Final_exam/a 3rd file.pdf
Folder2/Grades/an excel file of interest.xlsx
It sets the Field Separator to /
and then test if last field contains some thing and then do default action, print the line.
Another variation more like the grep
solution, test if line does not end with /
:
awk '!/[/]$/' file
Folder2/file of interest1.pdf
Folder2/Grades/another file of interest.pdf
Folder2/Grades/other files with unknown but not-slash extension
Folder2/Final_exam/a 3rd file.pdf
Folder2/Grades/an excel file of interest.xlsx
Upvotes: 1
Reputation: 12528
Use grep -v
to exclude all lines that end with /
:
$ grep -v '/$' testfile
Folder2/file of interest1.pdf
Folder2/Grades/another file of interest.pdf
Folder2/Grades/other files with unknown but not-slash extension
Folder2/Final_exam/a 3rd file.pdf
Folder2/Grades/an excel file of interest.xlsx
-v
is portable and specified by POSIX.
Upvotes: 1
Reputation: 88879
grep -v '/$' file
or
grep '[^/]$' file
Output:
Folder2/file of interest1.pdf Folder2/Grades/another file of interest.pdf Folder2/Grades/other files with unknown but not-slash extension Folder2/Final_exam/a 3rd file.pdf Folder2/Grades/an excel file of interest.xlsx
See: man grep
and The Stack Overflow Regular Expressions FAQ
Upvotes: 1