简体   繁体   English

如何在bash中转换文本文件? (从foo.txt到bar.txt)

[英]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: 因此,我在这里有两个问题:

  1. How do I discover, using bash, what file-type foo.txt really is? 如何使用bash发现真正的文件类型foo.txt?
  2. How do convert foo.txt properly to make it appendable to bar.txt? 如何正确转换 foo.txt以使其可附加到bar.txt?

dos2unix转换Windows行尾:

dos2unix foo.txt
sed 's/$'"/`echo \\\r`/" foo.txt >> bar.txt

或使用dos2unix (如果可用)。

  1. file foo.txt具有DOS样式的行尾,则file foo.txt将输出:“ ASCII文本,带有CRLF行终止符”。

使用fromdostodosunix2dosdos2unix

Look up the commands dos2unix and unix2dos . 查找命令dos2unixunix2dos You may wish to make a copy first ( cp ) 您不妨先制作一份副本( cp

快速而又肮脏的解决方案:

tr -d '\r' < foo.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM