[sed] Use sed to replace tab characters on both macOS and Linux

wrap up

By using ASCII code, you can replace the tab character in the same way on both macOS and Linux.

TAB="$(printf '\\\011')"
printf "hoge\tfuga\n" | sed "s/${TAB}/,/g"

sed OS dependent (BSD sed vs GNU sed)

As an example of using sed, I often used the following script that separates tabs with commas.

# Linux
$ printf "foo\tbar\n"
foo    bar
$ printf "foo\tbar\n" | sed "s/\t/,/g"
foo,bar

However, the: point_up: method does not work well on macOS.

# macOS
$ printf "foo\tbar\n"
foo    bar
$ printf "foo\tbar\n" | sed "s/\t/,/g"
foo    bar # <-Not changed!

Apparently this difference is due to macOS using BSD sed and Linux using GNU sed.

What to do with tab character replacement in BSD sed was mentioned in a previous Qiita article: bow:

-Replace tab string with sed on macOSX -Replace tab string with sed on macOSX

(Same title, but different article)

However, since the usage of the above method is limited, I wanted to have a little more versatility, but there was the following article.

-Replace the character string with a line break with the sed command, and replace it smartly.

In the above, LF (line feed) was expressed using ASCII code as follows.

LF=$(printf '\\\012_')
LF=${LF%_}

echo "hogehoge\nfoo\nbar" | sed 's/\\n/'"$LF"'/g'

I thought that this method could be applied to tab characters as it is, and when I verified it, it worked fine.

#Horizontal tab (HT) is 011 in ASCII code
TAB="$(printf '\\\011')" 
printf "hoge\tfuga\n" | sed "s/${TAB}/,/g"

You can now replace the tab character in the same way on both macOS and Linux.

Verification environment

# macOS
- OS: Catalina 10.15.6 19G2021 x86_64
- Kernel: 19.6.0
- Shell: zsh 5.7.1

# Linux
- OS: Linux Mint 20 x86_64
- kernel: 5.4.0-47-generic
- shell: bash 5.0.17

Recommended Posts

[sed] Use sed to replace tab characters on both macOS and Linux
Use Python's unicodedata library to display both full-width and half-width characters
I want to use Linux on mac
On Ubuntu Linux, set Tab to q
Beginners! Basic Linux commands and how to use them!
How to use C216 Audio Controller on Arch Linux
Preparing to use Ansible on an existing Linux server
I want to use OpenJDK 11 on Ubuntu Linux 18.04 LTS / 18.10
[Tips] How to use iPhone as webcam on Linux
[For beginners] How to use for statements on Linux (variables, etc.)
Install pyenv on MacBook Air and switch python to use
When you want to use python2.x on modern Gentoo Linux
What to do if you can't use WiFi on Linux
Assign a link-local address to Linux and use DNS-SD / SSDP
Use Github Desktop on Linux
Recording and playback on Linux
Use sshpass on Amazon linux2
Use Linux on Windows 10 (WSL2)
I want to use shortcut translation like DeepL app on Linux
How to set up and use OMC Log Analytics --Linux version -
How to use python put in pyenv on macOS with PyCall
How to display PDF resolution and detailed information on Linux (pdfinfo)
I want to use both key and value of Python iterator
Python 3.6 on Windows ... and to Xamarin.
How to install and use Tesseract-OCR
Are macOS and Linux completely different?
How to use Dataiku on Windows
Notes on how to use pywinauto
How to install graph-tool on macOS
How to install VMware-Tools on Linux
Notes on how to use featuretools
How to use homebrew on Debian
How to use .bash_profile and .bashrc
How to install and use Graphviz
Use Chrome Remote Desktop on Linux
Notes on how to use doctest
Use Linux commands on Android devices
[C language] How to use the crypt function on Linux [Password hashing]
Build Linux on a Windows environment. Steps to install Laradock and migrate