Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unix 如何grep(不)匹配每行末尾的特殊字符“/”(无引号)_Unix_Search_Grep_Special Characters - Fatal编程技术网

Unix 如何grep(不)匹配每行末尾的特殊字符“/”(无引号)

Unix 如何grep(不)匹配每行末尾的特殊字符“/”(无引号),unix,search,grep,special-characters,Unix,Search,Grep,Special Characters,我想在一个大文件中搜索不以/无引号结尾的行。以下是一个示例文件: 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-

我想在一个大文件中搜索不以/无引号结尾的行。以下是一个示例文件:

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/
我只想要以文件结尾的行,而不是作为目录路径的行。也就是说,这里我想要输出:

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
我搜索了stack exchange和unix论坛,并尝试了以下注意事项:我已反转搜索,因为它更易于测试:

grep  '.*/' testfile
grep  '\w*/\b' testfile
greps所有的单词和斜杠,以及获取文件;grep-v什么都不想

grep  "[a-z.0-9]"'/\b' testfile
grep  '\l*/\b' testfile
grep  '\>/' testfile
grep -F  '/\b' testfile
所有斜杠前的最后一个字符;grep-v什么都不想

grep  "[a-z.0-9]"'/\b' testfile
grep  '\l*/\b' testfile
grep  '\>/' testfile
grep -F  '/\b' testfile
所有的斜线不仅仅是结束斜线;grep-v什么都不想

grep  "[a-z.0-9]"'/\b' testfile
grep  '\l*/\b' testfile
grep  '\>/' testfile
grep -F  '/\b' testfile
fgrep或grep-F将其视为文本字符串,不包含任何内容;格雷普喜欢一切

有什么解决办法吗

grep -v '/$' 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 看:男人格雷普和

输出:

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 请参见:man grep和

使用grep-v排除以/结尾的所有行:

-v是可移植的,并由指定。

使用grep-v排除以/结尾的所有行:

-如果awk是一个选项,则v是可移植的,并由指定。

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
它将字段分隔符设置为/然后测试最后一个字段是否包含某些内容,然后执行默认操作,打印该行

另一个更像grep解决方案的变体是,测试行是否以/结尾:

如果awk是一个选项:

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
它将字段分隔符设置为/然后测试最后一个字段是否包含某些内容,然后执行默认操作,打印该行

另一个更像grep解决方案的变体是,测试行是否以/结尾:


是的,谢谢-就是这样!很明显,我不知道行尾的$flag,所以也谢谢你的链接。是的,谢谢-就是这样!很明显,我不知道行尾$flag,所以也谢谢你给我这个链接。是的,谢谢你,这很有效,我不知道的是行尾$flag。我需要阅读更多关于正则表达式的内容。是的,谢谢,这很有效,我不知道的是$end-of-line标志。我需要读更多关于正则表达式的书。