789の柄を合わせたいと思います。sed コマンドを 1 つだけ使用します (「-e」でリンクされた複数のコマンドはありません)。
ルールは、3 回目のファクス以降の番号と一致することです。
echo "fax 123 def fax tel 456 fax 789 fax 976" | sed 's/xxxxxxx/'
区切り文字としてawk
withを使用:fax
$ echo "fax 123 def fax tel 456 fax 789 fax ghi" | awk -F'fax ' '{print $4}'
789
とsed
:
$ echo "fax 123 def fax tel 456 fax 789 fax ghi" | sed 's/.*fax \([0-9]\+\).*/\1/'
789
編集: 行ベースなので、最初にフィールドを分割するsed
ために使用するのはどうですか:grep
$ echo "fax 123 def fax tel 456 fax 789 fax 976" | egrep -o 'fax (tel )?[0-9]+'
fax 123
fax tel 456
fax 789
fax 976
次にsed
、どの行(フィールド)を指定するために使用します。
$ ... | sed -n '1s/^[^0-9]*//p'
123
$ ... | sed -n '2s/^[^0-9]*//p'
456
$ ... | sed -n '3s/^[^0-9]*//p'
789
私は最終的に次のようなコマンドを使用しました:
echo " fax 123 def fax tel 456 fax 789 fax 012" |
sed -e 's/^\s*fax\s*[0-9a-z]*\s*[a-z0-9]*\s*fax\s*[0-9a-z]*\s*[0-9a-z]*\s*fax\s*\([0-9]*\).*$/\1/'