使用shell将.txt文件转换为.asc

问题描述 投票:3回答:2

我有以下文本文件:

ifile.txt
1.1 4.5
2.3 2.3
3.4 30.5

我想在开头添加一行(%file1),然后将其转换为.asc文件。

我使用以下命令:

(echo "%file1" ; cat ifile.txt)  > ofile.asc

但输出结果如下:

ofile.asc
%file1
1.1 4.5^M
2.3 2.3^M
3.4 30.5^M

期望的输出是:

ofile.asc
%file1
1.1 4.5
2.3 2.3
3.4 30.5
linux bash shell
2个回答
1
投票

您可以使用tr而不是cat从原始文件中删除DOS换行符:

{ echo "%file1"; tr -d '\r' < ifile.txt; } > ofile.asc

此外,不需要分叉shell,因为我们可以使用{ ... }对许多命令进行分组。


1
投票

你可以使用sed来消除^M字符:

{ echo "%file1" ; sed ‘s/^M//‘ file.txt ; } > ofile.asc
© www.soinside.com 2019 - 2024. All rights reserved.