回声将我的选项卡更改为空格 [英] Echo changes my tabs to spaces

查看:77
本文介绍了回声将我的选项卡更改为空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从网上获取以下结构作为如何从BASH中读取文件的基本示例:

I'm taking the following structure from around the net as a basic example of how to read from a file in BASH:

cat inputfile.txt | while read line; do echo $line; done

我的inputfile.txt是制表符分隔的,上面的行命令是用空格分隔的。

My inputfile.txt is tab-delimited, though, and the lines that come out of the above command are space-delimited.

这在我的实际应用程序中引起了我的问题,这当然比上面的更为复杂:我想接一行,生成一些基于它的新内容,然后输出原始行以及新内容作为额外字段。并且如果没有一堆 cut -d'' sed -e's / / \t / g,管道将变得非常复杂。 '(对于使用制表符分隔的数据包含空格来说,这绝对不安全)。

This is causing me problems in my actual application, which is of course more complex than the above: I want to take the line, generate some new stuff based on it, and then output the original line plus the new stuff as extra fields. And the pipeline is going to be complicated enough without a bunch of cut -d ' ' and sed -e 's/ /\t/g' (which wouldn't be safe for tab-delimited data containing spaces anyway).

我看过IFS解决方案,但它们在这种情况下似乎无济于事。我想要的是OFS ...除了我在回声,而不是awk!我认为,如果我能得到回声来吐出我所给的东西,那么我会保持良好状态。有什么想法吗?谢谢!

I've looked at IFS solutions, but they don't seem to help in this case. What I want is an OFS...except that I'm in echo, not awk! I think that if I could just get echo to spit out what I gave it, verbatim, I'd be in good shape. Any thoughts? Thanks!

推荐答案

尝试:

cat inputfile.txt | while read line; do echo "$line"; done

相反。

,而不是 read 代替标签,而是 echo

In other words, it's not read replacing the tabs, it's echo.

请参阅以下成绩单(使用<<< tab>> ,其中选项卡位于其中):

See the following transcript (using <<tab>> where the tabs are):

pax$ echo 'hello<<tab>>there' | while read line ; do echo $line ; done
hello there

pax$ echo 'hello<<tab>>there' | while read line ; do echo "$line" ; done
hello<<tab>>there

这篇关于回声将我的选项卡更改为空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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