print field before and after matching pattern of single linePrint nth line before the matched line, Matching line and nth line from the matched linePattern Matching and Delete the whole lineInsert new line after non matching patternreplace pattern matchingFind each line matching a pattern but print only the line above itInsert a line break before a numeric field or before an alphanumeric field that is just after a numeric fieldHow to add quotes to line break field values in a fileSed or awk - Insert a new line after Matching patternCompare data in 5th column with 6th column and print the least difference elementHow to get single character after space?

Not been paid even after reminding the Treasurer; what should I do?

Plato and the knowledge of the forms

Is there a way to prevent the production team from messing up my paper?

Did Captain America make out with his niece?

Based on what criteria do you add/not add icons to labels within a toolbar?

A verb for when some rights are not violated?

Is it double speak?

Probably terminated or laid off soon; confront or not?

Repeated! Factorials!

Tile the chessboard with four-colored triominoes

Vibration on the guitar when playing two strings

Is there a way to improve my grade after graduation?

Could an areostationary satellite help locate asteroids?

Nested Unlocked Packages requires Installation of Base Packages?

The Game of the Century - why didn't Byrne take the rook after he forked Fischer?

Why is the Vasa Museum in Stockholm so Popular?

How easy is it to get a gun illegally in the United States?

Why private jets such as GulfStream ones fly higher than other civil jets?

What's going on with an item that starts with an hbox?

How do I get the =LEFT function in excel, to also take the number zero as the first number?

Is a switch from R to Python worth it?

Definitional equality of two propositions about propositional equality

If the interviewer says "We have other interviews to conduct and then back to you in few days", is it a bad sign to not get the job?

How many years before enough atoms of your body are replaced to survive the sudden disappearance of the original body’s atoms?



print field before and after matching pattern of single line


Print nth line before the matched line, Matching line and nth line from the matched linePattern Matching and Delete the whole lineInsert new line after non matching patternreplace pattern matchingFind each line matching a pattern but print only the line above itInsert a line break before a numeric field or before an alphanumeric field that is just after a numeric fieldHow to add quotes to line break field values in a fileSed or awk - Insert a new line after Matching patternCompare data in 5th column with 6th column and print the least difference elementHow to get single character after space?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















My file contains the data below:



tail fn0 logfile
more tail3 fn0 logfile1
get than tail4 fn0 logfile2


I want to get output from the field before fn0 and the field after fn0.



Expected output:



tail logfile
tail3 logfile1
tail4 logfile2









share|improve this question









New contributor



