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 seconds since the script was started.
如何剪切从“$”开始并在“-”之前结束的所有文本并将其保存到另一个文件。我如何在 bash 脚本中做到这一点?
解决方案
使用 GNU grep,您可以使用
grep -Po '\$.*?(?= -)' inputFile > outputFile
如果您没有 GNU grep(例如在 Mac OS 上),您可以使用
perl -nle 'print $& if m{\$.*?(?= -)}' inputFile > outputFile
推荐阅读
- excel - VBA - 如何将变量输出到excel中的特定行和列?
- hive - 使用数组查询配置单元表
> 类型 - python - OSError: [Errno 9] 使用 PIL 保存图像时文件描述符错误
- reactjs - 得到错误期望一个赋值或函数调用,而是看到一个表达式 no-unused-expressions
- javascript - 将相同的响应附加到多个听众时得到奇怪的结果
- python - 如何使用 '?' 在python 中提取两个匹配模式之间的可选子字符串?
- javascript - jQuery选择数据属性并隐藏
- android - 如何使用 Chrome 自定义选项卡实现 OAuth 单点登录/注销
- css - 在 CSS 下落动画中创建类似重力的效果并在使用 rotateX 时计算“圆”的半径变化
- email - Chilkat - 重复附件