1

このシナリオで置換を使用する方法を知りたいです。以下のように、 myfile.txtresponsefile.txtという 2 つのファイルがあります。

myfile.txt

user=myname
user_1=yourname
group=mygroup
group_1=yourgroup

応答ファイル.txt

#Please fill the user id details.
user=
#user_1=    

#Please fill the group id details.
group=
#group_1=

ここで、上記の 2 つのファイルを使用して、一致するパターンを置き換えた後、次の最終結果を得たいと考えています。responsefile.txt の内容はそのままで、一致したパターンに myfile.txt で提供される詳細を前に付けるか、パターンが両方のファイルで同じであるため、一致する行全体のみを置き換える必要があります。

responsefile.txt (新しいファイルを置換/パターンで置換)

#Please fill the user id details.
user=myname
user_1=yourname    
#Please fill the group id details.
group=mygroup
group_1=yourgroup

パターンはハッシュなしまたはハッシュ付きであるため、一致時にハッシュを削除する必要があります。ファイル responsefile.txt の長さは約 604L、16481C であることに注意してください。

実際のシナリオのコンテンツには 100 以上の類似パターンがあるため、簡単な解決策が必要です。上記は一例です。

ありがとうラージェッシュ

4

1 に答える 1

0

この awk は動作するはずです:

awk -F '=' 'FNR==NR{a[$1]=$2;next} !($1 in a){print} ($1 in a){print $0 a[$1]}' myfile.txt responsefile.txt

拡張フォーム:

awk -F '=' 'FNR == NR {
   a[$1] = $2;
   next
}
!($1 in a) {
   print
}
($1 in a) {
   print $0 a[$1]
}' myfile.txt responsefile.txt

出力:

'#'Please fill the user id details.
'#'Here is example user=urname.
user==myname

'#'Please fill the group id details.
'#'Here is example group=urgroup.
group==mygroup
于 2013-08-17T19:34:39.190 に答える