rahul is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



























    2















    My file contains the data below:



    tail fn0 logfile
    more tail3 fn0 logfile1
    get than tail4 fn0 logfile2


    I want to get output from the field before fn0 and the field after fn0.



    Expected output:



    tail logfile
    tail3 logfile1
    tail4 logfile2









    share|improve this question









    New contributor



    rahul is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      2












      2








      2








      My file contains the data below:



      tail fn0 logfile
      more tail3 fn0 logfile1
      get than tail4 fn0 logfile2


      I want to get output from the field before fn0 and the field after fn0.



      Expected output:



      tail logfile
      tail3 logfile1
      tail4 logfile2









      share|improve this question









      New contributor



      rahul is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      My file contains the data below:



      tail fn0 logfile
      more tail3 fn0 logfile1
      get than tail4 fn0 logfile2


      I want to get output from the field before fn0 and the field after fn0.



      Expected output:



      tail logfile
      tail3 logfile1
      tail4 logfile2






      linux text-processing






      share|improve this question









      New contributor



      rahul is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share|improve this question









      New contributor



      rahul is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share|improve this question




      share|improve this question








      edited 10 hours ago









      Jeff Schaller

      48.5k11 gold badges72 silver badges162 bronze badges




      48.5k11 gold badges72 silver badges162 bronze badges






      New contributor



      rahul is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      asked 11 hours ago









      rahulrahul

      111 bronze badge




      111 bronze badge




      New contributor



      rahul is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      New contributor




      rahul is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.

























          4 Answers
          4






          active

          oldest

          votes


















          4














          You can use awk:



          awk -v pattern="fn0" 'for (i=0;i<=NF;i++) if ($i==pattern) print $(i-1),$(i+1) ' file


          or if you want to use a regex pattern:



          awk -v pattern="^fn0$" 'for (i=0;i<=NF;i++) if ($i~pattern) print $(i-1),$(i+1) ' file


          Output:



          tail logfile
          tail3 logfile1
          tail4 logfile2





          share|improve this answer


































            3














            With perl



            $ perl -lane '($i) = grep $F[$_] eq "fn0" 0..$#F;
            print "$F[$i-1] $F[$i+1]"' ip.txt
            tail logfile
            tail3 logfile1
            tail4 logfile2



            • -l will remove newline character from input line, and add it back while printing


            • -a split input line based on whitespaces, @F array will have the data


            • ($i) = grep $F[$_] eq "fn0" 0..$#F get index of the element whose exact content is fn0


            • print "$F[$i-1] $F[$i+1]" print the required fields



            With sed that supports ERE



            $ sed -E 's/^(.* )?([^ ]+) fn0 ([^ ]+).*/2 3/' ip.txt
            tail logfile
            tail3 logfile1
            tail4 logfile2



            • ^(.* )? optional fields at start of line


            • ([^ ]+) fn0 ([^ ]+) capture fields before and after fn0 (assuming single space as field separator)


            • .* rest of the line


            • 2 3 required fields in output





            share|improve this answer
































              0














              a solution with grep and sed:



              egrep -oh '[a-zA-Z0-9]+ fn0 [a-zA-Z0-9]+' testfile | sed 's/ fn0 / /'






              share|improve this answer








              New contributor



              markgraf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.




























                0














                $ sed 's/.*(tail[[:digit:]]*) fn[[:digit:]]*/1/' file
                tail logfile
                tail3 logfile1
                tail4 logfile2


                The sed expression is replaces everything on each line up to some substring matching tailXX fnYY with tailXX (where XX and YY are some positive integers).






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



                  );






                  rahul is a new contributor. Be nice, and check out our Code of Conduct.









                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f534129%2fprint-field-before-and-after-matching-pattern-of-single-line%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  4














                  You can use awk:



                  awk -v pattern="fn0" 'for (i=0;i<=NF;i++) if ($i==pattern) print $(i-1),$(i+1) ' file


                  or if you want to use a regex pattern:



                  awk -v pattern="^fn0$" 'for (i=0;i<=NF;i++) if ($i~pattern) print $(i-1),$(i+1) ' file


                  Output:



                  tail logfile
                  tail3 logfile1
                  tail4 logfile2





                  share|improve this answer































                    4














                    You can use awk:



                    awk -v pattern="fn0" 'for (i=0;i<=NF;i++) if ($i==pattern) print $(i-1),$(i+1) ' file


                    or if you want to use a regex pattern:



                    awk -v pattern="^fn0$" 'for (i=0;i<=NF;i++) if ($i~pattern) print $(i-1),$(i+1) ' file


                    Output:



                    tail logfile
                    tail3 logfile1
                    tail4 logfile2





                    share|improve this answer





























                      4












                      4








                      4







                      You can use awk:



                      awk -v pattern="fn0" 'for (i=0;i<=NF;i++) if ($i==pattern) print $(i-1),$(i+1) ' file


                      or if you want to use a regex pattern:



                      awk -v pattern="^fn0$" 'for (i=0;i<=NF;i++) if ($i~pattern) print $(i-1),$(i+1) ' file


                      Output:



                      tail logfile
                      tail3 logfile1
                      tail4 logfile2





                      share|improve this answer















                      You can use awk:



                      awk -v pattern="fn0" 'for (i=0;i<=NF;i++) if ($i==pattern) print $(i-1),$(i+1) ' file


                      or if you want to use a regex pattern:



                      awk -v pattern="^fn0$" 'for (i=0;i<=NF;i++) if ($i~pattern) print $(i-1),$(i+1) ' file


                      Output:



                      tail logfile
                      tail3 logfile1
                      tail4 logfile2






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 9 hours ago

























                      answered 9 hours ago









                      pLumopLumo

                      7,20814 silver badges32 bronze badges




                      7,20814 silver badges32 bronze badges


























                          3














                          With perl



                          $ perl -lane '($i) = grep $F[$_] eq "fn0" 0..$#F;
                          print "$F[$i-1] $F[$i+1]"' ip.txt
                          tail logfile
                          tail3 logfile1
                          tail4 logfile2



                          • -l will remove newline character from input line, and add it back while printing


                          • -a split input line based on whitespaces, @F array will have the data


                          • ($i) = grep $F[$_] eq "fn0" 0..$#F get index of the element whose exact content is fn0


                          • print "$F[$i-1] $F[$i+1]" print the required fields



                          With sed that supports ERE



                          $ sed -E 's/^(.* )?([^ ]+) fn0 ([^ ]+).*/2 3/' ip.txt
                          tail logfile
                          tail3 logfile1
                          tail4 logfile2



                          • ^(.* )? optional fields at start of line


                          • ([^ ]+) fn0 ([^ ]+) capture fields before and after fn0 (assuming single space as field separator)


                          • .* rest of the line


                          • 2 3 required fields in output





                          share|improve this answer





























                            3














                            With perl



                            $ perl -lane '($i) = grep $F[$_] eq "fn0" 0..$#F;
                            print "$F[$i-1] $F[$i+1]"' ip.txt
                            tail logfile
                            tail3 logfile1
                            tail4 logfile2



                            • -l will remove newline character from input line, and add it back while printing


                            • -a split input line based on whitespaces, @F array will have the data


                            • ($i) = grep $F[$_] eq "fn0" 0..$#F get index of the element whose exact content is fn0


                            • print "$F[$i-1] $F[$i+1]" print the required fields



                            With sed that supports ERE



                            $ sed -E 's/^(.* )?([^ ]+) fn0 ([^ ]+).*/2 3/' ip.txt
                            tail logfile
                            tail3 logfile1
                            tail4 logfile2



                            • ^(.* )? optional fields at start of line


                            • ([^ ]+) fn0 ([^ ]+) capture fields before and after fn0 (assuming single space as field separator)


                            • .* rest of the line


                            • 2 3 required fields in output





                            share|improve this answer



























                              3












                              3








                              3







                              With perl



                              $ perl -lane '($i) = grep $F[$_] eq "fn0" 0..$#F;
                              print "$F[$i-1] $F[$i+1]"' ip.txt
                              tail logfile
                              tail3 logfile1
                              tail4 logfile2



                              • -l will remove newline character from input line, and add it back while printing


                              • -a split input line based on whitespaces, @F array will have the data


                              • ($i) = grep $F[$_] eq "fn0" 0..$#F get index of the element whose exact content is fn0


                              • print "$F[$i-1] $F[$i+1]" print the required fields



                              With sed that supports ERE



                              $ sed -E 's/^(.* )?([^ ]+) fn0 ([^ ]+).*/2 3/' ip.txt
                              tail logfile
                              tail3 logfile1
                              tail4 logfile2



                              • ^(.* )? optional fields at start of line


                              • ([^ ]+) fn0 ([^ ]+) capture fields before and after fn0 (assuming single space as field separator)


                              • .* rest of the line


                              • 2 3 required fields in output





                              share|improve this answer













                              With perl



                              $ perl -lane '($i) = grep $F[$_] eq "fn0" 0..$#F;
                              print "$F[$i-1] $F[$i+1]"' ip.txt
                              tail logfile
                              tail3 logfile1
                              tail4 logfile2



                              • -l will remove newline character from input line, and add it back while printing


                              • -a split input line based on whitespaces, @F array will have the data


                              • ($i) = grep $F[$_] eq "fn0" 0..$#F get index of the element whose exact content is fn0


                              • print "$F[$i-1] $F[$i+1]" print the required fields



                              With sed that supports ERE



                              $ sed -E 's/^(.* )?([^ ]+) fn0 ([^ ]+).*/2 3/' ip.txt
                              tail logfile
                              tail3 logfile1
                              tail4 logfile2



                              • ^(.* )? optional fields at start of line


                              • ([^ ]+) fn0 ([^ ]+) capture fields before and after fn0 (assuming single space as field separator)


                              • .* rest of the line


                              • 2 3 required fields in output






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 8 hours ago









                              SundeepSundeep

                              7,7361 gold badge11 silver badges28 bronze badges




                              7,7361 gold badge11 silver badges28 bronze badges
























                                  0














                                  a solution with grep and sed:



                                  egrep -oh '[a-zA-Z0-9]+ fn0 [a-zA-Z0-9]+' testfile | sed 's/ fn0 / /'






                                  share|improve this answer








                                  New contributor



                                  markgraf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                  Check out our Code of Conduct.

























                                    0














                                    a solution with grep and sed:



                                    egrep -oh '[a-zA-Z0-9]+ fn0 [a-zA-Z0-9]+' testfile | sed 's/ fn0 / /'






                                    share|improve this answer








                                    New contributor



                                    markgraf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.























                                      0












                                      0








                                      0







                                      a solution with grep and sed:



                                      egrep -oh '[a-zA-Z0-9]+ fn0 [a-zA-Z0-9]+' testfile | sed 's/ fn0 / /'






                                      share|improve this answer








                                      New contributor



                                      markgraf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                      Check out our Code of Conduct.









                                      a solution with grep and sed:



                                      egrep -oh '[a-zA-Z0-9]+ fn0 [a-zA-Z0-9]+' testfile | sed 's/ fn0 / /'







                                      share|improve this answer








                                      New contributor



                                      markgraf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                      Check out our Code of Conduct.








                                      share|improve this answer



                                      share|improve this answer






                                      New contributor



                                      markgraf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                      Check out our Code of Conduct.








                                      answered 9 hours ago









                                      markgrafmarkgraf

                                      1205 bronze badges




                                      1205 bronze badges




                                      New contributor



                                      markgraf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                      Check out our Code of Conduct.




                                      New contributor




                                      markgraf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                      Check out our Code of Conduct.


























                                          0














                                          $ sed 's/.*(tail[[:digit:]]*) fn[[:digit:]]*/1/' file
                                          tail logfile
                                          tail3 logfile1
                                          tail4 logfile2


                                          The sed expression is replaces everything on each line up to some substring matching tailXX fnYY with tailXX (where XX and YY are some positive integers).






                                          share|improve this answer





























                                            0














                                            $ sed 's/.*(tail[[:digit:]]*) fn[[:digit:]]*/1/' file
                                            tail logfile
                                            tail3 logfile1
                                            tail4 logfile2


                                            The sed expression is replaces everything on each line up to some substring matching tailXX fnYY with tailXX (where XX and YY are some positive integers).






                                            share|improve this answer



























                                              0












                                              0








                                              0







                                              $ sed 's/.*(tail[[:digit:]]*) fn[[:digit:]]*/1/' file
                                              tail logfile
                                              tail3 logfile1
                                              tail4 logfile2


                                              The sed expression is replaces everything on each line up to some substring matching tailXX fnYY with tailXX (where XX and YY are some positive integers).






                                              share|improve this answer













                                              $ sed 's/.*(tail[[:digit:]]*) fn[[:digit:]]*/1/' file
                                              tail logfile
                                              tail3 logfile1
                                              tail4 logfile2


                                              The sed expression is replaces everything on each line up to some substring matching tailXX fnYY with tailXX (where XX and YY are some positive integers).







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered 9 hours ago









                                              KusalanandaKusalananda

                                              158k18 gold badges313 silver badges498 bronze badges




                                              158k18 gold badges313 silver badges498 bronze badges























                                                  rahul is a new contributor. Be nice, and check out our Code of Conduct.









                                                  draft saved

                                                  draft discarded


















                                                  rahul is a new contributor. Be nice, and check out our Code of Conduct.












                                                  rahul is a new contributor. Be nice, and check out our Code of Conduct.











                                                  rahul is a new contributor. Be nice, and check out our Code of Conduct.














                                                  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%2f534129%2fprint-field-before-and-after-matching-pattern-of-single-line%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

                                                  Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                                                  Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                                                  199年 目錄 大件事 到箇年出世嗰人 到箇年死嗰人 節慶、風俗習慣 導覽選單