How to search and replace a string with newlines on commandline in a file












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









share|improve this question

























  • 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
















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









share|improve this question

























  • 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














1












1








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









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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










3 Answers
3






active

oldest

votes


















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





share|improve this answer

































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






    share|improve this answer

































      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 portsnPORT/g' inputfile


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






      share|improve this answer























        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
        });


        }
        });














        draft saved

        draft discarded


















        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









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





        share|improve this answer






























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





          share|improve this answer




























            2












            2








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





            share|improve this answer















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






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jul 12 '16 at 13:22

























            answered Jul 12 '16 at 12:42









            steeldriversteeldriver

            37.2k45287




            37.2k45287

























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






                share|improve this answer






























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






                  share|improve this answer




























                    0












                    0








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






                    share|improve this answer















                    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.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jul 12 '16 at 11:48

























                    answered Jul 12 '16 at 11:34









                    ilkkachuilkkachu

                    61.9k10102178




                    61.9k10102178























                        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 portsnPORT/g' inputfile


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






                        share|improve this answer




























                          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 portsnPORT/g' inputfile


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






                          share|improve this answer


























                            0












                            0








                            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 portsnPORT/g' inputfile


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






                            share|improve this answer













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







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 5 hours ago









                            Walter AWalter A

                            514210




                            514210






























                                draft saved

                                draft discarded




















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                Accessing regular linux commands in Huawei's Dopra Linux

                                Can't connect RFCOMM socket: Host is down

                                Kernel panic - not syncing: Fatal Exception in Interrupt