bash 如何从bash读取整行

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

How to read entire line from bash

bashshellparsinglines

提问by webminal.org

I have a file file.txtwith contents like

我有一个file.txt包含类似内容的文件

i love this world

I hate stupid managers
I love linux

I have MS

When I do the following:

当我执行以下操作时:

for line in `cat file.txt`; do
echo $line
done

It gives output like

它给出了类似的输出

I
love
this
world
I
..
..

But I need the output as entire lines like below — any thoughts ?

但是我需要像下面这样的整行输出 - 有什么想法吗?

i love this world

I hate stupid managers
I love linux

I have MS

回答by SiegeX

while read -r line; do echo "$line"; done < file.txt

回答by tobyodavies

As @Zac noted in the comments, the simplest solution to the question you post is simply cat file.txtso i must assume there is something more interesting going on so i have put the two options that solve the question as asked as well:

正如@Zac 在评论中指出的那样,您发布的问题的最简单解决方案很简单,cat file.txt所以我必须假设有更有趣的事情发生,所以我也提出了解决问题的两个选项:

There are two things you can do here, either you can set IFS(Internal Field Separator) to a newline and use existing code, or you can use the reador linecommand in a while loop

您可以在这里做两件事,您可以将IFS(Internal Field Separator) 设置为换行符并使用现有代码,或者您可以在 while 循环中使用readorline命令

IFS="
"

or

或者

(while read line ; do
    //do something
 done) < file.txt

回答by Dave

I believe the question was how to read in an entire line at a time. The simple script below will do this. If you don't specify a variable name for "read" it will stuff the entire line into the variable $REPLY.

我相信问题是如何一次阅读整行。下面的简单脚本将执行此操作。如果你没有为“read”指定一个变量名,它会将整行填充到变量 $REPLY 中。

cat file.txt|while read; do echo $REPLY; done

cat file.txt|读取时;做回声 $REPLY; 完毕

Dave..

戴夫..

回答by thatbrentguy

You can do it by using read if the file is coming into stdin. If you need to do it in the middle of a script that already uses stdin for other purposes, you can temporarily reassign the stdin file descriptor.

如果文件进入标准输入,您可以使用 read 来完成。如果您需要在已经将 stdin 用于其他目的的脚本中间执行此操作,您可以临时重新分配 stdin 文件描述符。

#!/bin/bash
file= 

# save stdin to usually unused file descriptor 3
exec 3<&0

# connect the file to stdin
exec 0<"$file"

# read from stdin 
while read -r line
do
    echo "[$line]"
done

# when done, restore stdin
exec 0<&3

回答by Ulrich Schwarz

Try

尝试

(while read l; do echo $l; done) < temp.txt

read: Read a line from the standard input and split it into fields.

Reads a single line from the standard input, or from file descriptor FD if the -u option is supplied. The line is split into fields as with word splitting, and the first word is assigned to the first NAME, the second word to the second NAME, and so on, with any leftover words assigned to the last NAME. Only the characters found in $IFS are recognized as word delimiters.

读取:从标准输入中读取一行并将其拆分为字段。

如果提供了 -u 选项,则从标准输入或从文件描述符 FD 中读取一行。与分词一样,该行被分成多个字段,第一个词分配给第一个 NAME,第二个词分配给第二个 NAME,依此类推,剩余的词分配给最后一个 NAME。只有在 $IFS 中找到的字符才会被识别为单词分隔符。