首页 > 解决方案 > 如何将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 脚本中做到这一点?

标签: bash

解决方案


使用 GNU grep,您可以使用

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

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

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

推荐阅读