What the equivalent of “grep | cut” using sed or awk?












5














Say I had a config file /etc/emails.conf



email1 = user@dinkum.dorg 
email2 = user@winkum.worg
email3 = user@stinkum.storg


and I wanted to get email2



I could do a:



grep email2 /etc/emails.conf | cut -d'=' -f2 


to get the email2, but how do I do it "cooler" with one sed or awk command and remove the whitespace that the cut command would leave?










share|improve this question
























  • You can remove the first cat: grep email2 /etc/emails.conf | ...
    – Carlos Campderrós
    May 4 '15 at 15:59










  • @CarlosCampderrós dang, I had thought I tried that but it failed for some completely unknown hidden reason (because obviously it should have worked). I just put the file name at the end of the cut wrongly and that's why it didn't work. I revised the question, I don't think it invalidates the answers.
    – Peter Turner
    May 4 '15 at 16:20










  • I really don't understand your question: you already have the string that you're searching for: result="email2" -- what are you really trying to do?
    – glenn jackman
    May 5 '15 at 1:25
















5














Say I had a config file /etc/emails.conf



email1 = user@dinkum.dorg 
email2 = user@winkum.worg
email3 = user@stinkum.storg


and I wanted to get email2



I could do a:



grep email2 /etc/emails.conf | cut -d'=' -f2 


to get the email2, but how do I do it "cooler" with one sed or awk command and remove the whitespace that the cut command would leave?










share|improve this question
























  • You can remove the first cat: grep email2 /etc/emails.conf | ...
    – Carlos Campderrós
    May 4 '15 at 15:59










  • @CarlosCampderrós dang, I had thought I tried that but it failed for some completely unknown hidden reason (because obviously it should have worked). I just put the file name at the end of the cut wrongly and that's why it didn't work. I revised the question, I don't think it invalidates the answers.
    – Peter Turner
    May 4 '15 at 16:20










  • I really don't understand your question: you already have the string that you're searching for: result="email2" -- what are you really trying to do?
    – glenn jackman
    May 5 '15 at 1:25














5












5








5


1





Say I had a config file /etc/emails.conf



email1 = user@dinkum.dorg 
email2 = user@winkum.worg
email3 = user@stinkum.storg


and I wanted to get email2



I could do a:



grep email2 /etc/emails.conf | cut -d'=' -f2 


to get the email2, but how do I do it "cooler" with one sed or awk command and remove the whitespace that the cut command would leave?










share|improve this question















Say I had a config file /etc/emails.conf



email1 = user@dinkum.dorg 
email2 = user@winkum.worg
email3 = user@stinkum.storg


and I wanted to get email2



I could do a:



grep email2 /etc/emails.conf | cut -d'=' -f2 


to get the email2, but how do I do it "cooler" with one sed or awk command and remove the whitespace that the cut command would leave?







sed awk grep cat cut






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 4 '15 at 16:36

























asked May 4 '15 at 15:53









Peter Turner

5631825




5631825












  • You can remove the first cat: grep email2 /etc/emails.conf | ...
    – Carlos Campderrós
    May 4 '15 at 15:59










  • @CarlosCampderrós dang, I had thought I tried that but it failed for some completely unknown hidden reason (because obviously it should have worked). I just put the file name at the end of the cut wrongly and that's why it didn't work. I revised the question, I don't think it invalidates the answers.
    – Peter Turner
    May 4 '15 at 16:20










  • I really don't understand your question: you already have the string that you're searching for: result="email2" -- what are you really trying to do?
    – glenn jackman
    May 5 '15 at 1:25


















  • You can remove the first cat: grep email2 /etc/emails.conf | ...
    – Carlos Campderrós
    May 4 '15 at 15:59










  • @CarlosCampderrós dang, I had thought I tried that but it failed for some completely unknown hidden reason (because obviously it should have worked). I just put the file name at the end of the cut wrongly and that's why it didn't work. I revised the question, I don't think it invalidates the answers.
    – Peter Turner
    May 4 '15 at 16:20










  • I really don't understand your question: you already have the string that you're searching for: result="email2" -- what are you really trying to do?
    – glenn jackman
    May 5 '15 at 1:25
















