bash 为bash中每行的每个单词添加前缀

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38263274/
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-18 14:52:10  来源:igfitidea点击:

Add prefix to each word of each line in bash

bashsedgrep

提问by Christopher Loen

I have a variable called deps:

我有一个名为 deps 的变量:

deps='word1 word2'

I want to add a prefix to each word of the variable.

我想为变量的每个单词添加一个前缀。

I tried with:

我试过:

echo $deps | while read word do \ echo "prefix-$word" \ done

but i get:

但我得到:

bash: syntax error near unexpected token `done'

bash:意外标记“完成”附近的语法错误

any help? thanks

有什么帮助吗?谢谢

回答by SLePort

With sed :

使用 sed :

$ deps='word1 word2'
$ echo "$deps" | sed 's/[^ ]* */prefix-&/g'
prefix-word1 prefix-word2

回答by Heinrich Hartmann

For well behaved strings, the best answer is:

对于表现良好的字符串,最佳答案是:

printf "prefix-%s\n" $deps

as suggested by 123 in the comments to fedorqui's answer.

正如 123 在对 fedorqui 的回答的评论中所建议的那样。

Explanation:

解释:

  • Without quoting, bash will split the contents of $depsaccording to $IFS(which defaults to " \n\t") before calling printf
  • printfevaluates the pattern for each of the provided arguments and writes the output to stdout.
  • printfis a shell built-in (at least for bash) and does not fork another process, so this is faster than sed-based solutions.
  • 如果不引用,bash 将在调用之前$deps根据$IFS(默认为" \n\t")拆分内容printf
  • printf评估每个提供的参数的模式并将输出写入标准输出。
  • printf是一个内置的 shell(至少对于 bash)并且不会分叉另一个进程,所以这比基于 sed 的解决方案更快。

回答by codeling

In another questionI just came across the markers for beginning (\<) and end (\>) of words. With those you can shorten the solution of SLePortabove somewhat. The solution also nicely extends to appending a suffix, which I needed in addition to the prefix, but couldn't figure out how to use above solution for it, as the &also includes the possible trailing whitespace after the word.

另一个问题中,我刚刚遇到了单词开头 ( \<) 和结尾 ( \>) 的标记。有了这些你可以稍微缩短上面的SLePort的解决方案。该解决方案还很好地扩展到附加后缀,除了前缀之外我还需要它,但无法弄清楚如何使用上述解决方案,因为&它还包括单词后面可能的尾随空格。

So my solution is this:

所以我的解决方案是这样的:

$ deps='word1 word2'

# add prefix:
$ echo "$deps" | sed 's/\</prefix-/g'
prefix-word1 prefix-word2

# add suffix:
$ echo "$deps" | sed 's/\>/-suffix/g'
word1-suffix word2-suffix

Explanation:\<matches the beginning of every word, and \>matches the end of each word. You can simply "replace" these by the prefix/suffix, resulting in them being prepended/appended. There is no need to reference them anymore in the replacement, as these are not "real" characters anyway!

说明:\<匹配每个单词的开头,\>匹配每个单词的结尾。您可以简单地用前缀/后缀“替换”它们,从而使它们被前置/附加。无需在替换中再引用它们,因为无论如何这些都不是“真实”字符!

回答by fedorqui 'SO stop harming'

You can read the string into an array and then prepend the string to every item:

您可以将字符串读入数组,然后将字符串添加到每个项目中:

$ IFS=' ' read -r -a myarray <<< "word1 word2"
$ printf "%s\n" "${myarray[@]}"
word1
word2
$ printf "prefix-%s\n" "${myarray[@]}"
prefix-word1
prefix-word2