ファイルのコマンドラインから文字列を検索して改行文字に置き換える方法

ファイルのコマンドラインから文字列を検索して改行文字に置き換える方法

CentOS 7.2のコマンドラインでテキストファイル内のすべてのテキスト文字列を置き換えたいです。

Search-string: ).\nPORT
Replacement-string: ). \n0 closed ports\nPORT

私はこれを他のツール(例えば、、、、…)を使って達成することができますが、sedそれを実行してコマンドを理解する簡単な方法が見つかりません(重要;-))。私にとって最大の問題は改行です。改行がなくても大丈夫です。awktrsed

説明を含む提案は大歓迎です。

編集済み

入力する

...
Nmap scan report for w.x.y.z (x.x.x.x)
Host is up (0.00090s latency). 
PORT   STATE SERVICE
21/tcp open  ftp
...
Nmap scan report for w.x.y.z (x.x.x.x)
Host is up (0.00079s latency).
Not shown: 2 closed ports
PORT   STATE SERVICE
22/tcp open  ssh
|_banner: SSH-2.0-mpSSH_0.2.1
...

出力

...
Nmap scan report for w.x.y.z (x.x.x.x)
Host is up (0.00090s latency). 
0 closed ports
PORT   STATE SERVICE
21/tcp open  ftp
...
Nmap scan report for w.x.y.z (x.x.x.x)
Host is up (0.00079s latency).
Not shown: 2 closed ports
PORT   STATE SERVICE
22/tcp open  ssh
|_banner: SSH-2.0-mpSSH_0.2.1
...

ベストアンサー1

可能なアプローチです。

sed '/)\. $/ {
n                          
/^PORT/ i\
0 closed ports
}'

テスト

$ sed '/)\. $/ {
n                          
/^PORT/ i\
0 closed ports
}' < input
...
Nmap scan report for w.x.y.z (x.x.x.x)
Host is up (0.00090s latency). 
0 closed ports
PORT   STATE SERVICE
21/tcp open  ftp
...
Nmap scan report for w.x.y.z (x.x.x.x)
Host is up (0.00079s latency).
Not shown: 2 closed ports
PORT   STATE SERVICE
22/tcp open  ssh
|_banner: SSH-2.0-mpSSH_0.2.1
...

おすすめ記事