You can remove the first cat: grep email2 /etc/emails.conf | ...
– Carlos Campderrós
May 4 '15 at 15:59




You can remove the first cat: grep email2 /etc/emails.conf | ...
– Carlos Campderrós
May 4 '15 at 15:59












@CarlosCampderrós dang, I had thought I tried that but it failed for some completely unknown hidden reason (because obviously it should have worked). I just put the file name at the end of the cut wrongly and that's why it didn't work. I revised the question, I don't think it invalidates the answers.
– Peter Turner
May 4 '15 at 16:20




@CarlosCampderrós dang, I had thought I tried that but it failed for some completely unknown hidden reason (because obviously it should have worked). I just put the file name at the end of the cut wrongly and that's why it didn't work. I revised the question, I don't think it invalidates the answers.
– Peter Turner
May 4 '15 at 16:20












I really don't understand your question: you already have the string that you're searching for: result="email2" -- what are you really trying to do?
– glenn jackman
May 5 '15 at 1:25




I really don't understand your question: you already have the string that you're searching for: result="email2" -- what are you really trying to do?
– glenn jackman
May 5 '15 at 1:25










5 Answers
5






active

oldest

votes


















4














How about using awk?



awk -F = '/email2/ { print $2}' /etc/emails.conf



  • -F = Fields are separated by '='


  • '/email2/ { print $2}' On lines that match "email2", print the second field







share|improve this answer























  • can it trim the leading space too?
    – Peter Turner
    May 4 '15 at 16:23






  • 1




    @PeterTurner, my version of awk permits -F ' *= *', which is a wildcarded expression that groups spaces either side of = as part of the separator.
    – roaima
    May 4 '15 at 19:32





















4














The exact equivalent would be something like:



sed -n '/email2/{s/^[^=]*=([^=]*).*/1/;p;}' < file


But you'd probably want instead:



sed -n 's/^[^=]*email2[^=]*=[[:blank:]]*//p' < file


(that is match email2 only on the part before the first = and return everything on the right of the first = (skipping leading blanks), not only the part up to the second = if any).






share|improve this answer























  • Is space required between { s?
    – cuonglm
    May 4 '15 at 16:02










  • @cuonglm, no. AFAICT, ;} above is not POSIX but I don't know of any modern sed implementation where that fails.
    – Stéphane Chazelas
    May 4 '15 at 16:08










  • Yes, that's exactly I have stucked when working with this. After re-reading POSIX sed documentation, I see ;} is an accepted extension, but it's not required. It's strange that GNU sed with --posix option still allow {command} form. Do you think it's a bug?
    – cuonglm
    May 4 '15 at 16:12








  • 1




    @cuonglm {command} is not required to report an error by POSIX. It's just that a POSIX script must not use it as the behaviour is not specified there. Where GNU sed is not conformant even with --posix is that it doesn't accept ; in label names.
    – Stéphane Chazelas
    May 4 '15 at 16:23








  • 1




    @PeterTurner, see When should I use input redirection?.
    – Stéphane Chazelas
    May 5 '15 at 8:45



















3














perl -nlE 's/email2s*=s*// and say'    file


Where:





  • perl -nl is a for each line do...


  • s/email2 = // removes the searched email id and if you could do it ...


  • say prints the current input input line


  • s* zero or more spaces (equivalent to [ tn]*)






share|improve this answer























  • I'll give you a +1 for golfing, but where I'm running this (some sort of stripped down Cisco appliance), I'm not sure I'm going to have perl, especially the version of perl that has say.
    – Peter Turner
    May 4 '15 at 17:51










  • perl 5.10 appeared 7 year ago but sometimes we don't have it... perl -nle 's/email2s*=s*// and print'
    – JJoao
    May 4 '15 at 17:59



















0














There is no need to set the field separator to any special value such as "=".
By default, AWK uses contiguous spaces and tabs as field separators, which are skipped, so it will interpret each line of your file as having three fields, without any leading space on field three



