同じパターンを置き換えるには、ファイル内の文字列を別の値に置き換えます。

同じパターンを置き換えるには、ファイル内の文字列を別の値に置き換えます。

その中にこのような文字列があります。/etc/mail/virtusertable

[email protected]    error:nouser Account temporary disabled
[email protected]    error:nouser Account temporary disabled
[email protected]    error:nouser Account temporary disabled

すべてを私が得ることができるerror:nouser Account temporary disabled適切なコンテンツに置き換えたいと思います。virtuser_NNN/etc/dovecot/dovecot.passwd

私は同じ結果で別のバリエーションを試しました。すべてをerror:nouser Account temporary disabled最初のものと交換してくださいvirtuser_NNN/etc/dovecot/dovecot.passwd

私のスクリプトを実行すると、次のようになります。

[email protected]    virtuser_1
[email protected]    virtuser_1
[email protected]    virtuser_1

私のスクリプトは次のとおりです

 cat /etc/mail/virtusertable_back | grep example.com |
 grep 'error:nouser Account temporary disabled' | awk '{print $1}' |
 xargs -I{} grep {} /etc/dovecot/dovecot.passwd | awk -F'::' '{print $2}' |
 xargs -I{} sh -c 'grep {} /etc/passwd' | awk -F: '{print $1}'|
 xargs -n1 -P1 -I{} sed -i 's/error\:nouser Account temporary disabled/{}/' /etc/mail/virtusertable_back

はい、私はこれをコピーしています/etc/mail/virtusertable

そして、/etc/dovecot/dovecot.passwd次の形式の履歴があります。

[email protected]:*$1$hash here:user id:group id::/var/www/userlogin/data/email/example.com/mail:::/var/mail/virtuser_NNNN

追加するには、ファイル全体ではなく特定のメールドメイン内の対応するVMにのみ適用されるように、エラーメッセージを含む文字列を変更したいと思います。

ベストアンサー1

私はこれをするためにPerlを使います。電子メールと関連するユーザー名を/etc/dovecot/dovecot.passwdハッシュに保存し、次に変更します/etc/mail/virtusertable

$ perl -i -ape 'BEGIN{
                   open($fh,"/etc/dovecot/dovecot.passwd");
                    while(<$fh>){ @G=split(/:/); $k{$G[0]}=$G[2]; }
            }
            s/error:nouser Account temporary disabled/$k{$F[0]}/ if defined $k{$F[0]};
'  /etc/mail/virtusertable > newfile

説明する

-iPerlは良いですsed -i。「提供されたスクリプトを適用した後に各行を印刷します」を意味します。-a@Fawk-p-e

スクリプト自体は(入力ファイルを読む前に)/etc/dovecot/dovecot.passwdチャンクで読み込み、各行を配列に分割し、各電子メールを値がユーザー名のハッシュのキーとして保存します。BEGIN{}/etc/mail/virtusertable:@G%k

/etc/mail/virtusertable完了すると処理は続行され、各ユーザー名は最初のフィールド(定義されている場合)の電子メールに対応するユーザー名に置き換えられます。error:nouser Account temporary disabled

おすすめ記事