メールアドレスの抽出

メールアドレスの抽出

次の行を含むファイルがあります。

user=<[email protected]>,
user=<[email protected]>,
user=<[email protected]>,
user=<[email protected]>,
user=<[email protected]>,
user=<[email protected]>,

このファイルから電子メールアドレスを抽出する最良の方法は何ですか?

ベストアンサー1

awk他の回答に記載されているとおりに使用できます。

sedまたはperl同様のものを使用できますruby

perl -wlne '/<(.*)>/ and print $1' file

ただし、bash必要に応じて使用することも可能です。

最初のステップ。ファイルを1行ずつ出力します。

while read line; do echo $line; done <file

次に、不要なプレフィックスとサフィックスを削除します。

while read line; do line=${line##user=<}; line=${line%%>,}; echo $line; done <file

同様に、より一般的で短いです。

while read line; do line=${line##*<}; echo ${line%%>*}; done <file

これはあなたの例で動作し、他のシェルでも動作します。

始まりと終わりから数文字を削除するには、次のようにします。

while read line; do echo ${line:6:-2}; done <file

man bash詳細については、bash()の詳細なマニュアルページを読んでください。

おすすめ記事