-3

789の柄を合わせたいと思います。sed コマンドを 1 つだけ使用します (「-e」でリンクされた複数のコマンドはありません)。

ルールは、3 回目のファクス以降の番号と一致することです。

echo "fax 123 def fax tel 456 fax 789 fax 976" | sed 's/xxxxxxx/'
4

2 に答える 2

2

区切り文字としてawkwithを使用: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
于 2012-12-28T16:41:18.200 に答える
0

私は最終的に次のようなコマンドを使用しました:

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/'
于 2012-12-28T17:53:20.500 に答える