How to find word and change only next second line word

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

up vote
1
down vote

favorite

The file name staff.txt and sample contents are:

JHON
MANAGER
10000

I want to find JHON in a file and I want to change whatever’s in the 2nd line after that one with another word/number.

How can I accomplish this?

share|improve this question

  • is 10000 static text, or you want to change whatever is two lines below JHON?
    – Jeff Schaller
    Nov 29 at 16:30

  • Whatever below after two lines
    – Sourabh Kumar
    Nov 29 at 16:32

up vote
1
down vote

favorite

The file name staff.txt and sample contents are:

JHON
MANAGER
10000

I want to find JHON in a file and I want to change whatever’s in the 2nd line after that one with another word/number.

How can I accomplish this?

share|improve this question

  • is 10000 static text, or you want to change whatever is two lines below JHON?
    – Jeff Schaller
    Nov 29 at 16:30

  • Whatever below after two lines
    – Sourabh Kumar
    Nov 29 at 16:32

up vote
1
down vote

favorite

up vote
1
down vote

favorite

The file name staff.txt and sample contents are:

JHON
MANAGER
10000

I want to find JHON in a file and I want to change whatever’s in the 2nd line after that one with another word/number.

How can I accomplish this?

share|improve this question

The file name staff.txt and sample contents are:

JHON
MANAGER
10000

I want to find JHON in a file and I want to change whatever’s in the 2nd line after that one with another word/number.

How can I accomplish this?

text-processing awk grep

share|improve this question

share|improve this question

share|improve this question

share|improve this question

edited Nov 29 at 16:49

Jeff Schaller

37.4k1052121

37.4k1052121

asked Nov 29 at 16:19

Sourabh Kumar

42

42

  • is 10000 static text, or you want to change whatever is two lines below JHON?
    – Jeff Schaller
    Nov 29 at 16:30

  • Whatever below after two lines
    – Sourabh Kumar
    Nov 29 at 16:32

  • is 10000 static text, or you want to change whatever is two lines below JHON?
    – Jeff Schaller
    Nov 29 at 16:30

  • Whatever below after two lines
    – Sourabh Kumar
    Nov 29 at 16:32

is 10000 static text, or you want to change whatever is two lines below JHON?
– Jeff Schaller
Nov 29 at 16:30

is 10000 static text, or you want to change whatever is two lines below JHON?
– Jeff Schaller
Nov 29 at 16:30

Whatever below after two lines
– Sourabh Kumar
Nov 29 at 16:32

Whatever below after two lines
– Sourabh Kumar
Nov 29 at 16:32

6 Answers
6

active

oldest

votes

up vote
5
down vote

accepted

With sed you can move to next line with n:

sed '/JHON/{n;n;s/.*/42/}'

