1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

awkでパターンマッチかつ範囲指定する方法

Posted at

awkでファイルの特定パターン間の文字列を抜きたい

awk パターンとアクション

例えば, docker-compose.ymlからパターンマッチで文字列を抜き出す

version: "3"
services:
  web:
    build:
      context: ./
      dockerfile: Dockerfile
    
  mysqld:
    build:
      context: ./mysql
      dockerfile: Dockerfile.mysql
   

awkの使い方は以下

$ awk '/開始地点のパターン/,/終了地点のパターン/'

,(カンマ) が範囲指定を意味するらしい。

$ cat docker-compose.yml | awk '/^service/,/mysqld/'

services:
  web:
    build:
      context: ./
      dockerfile: Dockerfile

  mysqld:

こんな感じの挙動になる。

docker-composeからサービス名だけ抜くときとかに便利

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?