Bash 'cut' command for Mac

Asked by At

I want to cut everything with a delimiter ":" The input file is in the following format:

data1:data2
data11:data22
...

I have a linux command

cat merged.txt | cut -f1 -d ":" > output.txt

On mac terminal it gives an error:

cut: stdin: Illegal byte sequence

what is the correct way to do it on a mac terminal?

2 Answers

1
Gordon Davisson On Best Solutions

Your input file (merged.txt) probably contains bytes/byte sequences that are not valid in your current locale. For example, your locale might specify UTF-8 character encoding, but the file be in some other encoding and cannot be parsed as valid UTF-8. If this is the problem, you can work around it by telling tr to assume the "C" locale, which basically tells it to process the input as a stream of bytes without paying attention to encoding.

BTW, cat file | is what's commonly referred to as a Useless Use of Cat (UUOC) -- you can just use a standard input redirect < file instead, which cleaner and more efficient. Thus, my version of your command would be:

LC_ALL=C cut -f1 -d ":" < merged.txt > output.txt

Note that since the LC_ALL=C assignment is a prefix to the tr command, it only applies to that one command and won't mess up other operations that should assume UTF-8 (or whatever your normal locale is).

0
Adam vonNieda On

Your cut command works for me on my Mac, you can try awk for the same result

awk -F: '{print $1}' merged.txt

data1
data11