Wiefsコマンドの出力を処理する方法は?

Wiefsコマンドの出力を処理する方法は?

私のコマンドはwipefs /dev/sdX次のような出力を提供します。

offset               type
----------------------------------------------------------------
0x111                ext4   [filesystem]
                     UUID:  1111111-222222-333333-4444-5555555555

単一のコマンドラインからの出力でのみUUID値を取得するにはどうすればよいですか?

ベストアンサー1

GNUがある場合grep(1)-Pあなたのバージョンがまたはオプションをサポートしている--perl-regexp場合は、肯定的な振り返りアサーションを使用できます。

grep -Po "(?<=UUID:  ).*$" <(wipefs /dev/sd)

テスト

$ cat file
offset               type
----------------------------------------------------------------
0x111                ext4   [filesystem]
                     UUID:  1111111-222222-333333-4444-555555555

$ cat file | grep -Po "(?<=UUID:  ).*$"
1111111-222222-333333-4444-5555555555

~からgrep(1)マニュアルページ

-o, --only-matching
   Print  only  the  matched  (non-empty) parts of a matching line, with each such part 
   on a separate output line.

-P, --perl-regexp
   Interpret PATTERN as a Perl regular expression (PCRE, see below).  This is highly 
   experimental and grep -P may warn of unimplemented features.

正規表現の説明:

Positive Lookbehind アサーションを使用すると、(?<=UUID: )行末の後の文字列のみが印刷されます$

おすすめ記事