1

I want to replace all occurrences of a text string in a textfile on command line on CentOS 7.2.

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

I know, this can be achieved with different tools like sed, awk, tr, ... but I can't figure out an easy way to do this and be able to understand the command (important ;-)). For me the biggest problem are the newlines, without them, it is a very simple call to sed. But with newlines ...

Any suggestion with explanation is very welcome.

edited

Input

...
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
...

Output

...
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
...
  • Please edit your question and add an example of your input file and the output you would expect from it. – terdon Jul 12 '16 at 11:43
2

Here's one possible way

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

Testing

$ 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
...
0

You can use N in sed to pull next line of input in the pattern space, and then work on two lines at once.

As an example, conditionally pull the next line in when seeing a foo, and then do a replacement on both lines. (Note that consecutive lines with foo will break this.)

echo -e "asf\nfoo\nbar" | sed -e '/foo/N;s/o\nb/od\nc/'

Also, GNU sed has the -z option that separates "lines" on NUL characters instead of newlines. That will easily allow you to match on patterns crossing a newline, but will result in the string operations to run on the whole input at once, which might be a problem with large input.

The surrounding structure should of course be taken in to account, it might influence the best way to do the edit.

0

A new version of GNU sed supports the -z option.

Normally, sed reads a line by reading a string of characters up to the end-of-line character (new line or carriage return).
The GNU version of sed added a feature in version 4.2.2 to use the "NULL" character instead. This can be useful if you have files that use the NULL as a record separator. Some GNU utilities can genertae output that uses a NULL instead a new line, such as "find . -print0" or "grep -lZ".

You can use this option when you want sed to work over different lines.

sed -z 's/)[.]\nPORT/). \n0 closed ports\nPORT/g' inputfile

Please note that your example input has a space after (0.00090s latency).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.