将字符串连接到 bash/csh 中每个输出行的末尾

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4935278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:25:13  来源:igfitidea点击:

concatenate a string to the end of every output line in bash/csh

bashstring-concatenation

提问by SkypeMeSM

In bash command line, if I run "find . -name 'abc*' ", it prints out a list of filenames like

在 bash 命令行中,如果我运行“find . -name 'abc*'”,它会打印出一个文件名列表,例如

abc1
abc2
abc3

How can I pipe it with echo or other command, so that i get this output:

如何使用 echo 或其他命令对其进行管道传输,以便获得以下输出:

abc1   ok
abc2   ok
abc3   ok

回答by Boldewyn

find . -name 'abc*' | sed 's/$/\tok/' | column -t

sedappends the string <Tab>okto each line, and columnformats the output nicely (you can just skip this, if you don't need it).

sed将字符串附加<Tab>ok到每一行,并column很好地格式化输出(如果不需要,可以跳过它)。

回答by Tom Anderson

I tend to write:

我倾向于写:

whatever | while read line; do echo $line ok; done

That might be overkill for something this simple, but it becomes the simplest thing to do if you want to do more complicated things with the line. And it doesn't involve remembering how to make sed work!

对于这么简单的事情来说,这可能有点矫枉过正,但如果您想用这条线做更复杂的事情,这将成为最简单的事情。而且它不涉及记住如何使 sed 工作!

回答by kurumi

With GNU find,

用 GNU 查找,

find . -name "abc*" -printf "%f ok\n"

回答by Diego Torres Milano

Simply:

简单地:

$ find . -name 'abc*' | xargs -I {} echo {} OK

回答by Henno Brandsma

Pipe it through sed, is one way:

通过 sed 管道它,是一种方法:

 | sed -e 's/\(^.*\)/   ok/'

回答by crudcore

You could use xargs:

你可以使用xargs

find -iname "abc" | xargs -IREPL echo REPL ok

回答by crudcore

Have a look at the pastestandard command. Or for each element of what findshall get you manually echo the filename and the result of a function on that filename.

看看paste标准命令。或者对于find应该让您手动回显文件名和该文件名上的函数的结果的每个元素。

Can you please tell what you finally want to do?

你能告诉我你最后想做什么吗?

回答by Victor Alenkov

find . -name 'abc*' -exec echo {}' OK' \; | column -t

回答by rockeye

Use something like this :

使用这样的东西:

find . -name "abc*" -exec echo "{} - ok" \;