What grep/awk/sed command to use to the output that i want2019 Community Moderator ElectionWhat are the differences between Perl, Python, AWK and sed?How to do a recursive find/replace of a string with awk or sed?What is the difference between sed and awk?How to output only captured groups with sed?sed command find and replace in file and overwrite file doesn't work, it empties the fileInsert a line at specific line number with sed or awkWhat are the differences among grep, awk & sed?BASH: grep/awk/sed to extract variable dataHow to remove unique values from an HTML select list with linux program like sed, awk, or grep?Saved format output in columns [grep, sed, awk or ?]

Is divide-by-zero a security vulnerability?

How does insurance birth control work?

Split a number into equal parts given the number of parts

I encountered my boss during an on-site interview at another company. Should I bring it up when seeing him next time?

An Undercover Army

How can I conditionally format my HTML table?

Rationale to prefer local variables over instance variables?

Is there a math equivalent to the conditional ternary operator?

Book about a time-travel war fought by computers

How does signal strength relate to bandwidth?

Why are special aircraft used for the carriers in the United States Navy?

Quitting employee has privileged access to critical information

How to kill a localhost:8080

I've given my players a lot of magic items. Is it reasonable for me to give them harder encounters?

Lock enemy's y-axis when using Vector3.MoveTowards to follow the player

Find maximum of the output from reduce

Did Amazon pay $0 in taxes last year?

Formatting a table to look nice

Practical reasons to have both a large police force and bounty hunting network?

Why do phishing e-mails use faked e-mail addresses instead of the real one?

I can't die. Who am I?

Are there other characters in the Star Wars universe who had damaged bodies and needed to wear an outfit like Darth Vader?

Why would the IRS ask for birth certificates or even audit a small tax return?

Reason why dimensional travelling would be restricted



What grep/awk/sed command to use to the output that i want



2019 Community Moderator ElectionWhat are the differences between Perl, Python, AWK and sed?How to do a recursive find/replace of a string with awk or sed?What is the difference between sed and awk?How to output only captured groups with sed?sed command find and replace in file and overwrite file doesn't work, it empties the fileInsert a line at specific line number with sed or awkWhat are the differences among grep, awk & sed?BASH: grep/awk/sed to extract variable dataHow to remove unique values from an HTML select list with linux program like sed, awk, or grep?Saved format output in columns [grep, sed, awk or ?]










0















I have a input file like this:



COL1: VALUE1 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyyy23, NAME=AUDIT
COL1: VALUE2 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyy23, NAME=generic
XYZ:2, COL1: 289 , TREK:MRP, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, NAME=Oil, trial=TREE


I want to get the output like this:



 COL1: VALUE1 , NAME=AUDIT
COL1: VALUE2 , NAME=generic
COL1: 289 , NAME=Oil


How can I achieve this using awk/grep/sed on command line without using any advanced versions of awk like gawk, nawk etc?



Basically I want to get the value of COL1 (i.e. text after : and =) & NAME irrespective of where they are in the line.

See that location of NAME column got slightly altered.



This is what I could come up with:



awk -F"," 'print $1, $6' file.txt
COL1: VALUE1 NAME=AUDIT
COL1: VALUE2 NAME=generic
XYZ:2 NAME=Oil









