如何将bash文件中的某些文本切成特定的符号并写入另一个文件?

时间:2018-11-07 17:00:06

标签: 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 seconds since the script was started.

我如何剪切所有从“ $”开始到“-”之前结束的文本并将其保存到另一个文件。如何在bash脚本中做到这一点?

1 个答案:

答案 0 :(得分:0)

使用GNU grep,您可以使用

grep -Po '\$.*?(?= -)' inputFile > outputFile

如果您没有GNU grep(例如在Mac OS上),则可以使用

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