次の行を含むファイルがあります。
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()の詳細なマニュアルページを読んでください。