我有一个名为 test 的文件,其中包含以下几行:
1. Lil Nas X Ft, Billy Ray Cyrus - Old Town Road (Remix).mp3
10. Mark Ronson - Find U Again (feat. Camila Cabello).mp3
11. Mabel - Mad Love.mp3
12. Sigala & Becky Hill - Wish You Well.mp3
13. CamelPhat & Jake Bugg - Be Someone.mp3
14. Jess Glynne & Jax Jones - One Touch.mp3
15. Zara Larsson - Don't Worry Bout Me.mp3
16. Pink Ft, Cash Cash - Can We Pretend.mp3
17. Jax Jones & Martin Solveig & Madison Beer - All Day And Night (Jax Jones & Martin Solveig Present Europa).mp3
18. Martin Garrix - Summer Days (feat. Macklemore & Patrick Stump of Fall Out Boy).mp3
19. Little Mix - Bounce Back.mp3
我想使用 1. 代表歌曲名称来 grep 第一列。我正在使用的命令是:
#cat test | grep -w "1."
1. Lil Nas X Ft, Billy Ray Cyrus - Old Town Road (Remix).mp3
10. Mark Ronson - Find U Again (feat. Camila Cabello).mp3
11. Mabel - Mad Love.mp3
12. Sigala & Becky Hill - Wish You Well.mp3
13. CamelPhat & Jake Bugg - Be Someone.mp3
14. Jess Glynne & Jax Jones - One Touch.mp3
15. Zara Larsson - Don't Worry Bout Me.mp3
16. Pink Ft, Cash Cash - Can We Pretend.mp3
17. Jax Jones & Martin Solveig & Madison Beer - All Day And Night (Jax Jones & Martin Solveig Present Europa).mp3
18. Martin Garrix - Summer Days (feat. Macklemore & Patrick Stump of Fall Out Boy).mp3
19. Little Mix - Bounce Back.mp3
一切似乎都在酝酿之中。但我应该只看到以下内容:
1. Lil Nas X Ft, Billy Ray Cyrus - Old Town Road (Remix).mp3
答案1
即使作为固定字符串,1.
也会匹配11.
.但它不是字符串,而是正则表达式,因此某些字符应用规则而不是匹配文字。
点是“的替代品”任何字符"; 要匹配文字点,您需要将其转义为\.
。您还可以包含行首标记(插入符号,^
),以便1.
仅在行开头匹配。
您还可以放弃cat
因为grep
(和许多其他命令)直接接受文件名。
然后你会得到这样的东西
grep '^1\.' test
您的数据集中仅匹配一行
1. Lil Nas X Ft, Billy Ray Cyrus - Old Town Road (Remix).mp3