3

On macOS command line, how to replace first occurrence of a space by a tab, on each line of a file?

Examples of available tools on macOS: bash 3.2, BSD sed, awk, tr, perl 5, python 2.7, swift 4, etc.

I tried:

sed 's/^\([^ ]*\) /\1\t/' filename

But instead of a tab, I get the character 't'.

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

5

Good answers were posted in the comments. Here are some credits:

Thanks Leon and Inian for this solution:

sed 's/ /'$'\t''/' filename

Thanks Sundeep for this solution:

perl -pe 's/ /\t/' filename

Thanks Sundeep for finding the duplicate question, which gives this literal solution where the replacement string is a tab obtained in bash through Ctrl+V, Tab (note: copy-pasting it from Stack Overflow will not work):

sed 's/ / /' filename
Cœur
  • 37,241
  • 25
  • 195
  • 267