このbashスクリプトにif文(単語の句読点関連)をどのように追加しますか?

このbashスクリプトにif文(単語の句読点関連)をどのように追加しますか?

このbashスクリプトがあります

#!/bin/bash
cat $@ | while read line
do
    for word in $line
    do
        echo $word | circling-the-square
        # here's where i need to add the if statement:
        #if the word contains one of the four [!?.,],
        #then also echo that punctuation mark
    done
done

circling-the-square は Norvig に基づく Python スクリプトです。スペルキャリブレータ

このスクリプトは入力時に句読点を削除します。

def words(text): return re.findall('[a-z]+', text.lower()) 

それで私はbashこれに注意を払う必要がある。私は思ったか、それがうまくいくと思いましたが、正規表現を書く方法やif文にsed入れるawk方法がまだわからないので、ここで質問します。

ファイルをそのまま転送

alec@ROOROO:~/oddi-o/newton-fluxions$ cat 199
 advertisement lately publijtid by the author, the british hemisphere, or a map of a new contrivance, proper for initiating young minds in the firft rudiments of geography, and the ufe of the globes.

与えられた

alec@ROOROO:~/oddi-o/newton-fluxions$ ./hmmb 199
advertisement
lately
publijtid

by
the
author
the
british
hemisphere
or
a
map
of
a
new
contrivance
proper
for
initiating
young
minds
in
the
first
rudiments
of
geography
and
the
few
of
the
globes.

完璧ではありませんが、まだ便利です。参考までに\w、および句読点のみを含むように関連ファイルを編集しました[!?.,]。ファイルに:または;同じ文字が含まれていません。これらの4つの句読点をエコーするためだけに必要です。単語の一部として含めると、次のようになります。

alec@ROOROO:~/oddi-o/newton-fluxions/finforno$ ./hmmb 199
advertisement
lately
publijtid
by
the
author,
the
british
hemisphere,
or
a
map
of
a
new
contrivance,
proper
for
initiating
young
minds
in
the
firft
rudiments
of
geography,
and
the
ufe
of
the
globes.

ベストアンサー1

以下のように正規表現を使用します。指定された 1 つ以上の句読点を含む単語を検索し、その単語に最初に一致する句読点を印刷します。必要に応じて延長できます。

if [[ "$word" =~ ^.*([!?.,])+.*$ ]]
then
    echo "Found word: $word containing punctuation mark: ${BASH_REMATCH[1]}"
fi

おすすめ記事