如何在bash中从文件中剪切一些文本以指定符号并写入另一个文件?

如何在bash中从文件中剪切一些文本以指定符号并写入另一个文件?,bash,Bash,我在文件中有一个文本,如下所示 - $0 - The name of the Bash script. - $# - How many arguments were passed to the Bash script. - $@ - All the arguments supplied to the Bash script. - $USER - The username of the user running the script. - $SECONDS - The number of seco

我在文件中有一个文本,如下所示

- $0 - The name of the Bash script.
- $# - How many arguments were passed to the Bash script.
- $@ - All the arguments supplied to the Bash script.
- $USER - The username of the user running the script.
- $SECONDS - The number of seconds since the script was started.

如何剪切所有以“$”开头、以“-”结尾的文本,并将其保存到另一个文件中。如何在bash脚本中实现它?

使用GNU grep,您可以使用

grep -Po '\$.*?(?= -)' inputFile > outputFile
perl -nle 'print $& if m{\$.*?(?= -)}' inputFile > outputFile
如果您没有GNU grep(例如在Mac OS上),您可以使用

grep -Po '\$.*?(?= -)' inputFile > outputFile
perl -nle 'print $& if m{\$.*?(?= -)}' inputFile > outputFile

thx,但它对我不起作用=(我所需要的一切都写在我的问题中。在使用你的命令后,我得到了这个错误。如果我将“P”改为“P”,没有任何错误,但它仍然不起作用。-用法:grep[-abcdefghhiijllmnooqrsussuvvwxz][A num][-B num][-C[num]][-e pattern][-f file][-binary files=value][-color=when][-context[-num directories=action][-label][--line buffered][--null][pattern][file…]谢谢,现在它工作了!