0

这些是内容t.txt

ABCDEFG
ABCDEFG
ABCDEFG
ABCDEFG

我尝试使用这些命令在行尾添加后缀:

$ cat t.txt | sed -e 's/$/-asdf/'
$ cat t.txt | awk '{ print $0 "-asdf" }'

macOS 中的结果:

-asdfFG
-asdfFG
-asdfFG
ABCDEFG-asdf

Linux 中的结果:

ABCDEFG-abcd
ABCDEFG-abcd
ABCDEFG-abcd
ABCDEFG-abcd

为什么我在 macOS 中没有得到相同的结果?

4

1 回答 1

2

您的文件中有回车符。macOS Sierra,在仅 LF 文件上使用内置 sed:

mike ~ ❱❱❱ cat t.txt
ABCDEFG
ABCDEFG
ABCDEFG
ABCDEFG
mike ~ ❱❱❱ /usr/bin/sed -e 's/$/-asdf/' t.txt
ABCDEFG-asdf
ABCDEFG-asdf
ABCDEFG-asdf
ABCDEFG-asdf

用 CRLF 重新保存它:

mike ~ ❱❱❱ /usr/bin/sed -e 's/$/-asdf/' t.txt
-asdfFG
-asdfFG
-asdfFG
-asdfFG
于 2017-03-10T17:03:30.693 回答