別の文字列の後に#で始まる文字列を前の行に移動できますか?

別の文字列の後に#で始まる文字列を前の行に移動できますか?

次のスクリプトファイルがあります。

auditctl -a always,exit -F arch=b64 -S openat -F auid=1000      <TAB> #description of command....
auditctl -a ....                                                <TAB> #long description

(コメントの前のタブ文字に注意してください。)

、またはsed内部コマンドを使用して文字列自体の後に説明を入れることができるため、次のような結果が表示されます。awkvim#

#description of command....
auditctl -a always,exit -F arch=b64 -S openat -F auid=1000      


#long description
auditctl -a ....

このコマンドを試しましたが、norm結果は災害でした。

:'<,'>norm f#D O P

ベストアンサー1

match()GNU awkを3番目の引数と\S/\s略語として使用します。

$ awk 'match($0,/([^#]*\S)\s*(#.*)/,a) { $0=a[2] ORS a[1] } 1' file
#description of command....
auditctl -a always,exit -F arch=b64 -S openat -F auid=1000
#long description
auditctl -a ....

または、POSIX awkを使用してください。

$ awk 'match($0,/[[:space:]]*#/) { $0=substr($0,RSTART+RLENGTH-1) ORS substr($0,1,RSTART-1) } 1' file
#description of # command....
auditctl -a always,exit -F arch=b64 -S openat -F auid=1000
#long description
auditctl -a ....

上記の#コードは、コメントに含まれる文字(aを含む)とコメントの前にスペースがあるかどうかにかかわらず機能し、印刷する前に残りの行の末尾からコメント前のスペースを削除します。

おすすめ記事