简体   繁体   中英

Adding suffix not working with sed and awk on macOS

These are the contents of t.txt

ABCDEFG
ABCDEFG
ABCDEFG
ABCDEFG

I tried these commands to add a suffix to the end of the line:

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

The result in macOS:

-asdfFG
-asdfFG
-asdfFG
ABCDEFG-asdf

The result in Linux:

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

Why do I not get the same result in macOS?

You have carriage returns in your file. macOS Sierra, using built-in sed on an LF-only file:

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

Re-saved it with CRLF:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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