在 Linux shell 中,如何处理多行字符串的每一行?

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

In a Linux shell how can I process each line of a multiline string?

linuxshell

提问by Yazz.com

While in a Linux shell I have a string which has the following contents:

在 Linux shell 中,我有一个字符串,其中包含以下内容:

cat
dog
bird

and I want to pass each item as an argument to another function. How can I do this?

我想将每个项目作为参数传递给另一个函数。我怎样才能做到这一点?

采纳答案by osgx

Use this (it is loop of reading each line from file file)

使用这个(它是从文件中读取每一行的循环file

cat file | while read -r a; do echo $a; done

where the echo $ais whatever you want to do with current line.

哪里echo $a是你想对当前行做的任何事情。

UPDATE: from commentators (thanks!)

更新:来自评论员(谢谢!)

If you have no file with multiple lines, but have a variable with multiple lines, use

如果您没有包含多行的文件,但有包含多行的变量,请使用

echo "$variable" | while read -r a; do echo $a; done

UPDATE2: "read -r" is recommended to disable backslashed (\) chars interpretation (check mtraceur comments; supported in most shells). It is documented in POSIX 1003.1-2008 http://pubs.opengroup.org/onlinepubs/9699919799/utilities/read.html

UPDATE2: " read -r" 建议禁用反斜杠 ( \) 字符解释(检查 mtraceur 注释;在大多数 shell 中支持)。它记录在 POSIX 1003.1-2008 http://pubs.opengroup.org/onlinepubs/9699919799/utilities/read.html

By default, unless the -r option is specified, <backslash>shall act as an escape character. .. The following option is supported: -r- Do not treat a <backslash>character in any special way. Consider each to be part of the input line.

默认情况下,除非指定了 -r 选项,否则<backslash>应充当转义字符。.. 支持以下选项:-r- 不要<backslash>以任何特殊方式对待字符。将每个都视为输入行的一部分。

回答by moinudin

Use readwith a while loop:

使用readwhile循环:

while read line; do
    echo $line;
done

回答by kmkaplan

Just pass your string to your function:

只需将您的字符串传递给您的函数:

function my_function
{
    while test $# -gt 0
    do
        echo "do something with "
        shift
    done
}
my_string="cat
dog
bird"
my_function $my_string

gives you:

给你:

do something with cat
do something with dog
do something with bird

And if you really care about other whitespaces being taken as argument separators, first set your IFS:

如果你真的关心其他空格被当作参数分隔符,首先设置你的IFS

IFS="
"
my_string="cat and kittens
dog
bird"
my_function $my_string

to get:

要得到:

do something with cat and kittens
do something with dog
do something with bird

Do not forget to unset IFSafter that.

之后不要忘记unset IFS

回答by glenn Hymanman

if you use bash, setting IFS is all you need:

如果你使用 bash,你只需要设置 IFS:

$ x="black cat
brown dog
yellow bird"
$ IFS=$'\n'
$ for word in $x; do echo "$word"; done
black cat
brown dog
yellow bird

回答by ksb

Use xargs:

使用xargs

Depending on what you want to do with each line, it could be as simple as:

根据您想对每一行做什么,它可能很简单:

xargs -n1 func < file

or more complicated using:

或更复杂的使用:

cat file | xargs -n1 -I{} func {}

回答by Wangwang

Do like this:

这样做:

multrs="some multiple line string ...
...
..."

while read -r line; do
    echo $line;
done <<< "$mulstrs"

Variable $mulstrs must be enclosedin double quotes, otherwise spaces or carriage returns will interfere with the calculation.

变量$ mulstrs必须双引号,否则空格或者回车将与计算干扰。