Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
2
Help us understand the problem. What is going on with this article?
@mzaki

BSD sedで行挿入

More than 1 year has passed since last update.

BSD sedで行挿入は結構面倒くさい。とくにインタラクティブシェルから操作しようとすると、シェルの様々な機能とバッティングするのか、期待通りに動作させるのは一苦労だ。

今のところ、/usr/bin/printfでコマンド列を用意して、sed -f /dev/stdinで受け取るのが可搬性が高いのかなと思っている。

/usr/bin/printf '/^rpcbind/i\\\nrpcbind : 192.168.0.1/255.255.255.0 : allow\n' | sed -i '' -f /dev/stdin /etc/hosts.allow

bashやzshならプロセス置換を使って、

sed -i '' -f <(/usr/bin/printf '/^rpcbind/i\\\nrpcbind : 192.168.0.1/255.255.255.0 : allow\n') /etc/hosts.allow

と書く方が直感的だろうか。

printf(やecho)もシェルのビルトインとシステムのバイナリとで挙動が違うことが多く悩ましい。

この辺のノウハウがまとまっているページを求めているのだけど、GNU sedの話と切り分けて検索するのが手間で辛い。

2
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
2
Help us understand the problem. What is going on with this article?