How to search and replace a string with newlines on commandline in a file
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 portsnPORT
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
...
sed search replace
add a comment |
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 portsnPORT
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
...
sed search replace
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
add a comment |
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 portsnPORT
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
...
sed search replace
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 portsnPORT
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
...
sed search replace
sed search replace
edited Jul 12 '16 at 12:30
Christian Waidner
asked Jul 12 '16 at 11:02
Christian WaidnerChristian Waidner
1064
1064
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
add a comment |
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
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
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
add a comment |
3 Answers
3
active
oldest
votes
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
...
add a comment |
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 "asfnfoonbar" | sed -e '/foo/N;s/onb/odnc/'
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.
add a comment |
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 portsnPORT/g' inputfile
Please note that your example input has a space after (0.00090s latency).
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f295372%2fhow-to-search-and-replace-a-string-with-newlines-on-commandline-in-a-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
...
add a comment |
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
...
add a comment |
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
...
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
...
edited Jul 12 '16 at 13:22
answered Jul 12 '16 at 12:42
steeldriversteeldriver
37.2k45287
37.2k45287
add a comment |
add a comment |
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 "asfnfoonbar" | sed -e '/foo/N;s/onb/odnc/'
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.
add a comment |
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 "asfnfoonbar" | sed -e '/foo/N;s/onb/odnc/'
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.
add a comment |
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 "asfnfoonbar" | sed -e '/foo/N;s/onb/odnc/'
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.
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 "asfnfoonbar" | sed -e '/foo/N;s/onb/odnc/'
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.
edited Jul 12 '16 at 11:48
answered Jul 12 '16 at 11:34
ilkkachuilkkachu
61.9k10102178
61.9k10102178
add a comment |
add a comment |
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 portsnPORT/g' inputfile
Please note that your example input has a space after (0.00090s latency).
add a comment |
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 portsnPORT/g' inputfile
Please note that your example input has a space after (0.00090s latency).
add a comment |
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 portsnPORT/g' inputfile
Please note that your example input has a space after (0.00090s latency).
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 portsnPORT/g' inputfile
Please note that your example input has a space after (0.00090s latency).
answered 5 hours ago
Walter AWalter A
514210
514210
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f295372%2fhow-to-search-and-replace-a-string-with-newlines-on-commandline-in-a-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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