0

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?

miken32
  • 42,008
  • 16
  • 111
  • 154
Sean
  • 17
  • 4

1 Answers1

2

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
miken32
  • 42,008
  • 16
  • 111
  • 154
  • Same behavior on openbsd. I think awk of mac is same as openbsd. By the way my geany editor allows me to select line endings through a menu : LF (unix) - Windows (CRLF) and Classic Mac CR. – George Vasiliou Mar 10 '17 at 19:58
  • Yes, macOS uses all BSD utilities, and it causes no end of confusion! That said, I get the same behaviour with CRLF files using `gsed`. – miken32 Mar 10 '17 at 20:02
  • I have Debian - FreeBSD (VM) and OpenBSD (VM) and all of them have available gawk in their repos. I suppose even mac has gawk. I still can not understand why most non Linux people refuse to use gnu tools when these tools are available for most of the distros out there. – George Vasiliou Mar 10 '17 at 20:07
  • I had to install gawk via MacPorts actually. Not built-in at all. – miken32 Mar 10 '17 at 20:09
  • 1
    Built in is only at Debian and similar Linux Distros. Not built in on BSD also. But it is just few letters away.... :-) – George Vasiliou Mar 10 '17 at 20:10