awk '/^email2/ { print $3 }'


produces



user@winkum.worg


The above would also match any lines starting with email2, such as email20, for exact match you can use



awk '$1 == "email2" { print $3 }'





share|improve this answer





























    0














    To get something like grep | cut you can use sed -n s/A/B/p.



    By default, sed prints every line after all commands are processed. You can silence all output that you don't explicitly print from a command with sed -n.



    The s command takes the form s/$FIND/$REPLACE/$FLAGS. Specifically, the p flag prints the line whenever a replacement is made.



    With these combined, you can easily match and cut:



    sed -nE "s/email2 = (.+)/1/p" < /etc/emails.conf


    In fact, this is strictly more powerful than grep | cut because you can use an arbitrary replacement pattern.



    (The -E option enables modern regex, which allows you to reference capture groups in the replacement pattern. For a simple cut, you can get away without it by using more clever patterns.)






    share|improve this answer





















      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "106"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f200337%2fwhat-the-equivalent-of-grep-cut-using-sed-or-awk%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      5 Answers
      5






      active

      oldest

      votes








      5 Answers
      5






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      How about using awk?



      awk -F = '/email2/ { print $2}' /etc/emails.conf



      • -F = Fields are separated by '='


      • '/email2/ { print $2}' On lines that match "email2", print the second field







      share|improve this answer























      • can it trim the leading space too?
        – Peter Turner
        May 4 '15 at 16:23






      • 1




        @PeterTurner, my version of awk permits -F ' *= *', which is a wildcarded expression that groups spaces either side of = as part of the separator.
        – roaima
        May 4 '15 at 19:32


















      4














      How about using awk?



      awk -F = '/email2/ { print $2}' /etc/emails.conf



      • -F = Fields are separated by '='


      • '/email2/ { print $2}' On lines that match "email2", print the second field







      share|improve this answer























      • can it trim the leading space too?
        – Peter Turner
        May 4 '15 at 16:23






      • 1




        @PeterTurner, my version of awk permits -F ' *= *', which is a wildcarded expression that groups spaces either side of = as part of the separator.
        – roaima
        May 4 '15 at 19:32
















      4












      4








      4






      How about using awk?



      awk -F = '/email2/ { print $2}' /etc/emails.conf



      • -F = Fields are separated by '='


      • '/email2/ { print $2}' On lines that match "email2", print the second field







      share|improve this answer














      How about using awk?



      awk -F = '/email2/ { print $2}' /etc/emails.conf



      • -F = Fields are separated by '='


      • '/email2/ { print $2}' On lines that match "email2", print the second field








      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 25 '16 at 21:07









      wjandrea

      466413




      466413










      answered May 4 '15 at 15:58









      Carlos Campderrós

      8611716




      8611716












      • can it trim the leading space too?
        – Peter Turner
        May 4 '15 at 16:23






      • 1




        @PeterTurner, my version of awk permits -F ' *= *', which is a wildcarded expression that groups spaces either side of = as part of the separator.
        – roaima
        May 4 '15 at 19:32




















      • can it trim the leading space too?
        – Peter Turner
        May 4 '15 at 16:23






      • 1




        @PeterTurner, my version of awk permits -F ' *= *', which is a wildcarded expression that groups spaces either side of = as part of the separator.
        – roaima
        May 4 '15 at 19:32


















      can it trim the leading space too?
      – Peter Turner
      May 4 '15 at 16:23




      can it trim the leading space too?
      – Peter Turner
      May 4 '15 at 16:23




      1




      1




      @PeterTurner, my version of awk permits -F ' *= *', which is a wildcarded expression that groups spaces either side of = as part of the separator.
      – roaima
      May 4 '15 at 19:32






      @PeterTurner, my version of awk permits -F ' *= *', which is a wildcarded expression that groups spaces either side of = as part of the separator.
      – roaima
      May 4 '15 at 19:32















      4














      The exact equivalent would be something like:



      sed -n '/email2/{s/^[^=]*=([^=]*).*/1/;p;}' < file


      But you'd probably want instead:



      sed -n 's/^[^=]*email2[^=]*=[[:blank:]]*//p' < file


      (that is match email2 only on the part before the first = and return everything on the right of the first = (skipping leading blanks), not only the part up to the second = if any).






      share|improve this answer























      • Is space required between { s?
        – cuonglm
        May 4 '15 at 16:02










      • @cuonglm, no. AFAICT, ;} above is not POSIX but I don't know of any modern sed implementation where that fails.
        – Stéphane Chazelas
        May 4 '15 at 16:08










      • Yes, that's exactly I have stucked when working with this. After re-reading POSIX sed documentation, I see ;} is an accepted extension, but it's not required. It's strange that GNU sed with --posix option still allow {command} form. Do you think it's a bug?
        – cuonglm
        May 4 '15 at 16:12








      • 1




        @cuonglm {command} is not required to report an error by POSIX. It's just that a POSIX script must not use it as the behaviour is not specified there. Where GNU sed is not conformant even with --posix is that it doesn't accept ; in label names.
        – Stéphane Chazelas
        May 4 '15 at 16:23








      • 1




        @PeterTurner, see When should I use input redirection?.
        – Stéphane Chazelas
        May 5 '15 at 8:45
















      4














      The exact equivalent would be something like:



      sed -n '/email2/{s/^[^=]*=([^=]*).*/1/;p;}' < file


      But you'd probably want instead:



      sed -n 's/^[^=]*email2[^=]*=[[:blank:]]*//p' < file


      (that is match email2 only on the part before the first = and return everything on the right of the first = (skipping leading blanks), not only the part up to the second = if any).






      share|improve this answer























      • Is space required between { s?
        – cuonglm
        May 4 '15 at 16:02










      • @cuonglm, no. AFAICT, ;} above is not POSIX but I don't know of any modern sed implementation where that fails.
        – Stéphane Chazelas
        May 4 '15 at 16:08










      • Yes, that's exactly I have stucked when working with this. After re-reading POSIX sed documentation, I see ;} is an accepted extension, but it's not required. It's strange that GNU sed with --posix option still allow {command} form. Do you think it's a bug?
        – cuonglm
        May 4 '15 at 16:12








      • 1




        @cuonglm {command} is not required to report an error by POSIX. It's just that a POSIX script must not use it as the behaviour is not specified there. Where GNU sed is not conformant even with --posix is that it doesn't accept ; in label names.
        – Stéphane Chazelas
        May 4 '15 at 16:23








      • 1




        @PeterTurner, see When should I use input redirection?.
        – Stéphane Chazelas
        May 5 '15 at 8:45














      4












      4








      4






      The exact equivalent would be something like:



      sed -n '/email2/{s/^[^=]*=([^=]*).*/1/;p;}' < file


      But you'd probably want instead:



      sed -n 's/^[^=]*email2[^=]*=[[:blank:]]*//p' < file


      (that is match email2 only on the part before the first = and return everything on the right of the first = (skipping leading blanks), not only the part up to the second = if any).






      share|improve this answer














      The exact equivalent would be something like:



      sed -n '/email2/{s/^[^=]*=([^=]*).*/1/;p;}' < file


      But you'd probably want instead:



      sed -n 's/^[^=]*email2[^=]*=[[:blank:]]*//p' < file


      (that is match email2 only on the part before the first = and return everything on the right of the first = (skipping leading blanks), not only the part up to the second = if any).







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited May 4 '15 at 16:09

























      answered May 4 '15 at 16:00









      Stéphane Chazelas

      299k54564913




      299k54564913












      • Is space required between { s?
        – cuonglm
        May 4 '15 at 16:02










      • @cuonglm, no. AFAICT, ;} above is not POSIX but I don't know of any modern sed implementation where that fails.
        – Stéphane Chazelas
        May 4 '15 at 16:08










      • Yes, that's exactly I have stucked when working with this. After re-reading POSIX sed documentation, I see ;} is an accepted extension, but it's not required. It's strange that GNU sed with --posix option still allow {command} form. Do you think it's a bug?
        – cuonglm
        May 4 '15 at 16:12








      • 1




        @cuonglm {command} is not required to report an error by POSIX. It's just that a POSIX script must not use it as the behaviour is not specified there. Where GNU sed is not conformant even with --posix is that it doesn't accept ; in label names.
        – Stéphane Chazelas
        May 4 '15 at 16:23








      • 1




        @PeterTurner, see When should I use input redirection?.
        – Stéphane Chazelas
        May 5 '15 at 8:45


















      • Is space required between { s?
        – cuonglm
        May 4 '15 at 16:02










      • @cuonglm, no. AFAICT, ;} above is not POSIX but I don't know of any modern sed implementation where that fails.
        – Stéphane Chazelas
        May 4 '15 at 16:08










      • Yes, that's exactly I have stucked when working with this. After re-reading POSIX sed documentation, I see ;} is an accepted extension, but it's not required. It's strange that GNU sed with --posix option still allow {command} form. Do you think it's a bug?
        – cuonglm
        May 4 '15 at 16:12








      • 1




        @cuonglm {command} is not required to report an error by POSIX. It's just that a POSIX script must not use it as the behaviour is not specified there. Where GNU sed is not conformant even with --posix is that it doesn't accept ; in label names.
        – Stéphane Chazelas
        May 4 '15 at 16:23








      • 1




        @PeterTurner, see When should I use input redirection?.
        – Stéphane Chazelas
        May 5 '15 at 8:45
















      Is space required between { s?
      – cuonglm
      May 4 '15 at 16:02




      Is space required between { s?
      – cuonglm
      May 4 '15 at 16:02












      @cuonglm, no. AFAICT, ;} above is not POSIX but I don't know of any modern sed implementation where that fails.
      – Stéphane Chazelas
      May 4 '15 at 16:08




      @cuonglm, no. AFAICT, ;} above is not POSIX but I don't know of any modern sed implementation where that fails.
      – Stéphane Chazelas
      May 4 '15 at 16:08












      Yes, that's exactly I have stucked when working with this. After re-reading POSIX sed documentation, I see ;} is an accepted extension, but it's not required. It's strange that GNU sed with --posix option still allow {command} form. Do you think it's a bug?
      – cuonglm
      May 4 '15 at 16:12






      Yes, that's exactly I have stucked when working with this. After re-reading POSIX sed documentation, I see ;} is an accepted extension, but it's not required. It's strange that GNU sed with --posix option still allow {command} form. Do you think it's a bug?
      – cuonglm
      May 4 '15 at 16:12






      1




      1




      @cuonglm {command} is not required to report an error by POSIX. It's just that a POSIX script must not use it as the behaviour is not specified there. Where GNU sed is not conformant even with --posix is that it doesn't accept ; in label names.
      – Stéphane Chazelas
      May 4 '15 at 16:23






      @cuonglm {command} is not required to report an error by POSIX. It's just that a POSIX script must not use it as the behaviour is not specified there. Where GNU sed is not conformant even with --posix is that it doesn't accept ; in label names.
      – Stéphane Chazelas
      May 4 '15 at 16:23






      1




      1




      @PeterTurner, see When should I use input redirection?.
      – Stéphane Chazelas
      May 5 '15 at 8:45




      @PeterTurner, see When should I use input redirection?.
      – Stéphane Chazelas
      May 5 '15 at 8:45











      3














      perl -nlE 's/email2s*=s*// and say'    file


      Where:





      • perl -nl is a for each line do...


      • s/email2 = // removes the searched email id and if you could do it ...


      • say prints the current input input line


      • s* zero or more spaces (equivalent to [ tn]*)






      share|improve this answer























      • I'll give you a +1 for golfing, but where I'm running this (some sort of stripped down Cisco appliance), I'm not sure I'm going to have perl, especially the version of perl that has say.
        – Peter Turner
        May 4 '15 at 17:51










      • perl 5.10 appeared 7 year ago but sometimes we don't have it... perl -nle 's/email2s*=s*// and print'
        – JJoao
        May 4 '15 at 17:59
















      3














      perl -nlE 's/email2s*=s*// and say'    file


      Where:





      • perl -nl is a for each line do...


      • s/email2 = // removes the searched email id and if you could do it ...


      • say prints the current input input line


      • s* zero or more spaces (equivalent to [ tn]*)






      share|improve this answer























      • I'll give you a +1 for golfing, but where I'm running this (some sort of stripped down Cisco appliance), I'm not sure I'm going to have perl, especially the version of perl that has say.
        – Peter Turner
        May 4 '15 at 17:51










      • perl 5.10 appeared 7 year ago but sometimes we don't have it... perl -nle 's/email2s*=s*// and print'
        – JJoao
        May 4 '15 at 17:59














      3












      3








      3






      perl -nlE 's/email2s*=s*// and say'    file


      Where:





      • perl -nl is a for each line do...


      • s/email2 = // removes the searched email id and if you could do it ...


      • say prints the current input input line


      • s* zero or more spaces (equivalent to [ tn]*)






      share|improve this answer














      perl -nlE 's/email2s*=s*// and say'    file


      Where:





      • perl -nl is a for each line do...


      • s/email2 = // removes the searched email id and if you could do it ...


      • say prints the current input input line


      • s* zero or more spaces (equivalent to [ tn]*)







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited May 4 '15 at 17:41

























      answered May 4 '15 at 16:48









      JJoao

      7,1041928




      7,1041928












      • I'll give you a +1 for golfing, but where I'm running this (some sort of stripped down Cisco appliance), I'm not sure I'm going to have perl, especially the version of perl that has say.
        – Peter Turner
        May 4 '15 at 17:51










      • perl 5.10 appeared 7 year ago but sometimes we don't have it... perl -nle 's/email2s*=s*// and print'
        – JJoao
        May 4 '15 at 17:59


















      • I'll give you a +1 for golfing, but where I'm running this (some sort of stripped down Cisco appliance), I'm not sure I'm going to have perl, especially the version of perl that has say.
        – Peter Turner
        May 4 '15 at 17:51










      • perl 5.10 appeared 7 year ago but sometimes we don't have it... perl -nle 's/email2s*=s*// and print'
        – JJoao
        May 4 '15 at 17:59
















      I'll give you a +1 for golfing, but where I'm running this (some sort of stripped down Cisco appliance), I'm not sure I'm going to have perl, especially the version of perl that has say.
      – Peter Turner
      May 4 '15 at 17:51




      I'll give you a +1 for golfing, but where I'm running this (some sort of stripped down Cisco appliance), I'm not sure I'm going to have perl, especially the version of perl that has say.
      – Peter Turner
      May 4 '15 at 17:51












      perl 5.10 appeared 7 year ago but sometimes we don't have it... perl -nle 's/email2s*=s*// and print'
      – JJoao
      May 4 '15 at 17:59




      perl 5.10 appeared 7 year ago but sometimes we don't have it... perl -nle 's/email2s*=s*// and print'
      – JJoao
      May 4 '15 at 17:59











      0














      There is no need to set the field separator to any special value such as "=".
      By default, AWK uses contiguous spaces and tabs as field separators, which are skipped, so it will interpret each line of your file as having three fields, without any leading space on field three



      awk '/^email2/ { print $3 }'


      produces



      user@winkum.worg


      The above would also match any lines starting with email2, such as email20, for exact match you can use



      awk '$1 == "email2" { print $3 }'





      share|improve this answer


























        0














        There is no need to set the field separator to any special value such as "=".
        By default, AWK uses contiguous spaces and tabs as field separators, which are skipped, so it will interpret each line of your file as having three fields, without any leading space on field three



        awk '/^email2/ { print $3 }'


        produces



        user@winkum.worg


        The above would also match any lines starting with email2, such as email20, for exact match you can use



        awk '$1 == "email2" { print $3 }'





        share|improve this answer
























          0












          0








          0






          There is no need to set the field separator to any special value such as "=".
          By default, AWK uses contiguous spaces and tabs as field separators, which are skipped, so it will interpret each line of your file as having three fields, without any leading space on field three



          awk '/^email2/ { print $3 }'


          produces



          user@winkum.worg


          The above would also match any lines starting with email2, such as email20, for exact match you can use



          awk '$1 == "email2" { print $3 }'





          share|improve this answer












          There is no need to set the field separator to any special value such as "=".
          By default, AWK uses contiguous spaces and tabs as field separators, which are skipped, so it will interpret each line of your file as having three fields, without any leading space on field three



          awk '/^email2/ { print $3 }'


          produces



          user@winkum.worg


          The above would also match any lines starting with email2, such as email20, for exact match you can use



          awk '$1 == "email2" { print $3 }'






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 5 '15 at 16:20









          user531214

          964




          964























              0














              To get something like grep | cut you can use sed -n s/A/B/p.



              By default, sed prints every line after all commands are processed. You can silence all output that you don't explicitly print from a command with sed -n.



              The s command takes the form s/$FIND/$REPLACE/$FLAGS. Specifically, the p flag prints the line whenever a replacement is made.



              With these combined, you can easily match and cut:



              sed -nE "s/email2 = (.+)/1/p" < /etc/emails.conf


              In fact, this is strictly more powerful than grep | cut because you can use an arbitrary replacement pattern.



              (The -E option enables modern regex, which allows you to reference capture groups in the replacement pattern. For a simple cut, you can get away without it by using more clever patterns.)






              share|improve this answer


























                0














                To get something like grep | cut you can use sed -n s/A/B/p.



                By default, sed prints every line after all commands are processed. You can silence all output that you don't explicitly print from a command with sed -n.



                The s command takes the form s/$FIND/$REPLACE/$FLAGS. Specifically, the p flag prints the line whenever a replacement is made.



                With these combined, you can easily match and cut:



                sed -nE "s/email2 = (.+)/1/p" < /etc/emails.conf


                In fact, this is strictly more powerful than grep | cut because you can use an arbitrary replacement pattern.



                (The -E option enables modern regex, which allows you to reference capture groups in the replacement pattern. For a simple cut, you can get away without it by using more clever patterns.)






                share|improve this answer
























                  0












                  0








                  0






                  To get something like grep | cut you can use sed -n s/A/B/p.



                  By default, sed prints every line after all commands are processed. You can silence all output that you don't explicitly print from a command with sed -n.



                  The s command takes the form s/$FIND/$REPLACE/$FLAGS. Specifically, the p flag prints the line whenever a replacement is made.



                  With these combined, you can easily match and cut:



                  sed -nE "s/email2 = (.+)/1/p" < /etc/emails.conf


                  In fact, this is strictly more powerful than grep | cut because you can use an arbitrary replacement pattern.



                  (The -E option enables modern regex, which allows you to reference capture groups in the replacement pattern. For a simple cut, you can get away without it by using more clever patterns.)






                  share|improve this answer












                  To get something like grep | cut you can use sed -n s/A/B/p.



                  By default, sed prints every line after all commands are processed. You can silence all output that you don't explicitly print from a command with sed -n.



                  The s command takes the form s/$FIND/$REPLACE/$FLAGS. Specifically, the p flag prints the line whenever a replacement is made.



                  With these combined, you can easily match and cut:



                  sed -nE "s/email2 = (.+)/1/p" < /etc/emails.conf


                  In fact, this is strictly more powerful than grep | cut because you can use an arbitrary replacement pattern.



                  (The -E option enables modern regex, which allows you to reference capture groups in the replacement pattern. For a simple cut, you can get away without it by using more clever patterns.)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 19 '18 at 7:56









                  cbarrick

                  1011




                  1011






























                      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%2f200337%2fwhat-the-equivalent-of-grep-cut-using-sed-or-awk%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

                      Morgemoulin

                      Scott Moir

                      Souastre