sedを使って行指定の編集

sedを使って行指定の編集をすることができます。

たとえば、CentOS6.4で下記のようにすると、17行目の gpgcheck だけ 1 になって標準出力に表示されます。

$ sed '17s/gpgcheck=1/gpgcheck=0/' /etc/yum.repos.d/CentOS-Base.repo

ある行に追加したい場合は、行番号を指定して続いて「a\」を指定します。具体的には次のようになります。

$ sed '3a\hello' /etc/yum.repos.d/CentOS-Base.repo | head -5
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
hello
# update status of each mirror to pick mirrors that are updated to and

指定した 3行目に追加して「hello」という文字列が挿入されていることがわかります。

bashなどについて理解するには下記の参考書が役に立ちます。

同じタグの記事: bash
同じタグの記事: Linux
同じタグの記事: sed
同じタグの記事: shell script
同じカテゴリの記事: Linux
関連書籍: bash