bash Echo 将我的选项卡更改为空格

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

Echo changes my tabs to spaces

bashdelimiter

提问by Jenn D.

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

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

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

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

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

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).

这给我的实际应用造成了问题,这当然比上面的更复杂:我想取行,根据它生成一些新的东西,然后将原始行加上新的东西作为额外的字段输出。与管道将要足够复杂而不一堆cut -d ' 'sed -e 's/ /\t/g'(这将不会是包含空格反正制表符分隔数据安全)。

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!

我看过 IFS 解决方案,但在这种情况下它们似乎没有帮助。我想要的是一个 OFS ......除了我在回应,而不是 awk!我想,如果我能得到回声,逐字逐句地吐出我给它的东西,我的状态就会很好。有什么想法吗?谢谢!

回答by paxdiablo

Try:

尝试:

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

instead.

反而。

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

换句话说,它不是read替换选项卡,而是echo.

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

请参阅以下成绩单(使用<<tab>>选项卡所在的位置):

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