UNIX:用冒号替换换行符,在EOF之前保留换行符 [英] UNIX: Replace Newline w/ Colon, Preserving Newline Before EOF
问题描述
我有一个文本文件("INPUT.txt"),格式为:
I have a text file ("INPUT.txt") of the format:
A<LF>
B<LF>
C<LF>
D<LF>
X<LF>
Y<LF>
Z<LF>
<EOF>
我需要重新格式化为:
A:B:C:D:X:Y:Z<LF>
<EOF>
我知道您可以使用"sed"来做到这一点.通过"sed"实现此目标的Google点击量高达10亿.但是我试图强调可读性,简单性,并使用正确的工具来完成正确的工作. "sed"是一个使用和隐藏换行符的行编辑器.可能不是这项工作的正确工具!
I know you can do this with 'sed'. There's a billion google hits for doing this with 'sed'. But I'm trying to emphasis readability, simplicity, and using the correct tool for the correct job. 'sed' is a line editor that consumes and hides newlines. Probably not the right tool for this job!
我认为这项工作的正确工具是"tr".我可以使用以下命令用冒号替换所有换行符:
I think the correct tool for this job would be 'tr'. I can replace all the newlines with colons with the command:
cat INPUT.txt | tr '\n' ':'
我完成了99%的工作.不过,我现在有一个问题.通过用冒号替换所有换行符,我不仅在序列的末尾得到了一个多余的冒号,而且在输入的末尾也丢失了回车符.看起来像这样:
There's 99% of my work done. I have a problem, now, though. By replacing all the newlines with colons, I not only get an extraneous colon at the end of the sequence, but I also lose the carriage return at the end of the input. It looks like this:
A:B:C:D:X:Y:Z:<EOF>
现在,我需要从输入末尾删除冒号.但是,如果我尝试通过"sed"传递此处理后的输入以删除最后一个冒号(我认为现在应该正确使用"sed"),我会发现自己遇到了第二个问题.输入完全不再由换行符终止!对于所有命令,"sed"都将彻底失败,因为它永远找不到输入的第一行的结尾!
Now, I need to remove the colon from the end of the input. However, if I attempt to pass this processed input through 'sed' to remove the final colon (which would now, I think, be a proper use of 'sed'), I find myself with a second problem. The input is no longer terminated by a newline at all! 'sed' fails outright, for all commands, because it never finds the end of the first line of input!
似乎在某些输入的末尾添加换行符是非常非常常见的任务,并且考虑到我自己非常想写一个程序用C语言来完成(这需要大约八行代码) ,我无法想象还没有一种非常简单的方法可以使用Linux内核中已经可用的工具来做到这一点.
It seems like appending a newline to the end of some input is a very, very common task, and considering I myself was just sorely tempted to write a program to do it in C (which would take about eight lines of code), I can't imagine there's not already a very simple way to do this with the tools already available to you in the Linux kernel.
推荐答案
这应该可以完成(不需要cat
和echo
):
This should do the job (cat
and echo
are unnecessary):
tr '\n' ':' < INPUT.TXT | sed 's/:$/\n/'
仅使用sed
:
sed -n ':a; $ ! {N;ba}; s/\n/:/g;p' INPUT.TXT
没有任何外部条件的重击:
Bash without any externals:
string=($(<INPUT.TXT))
string=${string[@]/%/:}
string=${string//: /:}
string=${string%*:}
在sh
中使用循环:
colon=''
while read -r line
do
string=$string$colon$line
colon=':'
done < INPUT.TXT
使用AWK:
awk '{a=a colon $0; colon=":"} END {print a}' INPUT.TXT
或者:
awk '{printf colon $0; colon=":"} END {printf "\n" }' INPUT.TXT
这是纯Bash的另一种方式:
Here's another way in pure Bash:
string=($(<INPUT.TXT))
saveIFS=$IFS
IFS=':'
newstring="${string[*]}"
IFS=$saveIFS
这是 使用echo
的另一种方式:
Here's yet another way which does use echo
:
echo "$(tr '\n' ':' < INPUT.TXT | head -c -1)"
这篇关于UNIX:用冒号替换换行符,在EOF之前保留换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!