【问题标题】:Append string on grep multiple results with variable in a single command在单个命令中使用变量在 grep 多个结果上附加字符串
【发布时间】:2023-03-19 14:30:02
【问题描述】:

文件名包含空格,例如example like this.txt 我想在该字符串的前缀和后缀上附加"。例如"example like this.txt".

此外,我还想在 grep 结果的每一行附加数字

例如,此命令将返回几行:

ls -a | grep "filename"

例如:

example filename a.txt
example filename b.txt
example filename c.txt
example filename d.txt

如何使用单个命令在每个返回行上附加多个字符串?这样我就得到了这个输出:

1 "example filename a.txt"
2 "example filename b.txt"
3 "example filename c.txt"
4 "example filename d.txt"

【问题讨论】:

  • 这些数字在您的预期输出中应该来自哪里?

标签: linux bash perl awk grep


【解决方案1】:

如果您需要单个命令,则需要使用 awkperlpython 之类的命令:

例如:

$ ls -la | awk '/filename/{printf "%s %s%s%s\n",++i,q,$0,q}' q='"' 
1 "example filename a.txt"
2 "example filename b.txt"
3 "example filename c.txt"
4 "example filename d.txt"

【讨论】:

    猜你喜欢
    • 2015-01-03
    • 2022-10-05
    • 2014-04-08
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 2018-06-27
    • 2013-03-05
    • 2015-12-04
    相关资源
    最近更新 更多