share|improve this answer

    up vote
    7
    down vote

    Use ed, man!

    ed -s staff.txt <<< $'/JHON/+2s/.*/4242nwnq'
    

    This calls ed in scripting mode (-s) on the staff.txt file and sends it the following commands in an ANSI-quoted here-string:

    • find the line containing JHON and go two lines beyond that (+2)
    • on that line, search and replace anything and everything that’s there (.*) with 4242
    • write the file out to disk
    • quit ed

    The intermediate n‘s separate the various ed commands.

    Alternatively, you could use the c command to change the line:

    ed -s staff.txt <<< $'/JHON/+2cn4242n.nwnq'
    

    Here, the replacement text needs to be entered separately (after) the c command, and ended with a period on its own (.n).

    share|improve this answer

    • 1

      Rob Pike would be proud!
      – Stephen Kitt
      Nov 29 at 17:37

    • The succeeding comment on that post sounds like a new spin on the old joke: “I’ve been using ed for years, mostly because I can’t figure out how to exit it.”
      – Jeff Schaller
      Nov 29 at 17:41

    • When i am using this command ed -s staff.txt <<< $’/JHON/+2s/.*/4242nwnq’ Its working fine but in some case JHON word in a Bracket [ ]. at that time this command not working properly. Its changing some another word that start with [xyz ] example. 1) ed -s staff.txt <<< $’/[JHON]/+2s/.*/4242nwnq’ [JHON] [MANAGER] [10000] Its changing first bracket word in file not changing [JHON]
      – Sourabh Kumar
      Nov 30 at 8:47

    up vote
    3
    down vote

    Using awk:

    awk 'f{$0="newvalue";f=0}/JHON/{print;getline;f=1}1'
    

    f{$0="newvalue";f=0} – If f is true set line to newvalue and reset f to 0

    /JHON/{print;getline;f=1} – If pattern JHON is found print it, print the next line, and set f to 1

    1 – print

    share|improve this answer

      up vote
      1
      down vote

      Yet another awk solution:

      awk '/JHON/{c=0};(c!=""&&c++==2){$0="42"};{print};'
      

      sets a counter c when JHON is found. Then, if c is set, increment and when c==2, change line ($0) to “42”.

      share|improve this answer

        up vote
        1
        down vote

        if you have only one person (one register), you can use this code:

        awk '/JHON/{print $0; getline; print "SUPERVISOR"; getline; print "99999"; }' staff.txt
        

        If you staff.txt file has many persons (many registers) and you should replace only JHON data:

        awk '/.*/{if($0=="JHON"){print $0; getline; print "SUPERVISOR"; getline; print "99999"; } else {print $0;}}' staff2.txt
        

        Example of staff2.txt:

        JHON
        MANAGER
        10000
        ROGER
        TEACHER
        3000
        ROBERT
        SCIENTIST
        20000
        

        You should get this output:

        JHON
        SUPERVISOR
        99999
        ROGER
        TEACHER
        3000
        ROBERT
        SCIENTIST
        20000
        

        Another short way, to get the same output, could be:

        awk '/JHON/{print;getline; print "SUPERVISOR"; getline; $0="99999"}1' staff2.txt
        

        Explanation: if line is JHON, print JHON, go to the next line, print SUPERVISOR, go to the next line, replace it with 99999.
        if line is NOT JHON, only print it.

        share|improve this answer

          up vote
          0
          down vote

          When I am using this command

          ed -s staff.txt <<< $'/JHON/+2s/.*/4242nwnq'
          

          Its working fine but in some case JHON word is in a Bracket [ ]
          at that time this command not working properly. Its changing some another word that start with [xyz ]

          example. 1)

          ed -s staff.txt <<< $'/[JHON]/+2s/.*/4242nwnq'
          [JHON]
          [MANAGER]
          [10000]
          

          Its changing first bracket word in file not changing [JHON].

          share|improve this answer

          • 1

            I think this is not an answer, you should write it as comment below the answer unix.stackexchange.com/a/484968/255251
            – Debian_yadav
            Nov 30 at 8:45

          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’,
          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%2f484966%2fhow-to-find-word-and-change-only-next-second-line-word%23new-answer’, ‘question_page’);
          }
          );

          Post as a guest

          Required, but never shown

          6 Answers
          6

          active

          oldest

          votes

          6 Answers
          6

          active

          oldest

          votes

          active

          oldest

          votes

          active

          oldest

          votes

          up vote
          5
          down vote

          accepted

          With sed you can move to next line with n:

          sed '/JHON/{n;n;s/.*/42/}'
          

          share|improve this answer

            up vote
            5
            down vote

            accepted

            With sed you can move to next line with n:

            sed '/JHON/{n;n;s/.*/42/}'
            

            share|improve this answer

              up vote
              5
              down vote

              accepted

              up vote
              5
              down vote

              accepted

              With sed you can move to next line with n:

              sed '/JHON/{n;n;s/.*/42/}'
              

              share|improve this answer

              With sed you can move to next line with n:

              sed '/JHON/{n;n;s/.*/42/}'
              

              share|improve this answer

              share|improve this answer

              share|improve this answer

              answered Nov 29 at 16:42

              RoVo

              2,379215

              2,379215

                  up vote
                  7
                  down vote

                  Use ed, man!

                  ed -s staff.txt <<< $'/JHON/+2s/.*/4242nwnq'
                  

                  This calls ed in scripting mode (-s) on the staff.txt file and sends it the following commands in an ANSI-quoted here-string:

                  • find the line containing JHON and go two lines beyond that (+2)
                  • on that line, search and replace anything and everything that’s there (.*) with 4242
                  • write the file out to disk
                  • quit ed

                  The intermediate n‘s separate the various ed commands.

                  Alternatively, you could use the c command to change the line:

                  ed -s staff.txt <<< $'/JHON/+2cn4242n.nwnq'
                  

                  Here, the replacement text needs to be entered separately (after) the c command, and ended with a period on its own (.n).

                  share|improve this answer

                  • 1

                    Rob Pike would be proud!
                    – Stephen Kitt
                    Nov 29 at 17:37

                  • The succeeding comment on that post sounds like a new spin on the old joke: “I’ve been using ed for years, mostly because I can’t figure out how to exit it.”
                    – Jeff Schaller
                    Nov 29 at 17:41

                  • When i am using this command ed -s staff.txt <<< $’/JHON/+2s/.*/4242nwnq’ Its working fine but in some case JHON word in a Bracket [ ]. at that time this command not working properly. Its changing some another word that start with [xyz ] example. 1) ed -s staff.txt <<< $’/[JHON]/+2s/.*/4242nwnq’ [JHON] [MANAGER] [10000] Its changing first bracket word in file not changing [JHON]
                    – Sourabh Kumar
                    Nov 30 at 8:47

                  up vote
                  7
                  down vote

                  Use ed, man!

                  ed -s staff.txt <<< $'/JHON/+2s/.*/4242nwnq'
                  

                  This calls ed in scripting mode (-s) on the staff.txt file and sends it the following commands in an ANSI-quoted here-string:

                  • find the line containing JHON and go two lines beyond that (+2)
                  • on that line, search and replace anything and everything that’s there (.*) with 4242
                  • write the file out to disk
                  • quit ed

                  The intermediate n‘s separate the various ed commands.

                  Alternatively, you could use the c command to change the line:

                  ed -s staff.txt <<< $'/JHON/+2cn4242n.nwnq'
                  

                  Here, the replacement text needs to be entered separately (after) the c command, and ended with a period on its own (.n).

                  share|improve this answer

                  • 1

                    Rob Pike would be proud!
                    – Stephen Kitt
                    Nov 29 at 17:37

                  • The succeeding comment on that post sounds like a new spin on the old joke: “I’ve been using ed for years, mostly because I can’t figure out how to exit it.”
                    – Jeff Schaller
                    Nov 29 at 17:41

                  • When i am using this command ed -s staff.txt <<< $’/JHON/+2s/.*/4242nwnq’ Its working fine but in some case JHON word in a Bracket [ ]. at that time this command not working properly. Its changing some another word that start with [xyz ] example. 1) ed -s staff.txt <<< $’/[JHON]/+2s/.*/4242nwnq’ [JHON] [MANAGER] [10000] Its changing first bracket word in file not changing [JHON]
                    – Sourabh Kumar
                    Nov 30 at 8:47

                  up vote
                  7
                  down vote

                  up vote
                  7
                  down vote

                  Use ed, man!

                  ed -s staff.txt <<< $'/JHON/+2s/.*/4242nwnq'
                  

                  This calls ed in scripting mode (-s) on the staff.txt file and sends it the following commands in an ANSI-quoted here-string:

                  • find the line containing JHON and go two lines beyond that (+2)
                  • on that line, search and replace anything and everything that’s there (.*) with 4242
                  • write the file out to disk
                  • quit ed

                  The intermediate n‘s separate the various ed commands.

                  Alternatively, you could use the c command to change the line:

                  ed -s staff.txt <<< $'/JHON/+2cn4242n.nwnq'
                  

                  Here, the replacement text needs to be entered separately (after) the c command, and ended with a period on its own (.n).

                  share|improve this answer

                  Use ed, man!

                  ed -s staff.txt <<< $'/JHON/+2s/.*/4242nwnq'
                  

                  This calls ed in scripting mode (-s) on the staff.txt file and sends it the following commands in an ANSI-quoted here-string:

                  • find the line containing JHON and go two lines beyond that (+2)
                  • on that line, search and replace anything and everything that’s there (.*) with 4242
                  • write the file out to disk
                  • quit ed

                  The intermediate n‘s separate the various ed commands.

                  Alternatively, you could use the c command to change the line:

                  ed -s staff.txt <<< $'/JHON/+2cn4242n.nwnq'
                  

                  Here, the replacement text needs to be entered separately (after) the c command, and ended with a period on its own (.n).

                  share|improve this answer

                  share|improve this answer

                  share|improve this answer

                  answered Nov 29 at 16:34

                  Jeff Schaller

                  37.4k1052121

                  37.4k1052121

                  • 1

                    Rob Pike would be proud!
                    – Stephen Kitt
                    Nov 29 at 17:37

                  • The succeeding comment on that post sounds like a new spin on the old joke: “I’ve been using ed for years, mostly because I can’t figure out how to exit it.”
                    – Jeff Schaller
                    Nov 29 at 17:41

                  • When i am using this command ed -s staff.txt <<< $’/JHON/+2s/.*/4242nwnq’ Its working fine but in some case JHON word in a Bracket [ ]. at that time this command not working properly. Its changing some another word that start with [xyz ] example. 1) ed -s staff.txt <<< $’/[JHON]/+2s/.*/4242nwnq’ [JHON] [MANAGER] [10000] Its changing first bracket word in file not changing [JHON]
                    – Sourabh Kumar
                    Nov 30 at 8:47

                  • 1

                    Rob Pike would be proud!
                    – Stephen Kitt
                    Nov 29 at 17:37

                  • The succeeding comment on that post sounds like a new spin on the old joke: “I’ve been using ed for years, mostly because I can’t figure out how to exit it.”
                    – Jeff Schaller
                    Nov 29 at 17:41

                  • When i am using this command ed -s staff.txt <<< $’/JHON/+2s/.*/4242nwnq’ Its working fine but in some case JHON word in a Bracket [ ]. at that time this command not working properly. Its changing some another word that start with [xyz ] example. 1) ed -s staff.txt <<< $’/[JHON]/+2s/.*/4242nwnq’ [JHON] [MANAGER] [10000] Its changing first bracket word in file not changing [JHON]
                    – Sourabh Kumar
                    Nov 30 at 8:47

                  1

                  1

                  Rob Pike would be proud!
                  – Stephen Kitt
                  Nov 29 at 17:37

                  Rob Pike would be proud!
                  – Stephen Kitt
                  Nov 29 at 17:37

                  The succeeding comment on that post sounds like a new spin on the old joke: “I’ve been using ed for years, mostly because I can’t figure out how to exit it.”
                  – Jeff Schaller
                  Nov 29 at 17:41

                  The succeeding comment on that post sounds like a new spin on the old joke: “I’ve been using ed for years, mostly because I can’t figure out how to exit it.”
                  – Jeff Schaller
                  Nov 29 at 17:41

                  When i am using this command ed -s staff.txt <<< $’/JHON/+2s/.*/4242nwnq’ Its working fine but in some case JHON word in a Bracket [ ]. at that time this command not working properly. Its changing some another word that start with [xyz ] example. 1) ed -s staff.txt <<< $’/[JHON]/+2s/.*/4242nwnq’ [JHON] [MANAGER] [10000] Its changing first bracket word in file not changing [JHON]
                  – Sourabh Kumar
                  Nov 30 at 8:47

                  When i am using this command ed -s staff.txt <<< $’/JHON/+2s/.*/4242nwnq’ Its working fine but in some case JHON word in a Bracket [ ]. at that time this command not working properly. Its changing some another word that start with [xyz ] example. 1) ed -s staff.txt <<< $’/[JHON]/+2s/.*/4242nwnq’ [JHON] [MANAGER] [10000] Its changing first bracket word in file not changing [JHON]
                  – Sourabh Kumar
                  Nov 30 at 8:47

                  up vote
                  3
                  down vote

                  Using awk:

                  awk 'f{$0="newvalue";f=0}/JHON/{print;getline;f=1}1'
                  

                  f{$0="newvalue";f=0} – If f is true set line to newvalue and reset f to 0

                  /JHON/{print;getline;f=1} – If pattern JHON is found print it, print the next line, and set f to 1

                  1 – print

                  share|improve this answer

                    up vote
                    3
                    down vote

                    Using awk:

                    awk 'f{$0="newvalue";f=0}/JHON/{print;getline;f=1}1'
                    

                    f{$0="newvalue";f=0} – If f is true set line to newvalue and reset f to 0

                    /JHON/{print;getline;f=1} – If pattern JHON is found print it, print the next line, and set f to 1

                    1 – print

                    share|improve this answer

                      up vote
                      3
                      down vote

                      up vote
                      3
                      down vote

                      Using awk:

                      awk 'f{$0="newvalue";f=0}/JHON/{print;getline;f=1}1'
                      

                      f{$0="newvalue";f=0} – If f is true set line to newvalue and reset f to 0

                      /JHON/{print;getline;f=1} – If pattern JHON is found print it, print the next line, and set f to 1

                      1 – print

                      share|improve this answer

                      Using awk:

                      awk 'f{$0="newvalue";f=0}/JHON/{print;getline;f=1}1'
                      

                      f{$0="newvalue";f=0} – If f is true set line to newvalue and reset f to 0

                      /JHON/{print;getline;f=1} – If pattern JHON is found print it, print the next line, and set f to 1

                      1 – print

                      share|improve this answer

                      share|improve this answer

                      share|improve this answer

                      answered Nov 29 at 16:59

                      Jesse_b

                      11.5k23063

                      11.5k23063

                          up vote
                          1
                          down vote

                          Yet another awk solution:

                          awk '/JHON/{c=0};(c!=""&&c++==2){$0="42"};{print};'
                          

                          sets a counter c when JHON is found. Then, if c is set, increment and when c==2, change line ($0) to “42”.

                          share|improve this answer

                            up vote
                            1
                            down vote

                            Yet another awk solution:

                            awk '/JHON/{c=0};(c!=""&&c++==2){$0="42"};{print};'
                            

                            sets a counter c when JHON is found. Then, if c is set, increment and when c==2, change line ($0) to “42”.

                            share|improve this answer

                              up vote
                              1
                              down vote

                              up vote
                              1
                              down vote

                              Yet another awk solution:

                              awk '/JHON/{c=0};(c!=""&&c++==2){$0="42"};{print};'
                              

                              sets a counter c when JHON is found. Then, if c is set, increment and when c==2, change line ($0) to “42”.

                              share|improve this answer

                              Yet another awk solution:

                              awk '/JHON/{c=0};(c!=""&&c++==2){$0="42"};{print};'
                              

                              sets a counter c when JHON is found. Then, if c is set, increment and when c==2, change line ($0) to “42”.

                              share|improve this answer

                              share|improve this answer

                              share|improve this answer

                              answered Nov 29 at 17:12

                              RoVo

                              2,379215

                              2,379215

                                  up vote
                                  1
                                  down vote

                                  if you have only one person (one register), you can use this code:

                                  awk '/JHON/{print $0; getline; print "SUPERVISOR"; getline; print "99999"; }' staff.txt
                                  

                                  If you staff.txt file has many persons (many registers) and you should replace only JHON data:

                                  awk '/.*/{if($0=="JHON"){print $0; getline; print "SUPERVISOR"; getline; print "99999"; } else {print $0;}}' staff2.txt
                                  

                                  Example of staff2.txt:

                                  JHON
                                  MANAGER
                                  10000
                                  ROGER
                                  TEACHER
                                  3000
                                  ROBERT
                                  SCIENTIST
                                  20000
                                  

                                  You should get this output:

                                  JHON
                                  SUPERVISOR
                                  99999
                                  ROGER
                                  TEACHER
                                  3000
                                  ROBERT
                                  SCIENTIST
                                  20000
                                  

                                  Another short way, to get the same output, could be:

                                  awk '/JHON/{print;getline; print "SUPERVISOR"; getline; $0="99999"}1' staff2.txt
                                  

                                  Explanation: if line is JHON, print JHON, go to the next line, print SUPERVISOR, go to the next line, replace it with 99999.
                                  if line is NOT JHON, only print it.

                                  share|improve this answer

                                    up vote
                                    1
                                    down vote

                                    if you have only one person (one register), you can use this code:

                                    awk '/JHON/{print $0; getline; print "SUPERVISOR"; getline; print "99999"; }' staff.txt
                                    

                                    If you staff.txt file has many persons (many registers) and you should replace only JHON data:

                                    awk '/.*/{if($0=="JHON"){print $0; getline; print "SUPERVISOR"; getline; print "99999"; } else {print $0;}}' staff2.txt
                                    

                                    Example of staff2.txt:

                                    JHON
                                    MANAGER
                                    10000
                                    ROGER
                                    TEACHER
                                    3000
                                    ROBERT
                                    SCIENTIST
                                    20000
                                    

                                    You should get this output:

                                    JHON
                                    SUPERVISOR
                                    99999
                                    ROGER
                                    TEACHER
                                    3000
                                    ROBERT
                                    SCIENTIST
                                    20000
                                    

                                    Another short way, to get the same output, could be:

                                    awk '/JHON/{print;getline; print "SUPERVISOR"; getline; $0="99999"}1' staff2.txt
                                    

                                    Explanation: if line is JHON, print JHON, go to the next line, print SUPERVISOR, go to the next line, replace it with 99999.
                                    if line is NOT JHON, only print it.

                                    share|improve this answer

                                      up vote
                                      1
                                      down vote

                                      up vote
                                      1
                                      down vote

                                      if you have only one person (one register), you can use this code:

                                      awk '/JHON/{print $0; getline; print "SUPERVISOR"; getline; print "99999"; }' staff.txt
                                      

                                      If you staff.txt file has many persons (many registers) and you should replace only JHON data:

                                      awk '/.*/{if($0=="JHON"){print $0; getline; print "SUPERVISOR"; getline; print "99999"; } else {print $0;}}' staff2.txt
                                      

                                      Example of staff2.txt:

                                      JHON
                                      MANAGER
                                      10000
                                      ROGER
                                      TEACHER
                                      3000
                                      ROBERT
                                      SCIENTIST
                                      20000
                                      

                                      You should get this output:

                                      JHON
                                      SUPERVISOR
                                      99999
                                      ROGER
                                      TEACHER
                                      3000
                                      ROBERT
                                      SCIENTIST
                                      20000
                                      

                                      Another short way, to get the same output, could be:

                                      awk '/JHON/{print;getline; print "SUPERVISOR"; getline; $0="99999"}1' staff2.txt
                                      

                                      Explanation: if line is JHON, print JHON, go to the next line, print SUPERVISOR, go to the next line, replace it with 99999.
                                      if line is NOT JHON, only print it.

                                      share|improve this answer

                                      if you have only one person (one register), you can use this code:

                                      awk '/JHON/{print $0; getline; print "SUPERVISOR"; getline; print "99999"; }' staff.txt
                                      

                                      If you staff.txt file has many persons (many registers) and you should replace only JHON data:

                                      awk '/.*/{if($0=="JHON"){print $0; getline; print "SUPERVISOR"; getline; print "99999"; } else {print $0;}}' staff2.txt
                                      

                                      Example of staff2.txt:

                                      JHON
                                      MANAGER
                                      10000
                                      ROGER
                                      TEACHER
                                      3000
                                      ROBERT
                                      SCIENTIST
                                      20000
                                      

                                      You should get this output:

                                      JHON
                                      SUPERVISOR
                                      99999
                                      ROGER
                                      TEACHER
                                      3000
                                      ROBERT
                                      SCIENTIST
                                      20000
                                      

                                      Another short way, to get the same output, could be:

                                      awk '/JHON/{print;getline; print "SUPERVISOR"; getline; $0="99999"}1' staff2.txt
                                      

                                      Explanation: if line is JHON, print JHON, go to the next line, print SUPERVISOR, go to the next line, replace it with 99999.
                                      if line is NOT JHON, only print it.

                                      share|improve this answer

                                      share|improve this answer

                                      share|improve this answer

                                      edited Nov 30 at 19:25

                                      answered Nov 30 at 19:05

                                      Rogelio Prieto

                                      213

                                      213

                                          up vote
                                          0
                                          down vote

                                          When I am using this command

                                          ed -s staff.txt <<< $'/JHON/+2s/.*/4242nwnq'
                                          

                                          Its working fine but in some case JHON word is in a Bracket [ ]
                                          at that time this command not working properly. Its changing some another word that start with [xyz ]

                                          example. 1)

                                          ed -s staff.txt <<< $'/[JHON]/+2s/.*/4242nwnq'
                                          [JHON]
                                          [MANAGER]
                                          [10000]
                                          

                                          Its changing first bracket word in file not changing [JHON].

                                          share|improve this answer

                                          • 1

                                            I think this is not an answer, you should write it as comment below the answer unix.stackexchange.com/a/484968/255251
                                            – Debian_yadav
                                            Nov 30 at 8:45

                                          up vote
                                          0
                                          down vote

                                          When I am using this command

                                          ed -s staff.txt <<< $'/JHON/+2s/.*/4242nwnq'
                                          

                                          Its working fine but in some case JHON word is in a Bracket [ ]
                                          at that time this command not working properly. Its changing some another word that start with [xyz ]

                                          example. 1)

                                          ed -s staff.txt <<< $'/[JHON]/+2s/.*/4242nwnq'
                                          [JHON]
                                          [MANAGER]
                                          [10000]
                                          

                                          Its changing first bracket word in file not changing [JHON].

                                          share|improve this answer

                                          • 1

                                            I think this is not an answer, you should write it as comment below the answer unix.stackexchange.com/a/484968/255251
                                            – Debian_yadav
                                            Nov 30 at 8:45

                                          up vote
                                          0
                                          down vote

                                          up vote
                                          0
                                          down vote

                                          When I am using this command

                                          ed -s staff.txt <<< $'/JHON/+2s/.*/4242nwnq'
                                          

                                          Its working fine but in some case JHON word is in a Bracket [ ]
                                          at that time this command not working properly. Its changing some another word that start with [xyz ]

                                          example. 1)

                                          ed -s staff.txt <<< $'/[JHON]/+2s/.*/4242nwnq'
                                          [JHON]
                                          [MANAGER]
                                          [10000]
                                          

                                          Its changing first bracket word in file not changing [JHON].

                                          share|improve this answer

                                          When I am using this command

                                          ed -s staff.txt <<< $'/JHON/+2s/.*/4242nwnq'
                                          

                                          Its working fine but in some case JHON word is in a Bracket [ ]
                                          at that time this command not working properly. Its changing some another word that start with [xyz ]

                                          example. 1)

                                          ed -s staff.txt <<< $'/[JHON]/+2s/.*/4242nwnq'
                                          [JHON]
                                          [MANAGER]
                                          [10000]
                                          

                                          Its changing first bracket word in file not changing [JHON].

                                          share|improve this answer

                                          share|improve this answer

                                          share|improve this answer

                                          edited Nov 30 at 8:53

                                          Debian_yadav

                                          1,3583922

                                          1,3583922

                                          answered Nov 30 at 8:41

                                          Sourabh Kumar

                                          42

                                          42

                                          • 1

                                            I think this is not an answer, you should write it as comment below the answer unix.stackexchange.com/a/484968/255251
                                            – Debian_yadav
                                            Nov 30 at 8:45

                                          • 1

                                            I think this is not an answer, you should write it as comment below the answer unix.stackexchange.com/a/484968/255251
                                            – Debian_yadav
                                            Nov 30 at 8:45

                                          1

                                          1

                                          I think this is not an answer, you should write it as comment below the answer unix.stackexchange.com/a/484968/255251
                                          – Debian_yadav
                                          Nov 30 at 8:45

                                          I think this is not an answer, you should write it as comment below the answer unix.stackexchange.com/a/484968/255251
                                          – Debian_yadav
                                          Nov 30 at 8:45

                                          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.

                                          Some of your past answers have not been well-received, and you’re in danger of being blocked from answering.

                                          Please pay close attention to the following guidance:

                                          • 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%2f484966%2fhow-to-find-word-and-change-only-next-second-line-word%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

                                          Related Post

                                          Leave a Reply

                                          Your email address will not be published. Required fields are marked *