bash读取循环仅读取输入变量的第一行 [英] bash read loop only reading first line of input variable

查看:130
本文介绍了bash读取循环仅读取输入变量的第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个读取循环,该循环正在读取变量,但未达到我期望的方式。我想读取变量的每一行并处理每一行。这是我的循环:

I have a read loop that is reading a variable but not behaving the way I expect. I want to read every line of my variable and process each one. Here is my loop:

while read -r line
do
   echo $line | sed 's/<\/td>/<\/td>$/g' | cut -d'$' -f2,3,4 >> file.txt
done <<< "$TABLE"

我希望它能够处理文件的每一行,但它只会执行第一行一。如果我的中间只是回声$ line >> file.txt,它将按预期工作。这里发生了什么?如何获得所需的行为?

I expect it to process every line of the file but instead it just does the first one. If my the middle is simply echo $line >> file.txt it works as expected. What's going on here? How do I get the behavior I want?

推荐答案

似乎您的行由 \r分隔而不是 \n

It seems your lines are delimited by \r instead of \n.

使用此while循环使用 read -d $'\r'来迭代输入:

Use this while loop to iterate the input with use of read -d $'\r':

while read -rd $'\r' line; do
    echo "$line" | sed 's~</td>~</td>$~g' | cut -d'$' -f2,3,4 >> file.txt
done <<< "$TABLE"

这篇关于bash读取循环仅读取输入变量的第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