bash 如何从文件中读取单词(而不是行)?

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

How can I read words (instead of lines) from a file?

bash

提问by maniat1k

I've read this questionabout how to read n characters from a text file using bash. I would like to know how to read a word at a time from a file that looks like:

我已经阅读了有关如何使用 bash 从文本文件中读取 n 个字符的问题。我想知道如何从如下文件中一次读取一个单词:

example text
example1 text1
example2 text2
example3 text3

Can anyone explain that to me, or show me an easy example? Thanks!

谁能向我解释一下,或者给我一个简单的例子?谢谢!

采纳答案by Some programmer dude

The readcommand by default reads whole lines. So the solution is probably to read the whole line and then split it on whitespace with e.g. for:

read命令默认读取整行。因此,解决方案可能是读取整行,然后将其拆分为空白,例如for

while read line; do
    for word in $line; do
        echo "word = '$word'"
    done
done

回答by Todd A. Jacobs

The way to do this with standard input is by passing the -aflag to read:

使用标准输入执行此操作的方法是将-a标志传递给读取:

read -a words
echo "${words[@]}"

This will read your entire line into an indexed array variable, in this case named words. You can then perform any array operations you like on wordswith shell parameter expansions.

这会将您的整行读入一个索引数组变量,在本例中名为words。然后,您可以对带有shell 参数扩展的单词执行您喜欢的任何数组操作。

For file-oriented operations, current versions of Bash also support the mapfile built-in. For example:

对于面向文件的操作,当前版本的 Bash 还支持内置的 mapfile 。例如:

mapfile < /etc/passwd
echo ${MAPFILE[0]}

Either way, arrays are the way to go. It's worth your time to familiarize yourself with Bash array syntaxto make the most of this feature.

无论哪种方式,数组都是要走的路。花时间熟悉Bash 数组语法以充分利用此功能是值得的。

回答by Paused until further notice.

Ordinarily, you should read from a file using a while read -r lineloop. To do this and parse the words on the lines requires nesting a forloop inside the whileloop.

通常,您应该使用while read -r line循环从文件中读取。要做到这一点并解析行上的单词,需要在for循环内嵌套一个while循环。

Here is a technique that works without requiring nested loops:

这是一种无需嵌套循环即可工作的技术:

for word in $(<inputfile)
do
    echo "$word"
done

回答by Charles Duffy

In the context given, where the number of words is known:

在给定的上下文中,在已知单词数的情况下:

while read -r word1 word2 _; do
  echo "Read a line with word1 of $word1 and word2 of $word2"
done

If you want to read each line into an array, read -awill put the first word into element 0 of your array, the second into element 1, etc:

如果要将每一行读入数组,read -a请将第一个单词放入数组的元素 0,将第二个放入元素 1,依此类推:

while read -r -a words; do
  echo "First word is ${words[0]}; second word is ${words[1]}"
  declare -p words # print the whole array
done

回答by eocanha

In bash, just use space as delimiter (read -d ' '). This method requires some preprocessing to translate newlines into spaces (using tr) and to merge several spaces into a single one (using sed):

在 bash 中,只需使用空格作为分隔符 ( read -d ' ')。此方法需要进行一些预处理以将换行符转换为空格(使用tr)并将多个空格合并为一个(使用sed):

{
 tr '\n' ' ' | sed 's/  */ /g' | while read -d ' ' WORD
 do
  echo -n "<${WORD}> "
 done
 echo
} << EOF
Here you have some words, including * wildcards
that don't get expanded,
multiple   spaces   between   words,
    and lines with spaces at the begining.
EOF

The main advantage of this method is that you don't need to worry about the array syntax and just work as with a forloop, but without wildcard expansion.

这种方法的主要优点是您无需担心数组语法,只需像for循环一样工作,无需通配符扩展。

回答by Mr Kashyap

This can be done using AWK too:

这也可以使用 AWK 来完成:

awk '{for(i=1;i<=NF;i++) {print $i}}' text_file

awk '{for(i=1;i<=NF;i++) {print $i}}' text_file

回答by Michele Pinamonti

I came across this question and the proposed answers, but I don't see listed this simple possibile solution:

我遇到了这个问题和建议的答案,但我没有看到列出这个简单的可能解决方案:

for word in `cat inputfile`
do
  echo $word
done