bash 如何在bash中将一行拆分为由一个或多个空格分隔的单词?

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

How to split a line into words separated by one or more spaces in bash?

bash

提问by asdf

I realize how to do it in python, just with

我意识到如何在 python 中做到这一点,只需使用

line = db_file.readline()
ll=string.split(line)

but how can I do the same in bash? is it really possible to do it in a so simple way?

但是我怎么能在 bash 中做同样的事情呢?真的有可能以如此简单的方式做到吗?

回答by ZoogieZork

s='foo bar baz'
a=( $s )
echo ${a[0]}
echo ${a[1]}
...

回答by xioxox

If you want a specific word from the line, awk might be useful, e.g.

如果您想要该行中的特定单词,则 awk 可能很有用,例如

$ echo $LINE | awk '{print }'

Prints the second whitespace separated word in $LINE. You can also split on other characters, e.g.

在 $LINE 中打印第二个空格分隔的单词。您还可以拆分其他字符,例如

$ echo "5:6:7" | awk -F: '{print }'
6

回答by Alok Singhal

It depends upon what you mean by split. If you want to iterate over words in a line, which is in a variable, you can just iterate. For example, let's say the variable lineis this is a line. Then you can do this:

这取决于你所说的split是什么意思。如果你想迭代一行中的单词,它在一个变量中,你可以迭代。例如,假设变量linethis is a line。然后你可以这样做:

for word in $line; do echo $word; done

This will print:

这将打印:

this
is
a
line

for .. in $varsplits $varusing the values in $IFS, the default value of which means "split blanks and newlines".

for .. in $var$var使用 中的值进行拆分$IFS,其默认值表示“拆分空格和换行符”。

If you want to read lines from user or a file, you can do something like:

如果要从用户或文件中读取行,可以执行以下操作:

cat $filename | while read line
do
    echo "Processing new line" >/dev/tty
    for word in $line
    do
        echo $word
    done
done

For anything else, you need to be more explicit and define your question in more detail.

对于其他任何事情,您都需要更明确并更详细地定义您的问题。

Note: Edited to remove bashism, but I still kept cat $filename | ...because I like it more than redirection.

注意:编辑以删除 bashism,但我仍然保留,cat $filename | ...因为我更喜欢它而不是重定向。

回答by harshvchawla

echo $line | tr " " "\n"

gives the output similar to those of most of the answers above; without using loops.


In your case, you also mention ll=<...output...>,
so, (given that I don't know much python and assuming you need to assign output to a variable),

给出与上述大多数答案相似的输出;不使用循环。


在您的情况下,您还提到了ll=<...output...>
所以,(鉴于我对 python 了解不多,并假设您需要将输出分配给一个变量),

ll=`echo $line | tr " " "\n"`

should suffice (remember to echo "$ll"instead of echo $ll)

应该足够了(记住echo "$ll"而不是echo $ll

回答by Paused until further notice.

$ line="these are words"
$ ll=($line)
$ declare -p ll  # dump the array
declare -a ll='([0]="these" [1]="are" [2]="words")'
$ for w in ${ll[@]}; do echo $w; done
these
are
words

回答by ghostdog74

do this

做这个

while read -r line
do
  set -- $line
  echo " "
done <"file"

$1, $2 etc will be your 1st and 2nd splitted "fields". use $@ to get all values..use $# to get length of the "fields".

$1、$2 等将是您的第一个和第二个拆分的“字段”。使用 $@ 获取所有值..使用 $# 获取“字段”的长度。

回答by cturiel

More simple,

更简单,

echo $line | sed 's/\s/\n/g'

\s --> whitespace character (space, tab, NL, FF, VT, CR). In many systems also valid [:space:]

\n --> new line

\s --> 空白字符(空格、制表符、NL、FF、VT、CR)。在许多系统中也有效 [:space:]

\n --> 换行

回答by Ignacio Vazquez-Abrams

The -aoption of readwill allow you to split a line read in by the characters contained in $IFS.

-a选项read将允许您拆分由包含在$IFS.

回答by Phil Miller

If you already have your line of text in a variable $LINE, then you should be able to say

如果你已经在变量 $LINE 中有你的文本行,那么你应该能够说

for L in $LINE; do
   echo $L;
done