bash 如何在终端的文本文件中放置标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11378516/
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
How can I put tabs in text files from Terminal?
提问by W3Geek
When I use Terminal to find every job that has been worked on last twenty-four hours I get a text file with lines like this:
当我使用终端查找过去 24 小时内完成的每项工作时,我会得到一个文本文件,其中包含如下几行:
ABC 12345 Job Worked on DATE
ABC 12345 工作在 DATE
DEF 67890 Another Job on DATE
DEF 67890 DATE 的另一份工作
GHI 10112 Final Job on DATE
GHI 10112 日期的最终作业
This text file can be up to hundred lines. I have to insert all this data into a tabel in Microsoft Excel. To make my life easier I would like to figure out how to add a tab in every line, after the numbers.
这个文本文件最多可以有一百行。我必须将所有这些数据插入到 Microsoft Excel 的表格中。为了让我的生活更轻松,我想弄清楚如何在数字之后的每一行中添加一个选项卡。
Something like this:
像这样的东西:
echo "ABC 12345 <tab> Job Worked on DATE" >> jobs.txt
echo "DEF 67890 <tab> Another Job on DATE" >> jobs.txt
echo "GHI 10112 <tab> Final Job on DATE" >> jobs.txt
How would I achieve this?
我将如何实现这一目标?
回答by AW101
Use echo -e with \t for a tab. Like this:
使用 echo -e 和 \t 作为选项卡。像这样:
echo -e "ABC 12345 \t Job Worked on DATE" >> jobs.txt
回答by kev
You can press Ctrl-vTabto type a raw Tab.
您可以按Ctrl-vTab键入原始 Tab。
You can also use $'...'
c-string syntax:
您还可以使用$'...'
c-string 语法:
echo $'ABC 12345 \t Job Worked on DATE' >> jobs.txt
回答by Paused until further notice.
Append a tab to the second field:
将选项卡附加到第二个字段:
awk '{ = "\t"; print}' inputfile > outputfile
Similar, but using a regex:
类似,但使用正则表达式:
sed 's/[[:digit:]]\+/&\t/' inputfile > outputfile