添加后缀不适用于 macOS 上的 sed 和 awk
Adding suffix not working with sed and awk on macOS
这些是t.txt
的内容
ABCDEFG
ABCDEFG
ABCDEFG
ABCDEFG
我尝试使用这些命令在行尾添加后缀:
$ cat t.txt | sed -e 's/$/-asdf/'
$ cat t.txt | awk '{ print [=11=] "-asdf" }'
macOS 中的结果:
-asdfFG
-asdfFG
-asdfFG
ABCDEFG-asdf
Linux中的结果:
ABCDEFG-abcd
ABCDEFG-abcd
ABCDEFG-abcd
ABCDEFG-abcd
为什么我在 macOS 中得不到相同的结果?
您的文件中有回车 returns。 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
这些是t.txt
ABCDEFG
ABCDEFG
ABCDEFG
ABCDEFG
我尝试使用这些命令在行尾添加后缀:
$ cat t.txt | sed -e 's/$/-asdf/'
$ cat t.txt | awk '{ print [=11=] "-asdf" }'
macOS 中的结果:
-asdfFG
-asdfFG
-asdfFG
ABCDEFG-asdf
Linux中的结果:
ABCDEFG-abcd
ABCDEFG-abcd
ABCDEFG-abcd
ABCDEFG-abcd
为什么我在 macOS 中得不到相同的结果?
您的文件中有回车 returns。 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