[英]How do I convert text files in bash? (foo.txt to bar.txt)
I'm trying to append, via bash, one text file to another using foo.txt >> bar.txt
. 我试图通过bash使用foo.txt >> bar.txt
将一个文本文件追加到另一个。 The problem is, foo.txt is a windows text file (I think), because once appended in bar.txt 问题是,foo.txt是Windows文本文件(我认为),因为一旦附加在bar.txt中
every new^M
line^M
ends^M
like this.^M
Thus, they are surely two different file-types. 因此,它们肯定是两种不同的文件类型。
I've searched Google for answers but Google doesn't accept special characters like ">>" and "^" in search . 我已经在Google上搜索了答案,但Google在搜索中不接受诸如“ >>”和“ ^”之类的特殊字符 。 The closest solution I can find is a reported solution on commandlinefu.com , but all it does is strip the r's from bar.txt which, really, is no solution at all. 我能找到的最接近的解决方案是在commandlinefu.com上报告的解决方案 ,但是它所做的只是从bar.txt中删除r,这实际上根本不是解决方案。
So, I have two questions here: 因此,我在这里有两个问题:
用dos2unix
转换Windows行尾:
dos2unix foo.txt
sed 's/$'"/`echo \\\r`/" foo.txt >> bar.txt
或使用dos2unix
(如果可用)。
file foo.txt
具有DOS样式的行尾,则file foo.txt
将输出:“ ASCII文本,带有CRLF行终止符”。 使用fromdos
和todos
或unix2dos
和dos2unix
。
Look up the commands dos2unix
and unix2dos
. 查找命令dos2unix
和unix2dos
。 You may wish to make a copy first ( cp
) 您不妨先制作一份副本( cp
)
快速而又肮脏的解决方案:
tr -d '\r' < foo.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.