从文本文件中的多行中剪切键入的行号

Cut Typed Line Numbers From Multiple Lines in a Text File

我有一个长文本文件,文件中列出的每个路径前面都有数字,如下例所示:

1) /some/path/here/file.txt
1) /some/path/here/1file.edf
2) /some/another_path/here/2file.txt
3) /some/other_path/here/3file.txt
3) /some/other_path/here/4file.edf
3) /some/other_path/here/5file.edf
...

此文件持续了数千行。我要做的是从每一行的第一部分中删除数字,这样我就可以 tar 文件列表而不会受到数字的干扰。有没有一种方法可以使用 Shell Emacs 命令来做到这一点?

正则表达式是您的朋友。您可以在 emacs 或命令行中执行此操作。在 emacs 中,如果您按 C-M-%(control+alt+shift+5)或 运行 query-replace-regexp,它会提示您查找一个正则表达式,并提示您替换另一个正则表达式。如果你使用 ^[0-9]*)(注意这个末尾有一个 space),然后将 "repalce" 留空,你将替换数字、paren 和 space 什么都没有。如果您从文件的顶部开始,您可以键入 ! 来替换所有内容,或者先一个一个地检查它是否正常工作。

在正则表达式中,^匹配行首[0-9]表示匹配0到9范围内的单个字符,*表示"match any number of the previous thing",然后 ) 匹配文字括号(在 emacs 中这是默认设置,在 shell 中你可能需要使用 \).

来转义它

类似于:

cat list.txt | cut -f 2 -d' ' | xargs tar czf archive.tar.gz