开发者

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. The problem is, foo.txt is a windows text file (I think), because once appended in 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. 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.

So, I have two qu开发者_高级运维estions here:

  1. How do I discover, using bash, what file-type foo.txt really is?
  2. How do convert foo.txt properly to make it appendable to bar.txt?


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

or use dos2unix, if it's available.


Convert the Windows line endings with dos2unix:

dos2unix foo.txt


  1. file foo.txt will output: "ASCII text, with CRLF line terminators" if the file has DOS-style line endings.


Use fromdos and todos, or unix2dos and dos2unix.


Look up the commands dos2unix and unix2dos. You may wish to make a copy first (cp)


A quick-and-dirty solution:

tr -d '\r' < foo.txt
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