share|improve this question




























    0















    I have a input file like this:



    COL1: VALUE1 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyyy23, NAME=AUDIT
    COL1: VALUE2 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyy23, NAME=generic
    XYZ:2, COL1: 289 , TREK:MRP, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, NAME=Oil, trial=TREE


    I want to get the output like this:



     COL1: VALUE1 , NAME=AUDIT
    COL1: VALUE2 , NAME=generic
    COL1: 289 , NAME=Oil


    How can I achieve this using awk/grep/sed on command line without using any advanced versions of awk like gawk, nawk etc?



    Basically I want to get the value of COL1 (i.e. text after : and =) & NAME irrespective of where they are in the line.

    See that location of NAME column got slightly altered.



    This is what I could come up with:



    awk -F"," 'print $1, $6' file.txt
    COL1: VALUE1 NAME=AUDIT
    COL1: VALUE2 NAME=generic
    XYZ:2 NAME=Oil









    share|improve this question


























      0












      0








      0








      I have a input file like this:



      COL1: VALUE1 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyyy23, NAME=AUDIT
      COL1: VALUE2 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyy23, NAME=generic
      XYZ:2, COL1: 289 , TREK:MRP, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, NAME=Oil, trial=TREE


      I want to get the output like this:



       COL1: VALUE1 , NAME=AUDIT
      COL1: VALUE2 , NAME=generic
      COL1: 289 , NAME=Oil


      How can I achieve this using awk/grep/sed on command line without using any advanced versions of awk like gawk, nawk etc?



      Basically I want to get the value of COL1 (i.e. text after : and =) & NAME irrespective of where they are in the line.

      See that location of NAME column got slightly altered.



      This is what I could come up with:



      awk -F"," 'print $1, $6' file.txt
      COL1: VALUE1 NAME=AUDIT
      COL1: VALUE2 NAME=generic
      XYZ:2 NAME=Oil









      share|improve this question
















      I have a input file like this:



      COL1: VALUE1 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyyy23, NAME=AUDIT
      COL1: VALUE2 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyy23, NAME=generic
      XYZ:2, COL1: 289 , TREK:MRP, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, NAME=Oil, trial=TREE


      I want to get the output like this:



       COL1: VALUE1 , NAME=AUDIT
      COL1: VALUE2 , NAME=generic
      COL1: 289 , NAME=Oil


      How can I achieve this using awk/grep/sed on command line without using any advanced versions of awk like gawk, nawk etc?



      Basically I want to get the value of COL1 (i.e. text after : and =) & NAME irrespective of where they are in the line.

      See that location of NAME column got slightly altered.



      This is what I could come up with:



      awk -F"," 'print $1, $6' file.txt
      COL1: VALUE1 NAME=AUDIT
      COL1: VALUE2 NAME=generic
      XYZ:2 NAME=Oil






      perl awk sed






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 16 hours ago









      Tiw

      3,57341229




      3,57341229










      asked 19 hours ago









      SachSach

      575




      575






















          4 Answers
          4






          active

          oldest

          votes


















          1














          Could you please try following(tested and written in GNU awk).



          awk '
          BEGIN
          OFS=" , "

          match($0,/COL[0-9]+: [^,]*/)
          val=substr($0,RSTART,RLENGTH)
          match($0,/NAME[^,]*/)
          print val OFS substr($0,RSTART,RLENGTH)
          val=""

          ' Input_file


          I have clubbed the match(es) of string COL and NAME in each line so in case any line do not have string COL in it, it may not print anything in it.





          In case string COL is not found in a line and you still want to print NAME string match then try following.



          awk '
          BEGIN
          OFS=" , "

          match($0,/COL[0-9]+: [^,]*/)
          val=substr($0,RSTART,RLENGTH)

          match($0,/NAME[^,]*/)
          if(val)
          printf "%s%s",val,OFS

          print substr($0,RSTART,RLENGTH)

          ' Input_file




          Explanation: Adding explanation for above code now.



          awk ' ##Starting awk program heer.
          BEGIN ##Starting BEGIN section for awk code here.
          OFS=" , " ##Setting OFS output field separator as space comma space here.
          ##Closing BEGIN section here.
          match($0,/COL[0-9]+: [^,]*/) ##Using match of awk OOTB function to match a REGEX string COL till comma here.
          val=substr($0,RSTART,RLENGTH) ##If a match is foundthen creating variable val whose value is sub string of matched regex starting to till end value of it.
          match($0,/NAME[^,]*/) ##Again using match to match string from NAME to till next comma comes.
          print val OFS substr($0,RSTART,RLENGTH) ##Printing value of variable val OFS and substring of current line whose sarting point is RSTART and end point is RLENGTH.
          val="" ##Nullifying variable val here.

          ' Input_file ##Mentioning Input_file name here.


          Adding reference from man awk page:



           RSTART The index of the first character matched by match(); 0 if no match. (This implies that character indices start at one.)

          RLENGTH The length of the string matched by match(); -1 if no match.





          share|improve this answer




















          • 1





            works perfect, can you please explain it to me, will appreciate if you do

            – Sach
            18 hours ago






          • 1





            @Sach fyi, this awk solution is using gawk, which stands for GNU awk. awk and gawk can point to same executable, and in your case, I think you are already using GNU awk since you said it's working. Better to check your version (awk --version) and to know the difference, and if you plan to migrate the code to other platform, bear in mind that it does not guarantee that it will work.

            – Tiw
            17 hours ago






          • 2





            awk --version GNU Awk 3.1.7

            – Sach
            17 hours ago






          • 1





            @Sach, IMHO man awk is BEST reference, after that you could go through SO posts too there is lot to learn from this GREAT forums. It is simple, if a match is found then RSTART tells its starting indexing point and RLENGTH means that full length of matched index. Also [^,]* means match till first comma occures in current line.

            – RavinderSingh13
            17 hours ago







          • 1





            on moba this is the version :awk --version awk: unknown option -- version BusyBox v1.22.1 (2015-11-10 11:07:12 ) multi-call binary.

            – Sach
            17 hours ago



















          3














          You can try Perl one-liner



           perl -lne ' /(COL1:s*S+).+(NAME=w+)/ and print "$1,t$2" ' input_file


          with your inputs:



          $ cat sach.txt
          COL1: VALUE1 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyyy23, NAME=AUDIT
          COL1: VALUE2 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyy23, NAME=generic
          XYZ:2, COL1: 289 , TREK:MRP, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, NAME=Oil, trial=TREE
          $ perl -lne ' /(COL1:s*S+).+(NAME=w+)/ and print "$1,t$2" ' sach.txt
          COL1: VALUE1, NAME=AUDIT
          COL1: VALUE2, NAME=generic
          COL1: 289, NAME=Oil
          $


          Explanation:



          perl -lne # use -n for suppressing print default at the end of each line

          ' /(COL1:s*S+).+(NAME=w+)/ # Match pattern and capture them in capture groups first () will be $1 and second () will be in $2
          # First () matches COL1:s*S+ => COL1: followed by zero or more spaces using s* and S+ for non-space characters
          # .+ => match all strings between first () and second ()
          # Seecond () matches NAME followed by a word w+


          and # bind on the success of previous condition /..../
          print "$1,t$2" # print the $1 and $2 captured variables

          ' input_file





          share|improve this answer

























          • Ahh... perl, always a good choice, and so powerful as well as elegant PRE is :)

            – Tiw
            17 hours ago






          • 1





            @Tiw.. thank you for the appreciation..yep, Perl is the right tool for regex problems like this..

            – stack0114106
            17 hours ago











          • @stack0114106 can you please explain it a bit to me. this is very nice

            – Sach
            17 hours ago











          • yes.. will add explanation

            – stack0114106
            17 hours ago











          • Added explanation to the code.. pls check

            – stack0114106
            17 hours ago


















          0














          With grep you can maybe try something like that :



          while read line; do COL=$(echo $line | grep -o "COL1:.*,"); NAME=$(echo $line | grep -o "NAME=[a-zA-Z]*"); echo $COL $NAME >> new_file.txt; done < your_file.txt 


          The regexp in this example assume that the value after COL1 are always followed by a "," (then it take every characters between the : and ,) so you might have to adapt it to fit your file (same for the regexp used for NAME).






          share|improve this answer








          New contributor




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



























            0














            Try this:



            $ sed 'H;s/.*NAME=/NAME=/;s/ *,.*//;x;s/^.*COL1/COL1/;s/ *,.*//;G;s/n/t, /;' file
            COL1: VALUE1 , NAME=AUDIT
            COL1: VALUE2 , NAME=generic
            COL1: 289 , NAME=Oil


            Used hold space, and used t for alignment.






            share|improve this answer
























              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              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: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              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%2fstackoverflow.com%2fquestions%2f55021528%2fwhat-grep-awk-sed-command-to-use-to-the-output-that-i-want%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









              1














              Could you please try following(tested and written in GNU awk).



              awk '
              BEGIN
              OFS=" , "

              match($0,/COL[0-9]+: [^,]*/)
              val=substr($0,RSTART,RLENGTH)
              match($0,/NAME[^,]*/)
              print val OFS substr($0,RSTART,RLENGTH)
              val=""

              ' Input_file


              I have clubbed the match(es) of string COL and NAME in each line so in case any line do not have string COL in it, it may not print anything in it.





              In case string COL is not found in a line and you still want to print NAME string match then try following.



              awk '
              BEGIN
              OFS=" , "

              match($0,/COL[0-9]+: [^,]*/)
              val=substr($0,RSTART,RLENGTH)

              match($0,/NAME[^,]*/)
              if(val)
              printf "%s%s",val,OFS

              print substr($0,RSTART,RLENGTH)

              ' Input_file




              Explanation: Adding explanation for above code now.



              awk ' ##Starting awk program heer.
              BEGIN ##Starting BEGIN section for awk code here.
              OFS=" , " ##Setting OFS output field separator as space comma space here.
              ##Closing BEGIN section here.
              match($0,/COL[0-9]+: [^,]*/) ##Using match of awk OOTB function to match a REGEX string COL till comma here.
              val=substr($0,RSTART,RLENGTH) ##If a match is foundthen creating variable val whose value is sub string of matched regex starting to till end value of it.
              match($0,/NAME[^,]*/) ##Again using match to match string from NAME to till next comma comes.
              print val OFS substr($0,RSTART,RLENGTH) ##Printing value of variable val OFS and substring of current line whose sarting point is RSTART and end point is RLENGTH.
              val="" ##Nullifying variable val here.

              ' Input_file ##Mentioning Input_file name here.


              Adding reference from man awk page:



               RSTART The index of the first character matched by match(); 0 if no match. (This implies that character indices start at one.)

              RLENGTH The length of the string matched by match(); -1 if no match.





              share|improve this answer




















              • 1





                works perfect, can you please explain it to me, will appreciate if you do

                – Sach
                18 hours ago






              • 1





                @Sach fyi, this awk solution is using gawk, which stands for GNU awk. awk and gawk can point to same executable, and in your case, I think you are already using GNU awk since you said it's working. Better to check your version (awk --version) and to know the difference, and if you plan to migrate the code to other platform, bear in mind that it does not guarantee that it will work.

                – Tiw
                17 hours ago






              • 2





                awk --version GNU Awk 3.1.7

                – Sach
                17 hours ago






              • 1





                @Sach, IMHO man awk is BEST reference, after that you could go through SO posts too there is lot to learn from this GREAT forums. It is simple, if a match is found then RSTART tells its starting indexing point and RLENGTH means that full length of matched index. Also [^,]* means match till first comma occures in current line.

                – RavinderSingh13
                17 hours ago







              • 1





                on moba this is the version :awk --version awk: unknown option -- version BusyBox v1.22.1 (2015-11-10 11:07:12 ) multi-call binary.

                – Sach
                17 hours ago
















              1














              Could you please try following(tested and written in GNU awk).



              awk '
              BEGIN
              OFS=" , "

              match($0,/COL[0-9]+: [^,]*/)
              val=substr($0,RSTART,RLENGTH)
              match($0,/NAME[^,]*/)
              print val OFS substr($0,RSTART,RLENGTH)
              val=""

              ' Input_file


              I have clubbed the match(es) of string COL and NAME in each line so in case any line do not have string COL in it, it may not print anything in it.





              In case string COL is not found in a line and you still want to print NAME string match then try following.



              awk '
              BEGIN
              OFS=" , "

              match($0,/COL[0-9]+: [^,]*/)
              val=substr($0,RSTART,RLENGTH)

              match($0,/NAME[^,]*/)
              if(val)
              printf "%s%s",val,OFS

              print substr($0,RSTART,RLENGTH)

              ' Input_file




              Explanation: Adding explanation for above code now.



              awk ' ##Starting awk program heer.
              BEGIN ##Starting BEGIN section for awk code here.
              OFS=" , " ##Setting OFS output field separator as space comma space here.
              ##Closing BEGIN section here.
              match($0,/COL[0-9]+: [^,]*/) ##Using match of awk OOTB function to match a REGEX string COL till comma here.
              val=substr($0,RSTART,RLENGTH) ##If a match is foundthen creating variable val whose value is sub string of matched regex starting to till end value of it.
              match($0,/NAME[^,]*/) ##Again using match to match string from NAME to till next comma comes.
              print val OFS substr($0,RSTART,RLENGTH) ##Printing value of variable val OFS and substring of current line whose sarting point is RSTART and end point is RLENGTH.
              val="" ##Nullifying variable val here.

              ' Input_file ##Mentioning Input_file name here.


              Adding reference from man awk page:



               RSTART The index of the first character matched by match(); 0 if no match. (This implies that character indices start at one.)

              RLENGTH The length of the string matched by match(); -1 if no match.





              share|improve this answer




















              • 1





                works perfect, can you please explain it to me, will appreciate if you do

                – Sach
                18 hours ago






              • 1





                @Sach fyi, this awk solution is using gawk, which stands for GNU awk. awk and gawk can point to same executable, and in your case, I think you are already using GNU awk since you said it's working. Better to check your version (awk --version) and to know the difference, and if you plan to migrate the code to other platform, bear in mind that it does not guarantee that it will work.

                – Tiw
                17 hours ago






              • 2





                awk --version GNU Awk 3.1.7

                – Sach
                17 hours ago






              • 1





                @Sach, IMHO man awk is BEST reference, after that you could go through SO posts too there is lot to learn from this GREAT forums. It is simple, if a match is found then RSTART tells its starting indexing point and RLENGTH means that full length of matched index. Also [^,]* means match till first comma occures in current line.

                – RavinderSingh13
                17 hours ago







              • 1





                on moba this is the version :awk --version awk: unknown option -- version BusyBox v1.22.1 (2015-11-10 11:07:12 ) multi-call binary.

                – Sach
                17 hours ago














              1












              1








              1







              Could you please try following(tested and written in GNU awk).



              awk '
              BEGIN
              OFS=" , "

              match($0,/COL[0-9]+: [^,]*/)
              val=substr($0,RSTART,RLENGTH)
              match($0,/NAME[^,]*/)
              print val OFS substr($0,RSTART,RLENGTH)
              val=""

              ' Input_file


              I have clubbed the match(es) of string COL and NAME in each line so in case any line do not have string COL in it, it may not print anything in it.





              In case string COL is not found in a line and you still want to print NAME string match then try following.



              awk '
              BEGIN
              OFS=" , "

              match($0,/COL[0-9]+: [^,]*/)
              val=substr($0,RSTART,RLENGTH)

              match($0,/NAME[^,]*/)
              if(val)
              printf "%s%s",val,OFS

              print substr($0,RSTART,RLENGTH)

              ' Input_file




              Explanation: Adding explanation for above code now.



              awk ' ##Starting awk program heer.
              BEGIN ##Starting BEGIN section for awk code here.
              OFS=" , " ##Setting OFS output field separator as space comma space here.
              ##Closing BEGIN section here.
              match($0,/COL[0-9]+: [^,]*/) ##Using match of awk OOTB function to match a REGEX string COL till comma here.
              val=substr($0,RSTART,RLENGTH) ##If a match is foundthen creating variable val whose value is sub string of matched regex starting to till end value of it.
              match($0,/NAME[^,]*/) ##Again using match to match string from NAME to till next comma comes.
              print val OFS substr($0,RSTART,RLENGTH) ##Printing value of variable val OFS and substring of current line whose sarting point is RSTART and end point is RLENGTH.
              val="" ##Nullifying variable val here.

              ' Input_file ##Mentioning Input_file name here.


              Adding reference from man awk page:



               RSTART The index of the first character matched by match(); 0 if no match. (This implies that character indices start at one.)

              RLENGTH The length of the string matched by match(); -1 if no match.





              share|improve this answer















              Could you please try following(tested and written in GNU awk).



              awk '
              BEGIN
              OFS=" , "

              match($0,/COL[0-9]+: [^,]*/)
              val=substr($0,RSTART,RLENGTH)
              match($0,/NAME[^,]*/)
              print val OFS substr($0,RSTART,RLENGTH)
              val=""

              ' Input_file


              I have clubbed the match(es) of string COL and NAME in each line so in case any line do not have string COL in it, it may not print anything in it.





              In case string COL is not found in a line and you still want to print NAME string match then try following.



              awk '
              BEGIN
              OFS=" , "

              match($0,/COL[0-9]+: [^,]*/)
              val=substr($0,RSTART,RLENGTH)

              match($0,/NAME[^,]*/)
              if(val)
              printf "%s%s",val,OFS

              print substr($0,RSTART,RLENGTH)

              ' Input_file




              Explanation: Adding explanation for above code now.



              awk ' ##Starting awk program heer.
              BEGIN ##Starting BEGIN section for awk code here.
              OFS=" , " ##Setting OFS output field separator as space comma space here.
              ##Closing BEGIN section here.
              match($0,/COL[0-9]+: [^,]*/) ##Using match of awk OOTB function to match a REGEX string COL till comma here.
              val=substr($0,RSTART,RLENGTH) ##If a match is foundthen creating variable val whose value is sub string of matched regex starting to till end value of it.
              match($0,/NAME[^,]*/) ##Again using match to match string from NAME to till next comma comes.
              print val OFS substr($0,RSTART,RLENGTH) ##Printing value of variable val OFS and substring of current line whose sarting point is RSTART and end point is RLENGTH.
              val="" ##Nullifying variable val here.

              ' Input_file ##Mentioning Input_file name here.


              Adding reference from man awk page:



               RSTART The index of the first character matched by match(); 0 if no match. (This implies that character indices start at one.)

              RLENGTH The length of the string matched by match(); -1 if no match.






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 17 hours ago

























              answered 19 hours ago









              RavinderSingh13RavinderSingh13

              29.3k41639




              29.3k41639







              • 1





                works perfect, can you please explain it to me, will appreciate if you do

                – Sach
                18 hours ago






              • 1





                @Sach fyi, this awk solution is using gawk, which stands for GNU awk. awk and gawk can point to same executable, and in your case, I think you are already using GNU awk since you said it's working. Better to check your version (awk --version) and to know the difference, and if you plan to migrate the code to other platform, bear in mind that it does not guarantee that it will work.

                – Tiw
                17 hours ago






              • 2





                awk --version GNU Awk 3.1.7

                – Sach
                17 hours ago






              • 1





                @Sach, IMHO man awk is BEST reference, after that you could go through SO posts too there is lot to learn from this GREAT forums. It is simple, if a match is found then RSTART tells its starting indexing point and RLENGTH means that full length of matched index. Also [^,]* means match till first comma occures in current line.

                – RavinderSingh13
                17 hours ago







              • 1





                on moba this is the version :awk --version awk: unknown option -- version BusyBox v1.22.1 (2015-11-10 11:07:12 ) multi-call binary.

                – Sach
                17 hours ago













              • 1





                works perfect, can you please explain it to me, will appreciate if you do

                – Sach
                18 hours ago






              • 1





                @Sach fyi, this awk solution is using gawk, which stands for GNU awk. awk and gawk can point to same executable, and in your case, I think you are already using GNU awk since you said it's working. Better to check your version (awk --version) and to know the difference, and if you plan to migrate the code to other platform, bear in mind that it does not guarantee that it will work.

                – Tiw
                17 hours ago






              • 2





                awk --version GNU Awk 3.1.7

                – Sach
                17 hours ago






              • 1





                @Sach, IMHO man awk is BEST reference, after that you could go through SO posts too there is lot to learn from this GREAT forums. It is simple, if a match is found then RSTART tells its starting indexing point and RLENGTH means that full length of matched index. Also [^,]* means match till first comma occures in current line.

                – RavinderSingh13
                17 hours ago







              • 1





                on moba this is the version :awk --version awk: unknown option -- version BusyBox v1.22.1 (2015-11-10 11:07:12 ) multi-call binary.

                – Sach
                17 hours ago








              1




              1





              works perfect, can you please explain it to me, will appreciate if you do

              – Sach
              18 hours ago





              works perfect, can you please explain it to me, will appreciate if you do

              – Sach
              18 hours ago




              1




              1





              @Sach fyi, this awk solution is using gawk, which stands for GNU awk. awk and gawk can point to same executable, and in your case, I think you are already using GNU awk since you said it's working. Better to check your version (awk --version) and to know the difference, and if you plan to migrate the code to other platform, bear in mind that it does not guarantee that it will work.

              – Tiw
              17 hours ago





              @Sach fyi, this awk solution is using gawk, which stands for GNU awk. awk and gawk can point to same executable, and in your case, I think you are already using GNU awk since you said it's working. Better to check your version (awk --version) and to know the difference, and if you plan to migrate the code to other platform, bear in mind that it does not guarantee that it will work.

              – Tiw
              17 hours ago




              2




              2





              awk --version GNU Awk 3.1.7

              – Sach
              17 hours ago





              awk --version GNU Awk 3.1.7

              – Sach
              17 hours ago




              1




              1





              @Sach, IMHO man awk is BEST reference, after that you could go through SO posts too there is lot to learn from this GREAT forums. It is simple, if a match is found then RSTART tells its starting indexing point and RLENGTH means that full length of matched index. Also [^,]* means match till first comma occures in current line.

              – RavinderSingh13
              17 hours ago






              @Sach, IMHO man awk is BEST reference, after that you could go through SO posts too there is lot to learn from this GREAT forums. It is simple, if a match is found then RSTART tells its starting indexing point and RLENGTH means that full length of matched index. Also [^,]* means match till first comma occures in current line.

              – RavinderSingh13
              17 hours ago





              1




              1





              on moba this is the version :awk --version awk: unknown option -- version BusyBox v1.22.1 (2015-11-10 11:07:12 ) multi-call binary.

              – Sach
              17 hours ago






              on moba this is the version :awk --version awk: unknown option -- version BusyBox v1.22.1 (2015-11-10 11:07:12 ) multi-call binary.

              – Sach
              17 hours ago














              3














              You can try Perl one-liner



               perl -lne ' /(COL1:s*S+).+(NAME=w+)/ and print "$1,t$2" ' input_file


              with your inputs:



              $ cat sach.txt
              COL1: VALUE1 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyyy23, NAME=AUDIT
              COL1: VALUE2 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyy23, NAME=generic
              XYZ:2, COL1: 289 , TREK:MRP, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, NAME=Oil, trial=TREE
              $ perl -lne ' /(COL1:s*S+).+(NAME=w+)/ and print "$1,t$2" ' sach.txt
              COL1: VALUE1, NAME=AUDIT
              COL1: VALUE2, NAME=generic
              COL1: 289, NAME=Oil
              $


              Explanation:



              perl -lne # use -n for suppressing print default at the end of each line

              ' /(COL1:s*S+).+(NAME=w+)/ # Match pattern and capture them in capture groups first () will be $1 and second () will be in $2
              # First () matches COL1:s*S+ => COL1: followed by zero or more spaces using s* and S+ for non-space characters
              # .+ => match all strings between first () and second ()
              # Seecond () matches NAME followed by a word w+


              and # bind on the success of previous condition /..../
              print "$1,t$2" # print the $1 and $2 captured variables

              ' input_file





              share|improve this answer

























              • Ahh... perl, always a good choice, and so powerful as well as elegant PRE is :)

                – Tiw
                17 hours ago






              • 1





                @Tiw.. thank you for the appreciation..yep, Perl is the right tool for regex problems like this..

                – stack0114106
                17 hours ago











              • @stack0114106 can you please explain it a bit to me. this is very nice

                – Sach
                17 hours ago











              • yes.. will add explanation

                – stack0114106
                17 hours ago











              • Added explanation to the code.. pls check

                – stack0114106
                17 hours ago















              3














              You can try Perl one-liner



               perl -lne ' /(COL1:s*S+).+(NAME=w+)/ and print "$1,t$2" ' input_file


              with your inputs:



              $ cat sach.txt
              COL1: VALUE1 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyyy23, NAME=AUDIT
              COL1: VALUE2 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyy23, NAME=generic
              XYZ:2, COL1: 289 , TREK:MRP, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, NAME=Oil, trial=TREE
              $ perl -lne ' /(COL1:s*S+).+(NAME=w+)/ and print "$1,t$2" ' sach.txt
              COL1: VALUE1, NAME=AUDIT
              COL1: VALUE2, NAME=generic
              COL1: 289, NAME=Oil
              $


              Explanation:



              perl -lne # use -n for suppressing print default at the end of each line

              ' /(COL1:s*S+).+(NAME=w+)/ # Match pattern and capture them in capture groups first () will be $1 and second () will be in $2
              # First () matches COL1:s*S+ => COL1: followed by zero or more spaces using s* and S+ for non-space characters
              # .+ => match all strings between first () and second ()
              # Seecond () matches NAME followed by a word w+


              and # bind on the success of previous condition /..../
              print "$1,t$2" # print the $1 and $2 captured variables

              ' input_file





              share|improve this answer

























              • Ahh... perl, always a good choice, and so powerful as well as elegant PRE is :)

                – Tiw
                17 hours ago






              • 1





                @Tiw.. thank you for the appreciation..yep, Perl is the right tool for regex problems like this..

                – stack0114106
                17 hours ago











              • @stack0114106 can you please explain it a bit to me. this is very nice

                – Sach
                17 hours ago











              • yes.. will add explanation

                – stack0114106
                17 hours ago











              • Added explanation to the code.. pls check

                – stack0114106
                17 hours ago













              3












              3








              3







              You can try Perl one-liner



               perl -lne ' /(COL1:s*S+).+(NAME=w+)/ and print "$1,t$2" ' input_file


              with your inputs:



              $ cat sach.txt
              COL1: VALUE1 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyyy23, NAME=AUDIT
              COL1: VALUE2 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyy23, NAME=generic
              XYZ:2, COL1: 289 , TREK:MRP, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, NAME=Oil, trial=TREE
              $ perl -lne ' /(COL1:s*S+).+(NAME=w+)/ and print "$1,t$2" ' sach.txt
              COL1: VALUE1, NAME=AUDIT
              COL1: VALUE2, NAME=generic
              COL1: 289, NAME=Oil
              $


              Explanation:



              perl -lne # use -n for suppressing print default at the end of each line

              ' /(COL1:s*S+).+(NAME=w+)/ # Match pattern and capture them in capture groups first () will be $1 and second () will be in $2
              # First () matches COL1:s*S+ => COL1: followed by zero or more spaces using s* and S+ for non-space characters
              # .+ => match all strings between first () and second ()
              # Seecond () matches NAME followed by a word w+


              and # bind on the success of previous condition /..../
              print "$1,t$2" # print the $1 and $2 captured variables

              ' input_file





              share|improve this answer















              You can try Perl one-liner



               perl -lne ' /(COL1:s*S+).+(NAME=w+)/ and print "$1,t$2" ' input_file


              with your inputs:



              $ cat sach.txt
              COL1: VALUE1 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyyy23, NAME=AUDIT
              COL1: VALUE2 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyy23, NAME=generic
              XYZ:2, COL1: 289 , TREK:MRP, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, NAME=Oil, trial=TREE
              $ perl -lne ' /(COL1:s*S+).+(NAME=w+)/ and print "$1,t$2" ' sach.txt
              COL1: VALUE1, NAME=AUDIT
              COL1: VALUE2, NAME=generic
              COL1: 289, NAME=Oil
              $


              Explanation:



              perl -lne # use -n for suppressing print default at the end of each line

              ' /(COL1:s*S+).+(NAME=w+)/ # Match pattern and capture them in capture groups first () will be $1 and second () will be in $2
              # First () matches COL1:s*S+ => COL1: followed by zero or more spaces using s* and S+ for non-space characters
              # .+ => match all strings between first () and second ()
              # Seecond () matches NAME followed by a word w+


              and # bind on the success of previous condition /..../
              print "$1,t$2" # print the $1 and $2 captured variables

              ' input_file






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 17 hours ago

























              answered 17 hours ago









              stack0114106stack0114106

              4,1032421




              4,1032421












              • Ahh... perl, always a good choice, and so powerful as well as elegant PRE is :)

                – Tiw
                17 hours ago






              • 1





                @Tiw.. thank you for the appreciation..yep, Perl is the right tool for regex problems like this..

                – stack0114106
                17 hours ago











              • @stack0114106 can you please explain it a bit to me. this is very nice

                – Sach
                17 hours ago











              • yes.. will add explanation

                – stack0114106
                17 hours ago











              • Added explanation to the code.. pls check

                – stack0114106
                17 hours ago

















              • Ahh... perl, always a good choice, and so powerful as well as elegant PRE is :)

                – Tiw
                17 hours ago






              • 1





                @Tiw.. thank you for the appreciation..yep, Perl is the right tool for regex problems like this..

                – stack0114106
                17 hours ago











              • @stack0114106 can you please explain it a bit to me. this is very nice

                – Sach
                17 hours ago











              • yes.. will add explanation

                – stack0114106
                17 hours ago











              • Added explanation to the code.. pls check

                – stack0114106
                17 hours ago
















              Ahh... perl, always a good choice, and so powerful as well as elegant PRE is :)

              – Tiw
              17 hours ago





              Ahh... perl, always a good choice, and so powerful as well as elegant PRE is :)

              – Tiw
              17 hours ago




              1




              1





              @Tiw.. thank you for the appreciation..yep, Perl is the right tool for regex problems like this..

              – stack0114106
              17 hours ago





              @Tiw.. thank you for the appreciation..yep, Perl is the right tool for regex problems like this..

              – stack0114106
              17 hours ago













              @stack0114106 can you please explain it a bit to me. this is very nice

              – Sach
              17 hours ago





              @stack0114106 can you please explain it a bit to me. this is very nice

              – Sach
              17 hours ago













              yes.. will add explanation

              – stack0114106
              17 hours ago





              yes.. will add explanation

              – stack0114106
              17 hours ago













              Added explanation to the code.. pls check

              – stack0114106
              17 hours ago





              Added explanation to the code.. pls check

              – stack0114106
              17 hours ago











              0














              With grep you can maybe try something like that :



              while read line; do COL=$(echo $line | grep -o "COL1:.*,"); NAME=$(echo $line | grep -o "NAME=[a-zA-Z]*"); echo $COL $NAME >> new_file.txt; done < your_file.txt 


              The regexp in this example assume that the value after COL1 are always followed by a "," (then it take every characters between the : and ,) so you might have to adapt it to fit your file (same for the regexp used for NAME).






              share|improve this answer








              New contributor




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
























                0














                With grep you can maybe try something like that :



                while read line; do COL=$(echo $line | grep -o "COL1:.*,"); NAME=$(echo $line | grep -o "NAME=[a-zA-Z]*"); echo $COL $NAME >> new_file.txt; done < your_file.txt 


                The regexp in this example assume that the value after COL1 are always followed by a "," (then it take every characters between the : and ,) so you might have to adapt it to fit your file (same for the regexp used for NAME).






                share|improve this answer








                New contributor




                Loïs Rancilhac 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







                  With grep you can maybe try something like that :



                  while read line; do COL=$(echo $line | grep -o "COL1:.*,"); NAME=$(echo $line | grep -o "NAME=[a-zA-Z]*"); echo $COL $NAME >> new_file.txt; done < your_file.txt 


                  The regexp in this example assume that the value after COL1 are always followed by a "," (then it take every characters between the : and ,) so you might have to adapt it to fit your file (same for the regexp used for NAME).






                  share|improve this answer








                  New contributor




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










                  With grep you can maybe try something like that :



                  while read line; do COL=$(echo $line | grep -o "COL1:.*,"); NAME=$(echo $line | grep -o "NAME=[a-zA-Z]*"); echo $COL $NAME >> new_file.txt; done < your_file.txt 


                  The regexp in this example assume that the value after COL1 are always followed by a "," (then it take every characters between the : and ,) so you might have to adapt it to fit your file (same for the regexp used for NAME).







                  share|improve this answer








                  New contributor




                  Loïs Rancilhac 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




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









                  answered 18 hours ago









                  Loïs RancilhacLoïs Rancilhac

                  11




                  11




                  New contributor




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





                  New contributor





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






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





















                      0














                      Try this:



                      $ sed 'H;s/.*NAME=/NAME=/;s/ *,.*//;x;s/^.*COL1/COL1/;s/ *,.*//;G;s/n/t, /;' file
                      COL1: VALUE1 , NAME=AUDIT
                      COL1: VALUE2 , NAME=generic
                      COL1: 289 , NAME=Oil


                      Used hold space, and used t for alignment.






                      share|improve this answer





























                        0














                        Try this:



                        $ sed 'H;s/.*NAME=/NAME=/;s/ *,.*//;x;s/^.*COL1/COL1/;s/ *,.*//;G;s/n/t, /;' file
                        COL1: VALUE1 , NAME=AUDIT
                        COL1: VALUE2 , NAME=generic
                        COL1: 289 , NAME=Oil


                        Used hold space, and used t for alignment.






                        share|improve this answer



























                          0












                          0








                          0







                          Try this:



                          $ sed 'H;s/.*NAME=/NAME=/;s/ *,.*//;x;s/^.*COL1/COL1/;s/ *,.*//;G;s/n/t, /;' file
                          COL1: VALUE1 , NAME=AUDIT
                          COL1: VALUE2 , NAME=generic
                          COL1: 289 , NAME=Oil


                          Used hold space, and used t for alignment.






                          share|improve this answer















                          Try this:



                          $ sed 'H;s/.*NAME=/NAME=/;s/ *,.*//;x;s/^.*COL1/COL1/;s/ *,.*//;G;s/n/t, /;' file
                          COL1: VALUE1 , NAME=AUDIT
                          COL1: VALUE2 , NAME=generic
                          COL1: 289 , NAME=Oil


                          Used hold space, and used t for alignment.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 16 hours ago

























                          answered 17 hours ago









                          TiwTiw

                          3,57341229




                          3,57341229



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • 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%2fstackoverflow.com%2fquestions%2f55021528%2fwhat-grep-awk-sed-command-to-use-to-the-output-that-i-want%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

                              Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                              Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                              2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived