Regex to format files output

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











up vote
1
down vote

favorite
1












I have file which has the following contents:



 foo-6-25.example.com:
1 Var. Speed System Board Normal Yes Normal ( 49)
--
foo-5-4.example.com:
1 Var. Speed System Board Normal Yes Normal ( 19)
--
foo-8-28.example.com:
1 Var. Speed System Board Normal Yes Normal ( 43)
--
foo-9-7.example.com:
1 Var. Speed System Board Normal Yes Normal ( 91)
--
foo-5-19.idmz.example.com:
1 Var. Speed System Board Normal Yes Normal ( 19)
--
foo-7-3.example.com:
1 Var. Speed System Board Normal Yes Normal ( 20)


I want to format it in the following way: servername and then its FAN speed which is inside () bracket



foo-6-25.example.com: ( 49)
foo-5-4.example.com: ( 19)


Not sure how to use that using awk or any other tools.







share|improve this question


























    up vote
    1
    down vote

    favorite
    1












    I have file which has the following contents:



     foo-6-25.example.com:
    1 Var. Speed System Board Normal Yes Normal ( 49)
    --
    foo-5-4.example.com:
    1 Var. Speed System Board Normal Yes Normal ( 19)
    --
    foo-8-28.example.com:
    1 Var. Speed System Board Normal Yes Normal ( 43)
    --
    foo-9-7.example.com:
    1 Var. Speed System Board Normal Yes Normal ( 91)
    --
    foo-5-19.idmz.example.com:
    1 Var. Speed System Board Normal Yes Normal ( 19)
    --
    foo-7-3.example.com:
    1 Var. Speed System Board Normal Yes Normal ( 20)


    I want to format it in the following way: servername and then its FAN speed which is inside () bracket



    foo-6-25.example.com: ( 49)
    foo-5-4.example.com: ( 19)


    Not sure how to use that using awk or any other tools.







    share|improve this question
























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      I have file which has the following contents:



       foo-6-25.example.com:
      1 Var. Speed System Board Normal Yes Normal ( 49)
      --
      foo-5-4.example.com:
      1 Var. Speed System Board Normal Yes Normal ( 19)
      --
      foo-8-28.example.com:
      1 Var. Speed System Board Normal Yes Normal ( 43)
      --
      foo-9-7.example.com:
      1 Var. Speed System Board Normal Yes Normal ( 91)
      --
      foo-5-19.idmz.example.com:
      1 Var. Speed System Board Normal Yes Normal ( 19)
      --
      foo-7-3.example.com:
      1 Var. Speed System Board Normal Yes Normal ( 20)


      I want to format it in the following way: servername and then its FAN speed which is inside () bracket



      foo-6-25.example.com: ( 49)
      foo-5-4.example.com: ( 19)


      Not sure how to use that using awk or any other tools.







      share|improve this question














      I have file which has the following contents:



       foo-6-25.example.com:
      1 Var. Speed System Board Normal Yes Normal ( 49)
      --
      foo-5-4.example.com:
      1 Var. Speed System Board Normal Yes Normal ( 19)
      --
      foo-8-28.example.com:
      1 Var. Speed System Board Normal Yes Normal ( 43)
      --
      foo-9-7.example.com:
      1 Var. Speed System Board Normal Yes Normal ( 91)
      --
      foo-5-19.idmz.example.com:
      1 Var. Speed System Board Normal Yes Normal ( 19)
      --
      foo-7-3.example.com:
      1 Var. Speed System Board Normal Yes Normal ( 20)


      I want to format it in the following way: servername and then its FAN speed which is inside () bracket



      foo-6-25.example.com: ( 49)
      foo-5-4.example.com: ( 19)


      Not sure how to use that using awk or any other tools.









      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 8 at 23:15









      Jeff Schaller

      32k848109




      32k848109










      asked Aug 8 at 22:35









      Satish

      61011130




      61011130




















          6 Answers
          6






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          awk solution



          $ awk '/:/d=$1/Speed/printf"%-28s%sn",d,substr($0,length($0)-4)' file
          foo-6-25.example.com: ( 49)
          foo-5-4.example.com: ( 19)
          foo-8-28.example.com: ( 43)
          foo-9-7.example.com: ( 91)
          foo-5-19.idmz.example.com: ( 19)
          foo-7-3.example.com: ( 20)
          $


          awk + column solution



          Aligns the columns dynamically.



          $ awk '/:/d=$1/Speed/print d,substr($0,length($0)-4)' file|column -to' '
          foo-6-25.example.com: ( 49)
          foo-5-4.example.com: ( 19)
          foo-8-28.example.com: ( 43)
          foo-9-7.example.com: ( 91)
          foo-5-19.idmz.example.com: ( 19)
          foo-7-3.example.com: ( 20)
          $





          share|improve this answer


















          • 2




            you are just awesome!!!
            – Satish
            Aug 8 at 22:54






          • 1




            I like your aws + column solution but in output i am seeing foo-8-1.example.com: ( 49) how do i reduce this space in ` ( look like tab space 49)` ?
            – Satish
            Aug 8 at 22:55











          • column: illegal option -- o :(
            – Satish
            Aug 8 at 23:10






          • 1




            nevermid my bad, i was on MacBookPro after moving my file on Linux it seems looking good
            – Satish
            Aug 8 at 23:17






          • 1




            Damn!!! it you are the beast in awk take a bow!!!
            – Satish
            Aug 8 at 23:22

















          up vote
          1
          down vote













          $ sed -e '/--/d;N;s/n/ /;s/[[:blank:]]+//;s/[[:blank:]]+[^(]+/, /' file | column -ts ','
          foo-6-25.example.com: ( 49)
          foo-5-4.example.com: ( 19)
          foo-8-28.example.com: ( 43)
          foo-9-7.example.com: ( 91)
          foo-5-19.idmz.example.com: ( 19)
          foo-7-3.example.com: ( 20)


          If you don't want any spacing/alignment of the 2nd column you can omit the column command:



          $ sed -e '/--/d;N;s/n/ /;s/[[:blank:]]+//;s/[[:blank:]]+[^(]+/ /' file
          foo-6-25.example.com: ( 49)
          foo-5-4.example.com: ( 19)
          foo-8-28.example.com: ( 43)
          foo-9-7.example.com: ( 91)
          foo-5-19.idmz.example.com: ( 19)
          foo-7-3.example.com: ( 20)


          If you're dealing with GNU sed you can reduce it further:



          $ sed -e '/--/d;N;s/n/ /;s/[ ]+//;s/[ ]+[^(]+/ /' file





          share|improve this answer






















          • I have tired your solution but its printing everything in single like like foo-010101-5-4.example.com: 1 Var. Speed System Board Normal Yes Normal ( 19)
            – Satish
            Aug 8 at 23:03










          • I suspect your output isn't just spaces as you're showing in the example data, is it?
            – slm♦
            Aug 8 at 23:05










          • my file is correct and i am replacing file with my filename.txt
            – Satish
            Aug 8 at 23:12










          • I showed this style of example b/c unlike the awk you can more easily add/remove portions of this solution to see how it's working. Each /../; block in the sed is doing something specific, you can take them all out and then add them back in 1 at a time to see how they work and work towards you solution rather than have to have a lot of knowledge about awk.
            – slm♦
            Aug 8 at 23:13


















          up vote
          1
          down vote













          % perl -ne 'print(($_ .= <>) =~ s/n.*(/ (/r) if /:/' input.file


          outputs:



          foo-6-25.example.com: ( 49)
          foo-5-4.example.com: ( 19)
          foo-8-28.example.com: ( 43)
          foo-9-7.example.com: ( 91)
          foo-5-19.idmz.example.com: ( 19)
          foo-7-3.example.com: ( 20)


          explanation:



          1. on a line comprising :, it appends the next line to the current record.

          2. then deletes from the newline of the original line till the last ( and replaces this with ( and prints the record.

          3. Since perl was invoked with -n, records not affected are not printed.

          Sed has a very compact way to write the same:



          sed -ne '/:/N;s/n.*(/ (/p' input.file





          share|improve this answer



























            up vote
            0
            down vote













            < file perl -pe 's/n//g;s/(foo)/n$1/g' | perl -pe 's/:.*(/: (/g'





            share|improve this answer



























              up vote
              0
              down vote













              Using awk in paragraph mode



              awk -vRS='n[ t]*--' '
              match($0, /( *[0-9]+)/) print $1, substr($0,RSTART,RLENGTH)
              ' file
              foo-6-25.example.com: ( 49)
              foo-5-4.example.com: ( 19)
              foo-8-28.example.com: ( 43)
              foo-9-7.example.com: ( 91)
              foo-5-19.idmz.example.com: ( 19)
              foo-7-3.example.com: ( 20)





              share|improve this answer





























                up vote
                0
                down vote













                $ sed -nE '/:/N;s/^[[:space:]]+//; s/:[^(]+/: /p;' file
                foo-6-25.example.com: ( 49)
                foo-5-4.example.com: ( 19)
                foo-8-28.example.com: ( 43)
                foo-9-7.example.com: ( 91)
                foo-5-19.idmz.example.com: ( 19)
                foo-7-3.example.com: ( 20)


                If there are no initial spaces or tabs on each line, then just



                sed -nE '/:/N;s/:[^(]+/: /p;' file


                The (first) sed script annotated (assumes -E and -n):



                /:/ # this line contains a host name
                N; # append the next line from the input
                s/^[[:space:]]+//; # remove the initial spaces or tabs
                s/:[^(]+/: /p; # remove the bit between the ":" and "(" and print






                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',
                  convertImagesToLinks: false,
                  noModals: false,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: null,
                  bindNavPrevention: true,
                  postfix: "",
                  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%2f461389%2fregex-to-format-files-output%23new-answer', 'question_page');

                  );

                  Post as a guest






























                  6 Answers
                  6






                  active

                  oldest

                  votes








                  6 Answers
                  6






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  2
                  down vote



                  accepted










                  awk solution



                  $ awk '/:/d=$1/Speed/printf"%-28s%sn",d,substr($0,length($0)-4)' file
                  foo-6-25.example.com: ( 49)
                  foo-5-4.example.com: ( 19)
                  foo-8-28.example.com: ( 43)
                  foo-9-7.example.com: ( 91)
                  foo-5-19.idmz.example.com: ( 19)
                  foo-7-3.example.com: ( 20)
                  $


                  awk + column solution



                  Aligns the columns dynamically.



                  $ awk '/:/d=$1/Speed/print d,substr($0,length($0)-4)' file|column -to' '
                  foo-6-25.example.com: ( 49)
                  foo-5-4.example.com: ( 19)
                  foo-8-28.example.com: ( 43)
                  foo-9-7.example.com: ( 91)
                  foo-5-19.idmz.example.com: ( 19)
                  foo-7-3.example.com: ( 20)
                  $





                  share|improve this answer


















                  • 2




                    you are just awesome!!!
                    – Satish
                    Aug 8 at 22:54






                  • 1




                    I like your aws + column solution but in output i am seeing foo-8-1.example.com: ( 49) how do i reduce this space in ` ( look like tab space 49)` ?
                    – Satish
                    Aug 8 at 22:55











                  • column: illegal option -- o :(
                    – Satish
                    Aug 8 at 23:10






                  • 1




                    nevermid my bad, i was on MacBookPro after moving my file on Linux it seems looking good
                    – Satish
                    Aug 8 at 23:17






                  • 1




                    Damn!!! it you are the beast in awk take a bow!!!
                    – Satish
                    Aug 8 at 23:22














                  up vote
                  2
                  down vote



                  accepted










                  awk solution



                  $ awk '/:/d=$1/Speed/printf"%-28s%sn",d,substr($0,length($0)-4)' file
                  foo-6-25.example.com: ( 49)
                  foo-5-4.example.com: ( 19)
                  foo-8-28.example.com: ( 43)
                  foo-9-7.example.com: ( 91)
                  foo-5-19.idmz.example.com: ( 19)
                  foo-7-3.example.com: ( 20)
                  $


                  awk + column solution



                  Aligns the columns dynamically.



                  $ awk '/:/d=$1/Speed/print d,substr($0,length($0)-4)' file|column -to' '
                  foo-6-25.example.com: ( 49)
                  foo-5-4.example.com: ( 19)
                  foo-8-28.example.com: ( 43)
                  foo-9-7.example.com: ( 91)
                  foo-5-19.idmz.example.com: ( 19)
                  foo-7-3.example.com: ( 20)
                  $





                  share|improve this answer


















                  • 2




                    you are just awesome!!!
                    – Satish
                    Aug 8 at 22:54






                  • 1




                    I like your aws + column solution but in output i am seeing foo-8-1.example.com: ( 49) how do i reduce this space in ` ( look like tab space 49)` ?
                    – Satish
                    Aug 8 at 22:55











                  • column: illegal option -- o :(
                    – Satish
                    Aug 8 at 23:10






                  • 1




                    nevermid my bad, i was on MacBookPro after moving my file on Linux it seems looking good
                    – Satish
                    Aug 8 at 23:17






                  • 1




                    Damn!!! it you are the beast in awk take a bow!!!
                    – Satish
                    Aug 8 at 23:22












                  up vote
                  2
                  down vote



                  accepted







                  up vote
                  2
                  down vote



                  accepted






                  awk solution



                  $ awk '/:/d=$1/Speed/printf"%-28s%sn",d,substr($0,length($0)-4)' file
                  foo-6-25.example.com: ( 49)
                  foo-5-4.example.com: ( 19)
                  foo-8-28.example.com: ( 43)
                  foo-9-7.example.com: ( 91)
                  foo-5-19.idmz.example.com: ( 19)
                  foo-7-3.example.com: ( 20)
                  $


                  awk + column solution



                  Aligns the columns dynamically.



                  $ awk '/:/d=$1/Speed/print d,substr($0,length($0)-4)' file|column -to' '
                  foo-6-25.example.com: ( 49)
                  foo-5-4.example.com: ( 19)
                  foo-8-28.example.com: ( 43)
                  foo-9-7.example.com: ( 91)
                  foo-5-19.idmz.example.com: ( 19)
                  foo-7-3.example.com: ( 20)
                  $





                  share|improve this answer














                  awk solution



                  $ awk '/:/d=$1/Speed/printf"%-28s%sn",d,substr($0,length($0)-4)' file
                  foo-6-25.example.com: ( 49)
                  foo-5-4.example.com: ( 19)
                  foo-8-28.example.com: ( 43)
                  foo-9-7.example.com: ( 91)
                  foo-5-19.idmz.example.com: ( 19)
                  foo-7-3.example.com: ( 20)
                  $


                  awk + column solution



                  Aligns the columns dynamically.



                  $ awk '/:/d=$1/Speed/print d,substr($0,length($0)-4)' file|column -to' '
                  foo-6-25.example.com: ( 49)
                  foo-5-4.example.com: ( 19)
                  foo-8-28.example.com: ( 43)
                  foo-9-7.example.com: ( 91)
                  foo-5-19.idmz.example.com: ( 19)
                  foo-7-3.example.com: ( 20)
                  $






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Aug 8 at 23:06

























                  answered Aug 8 at 22:44









                  steve

                  12.6k22048




                  12.6k22048







                  • 2




                    you are just awesome!!!
                    – Satish
                    Aug 8 at 22:54






                  • 1




                    I like your aws + column solution but in output i am seeing foo-8-1.example.com: ( 49) how do i reduce this space in ` ( look like tab space 49)` ?
                    – Satish
                    Aug 8 at 22:55











                  • column: illegal option -- o :(
                    – Satish
                    Aug 8 at 23:10






                  • 1




                    nevermid my bad, i was on MacBookPro after moving my file on Linux it seems looking good
                    – Satish
                    Aug 8 at 23:17






                  • 1




                    Damn!!! it you are the beast in awk take a bow!!!
                    – Satish
                    Aug 8 at 23:22












                  • 2




                    you are just awesome!!!
                    – Satish
                    Aug 8 at 22:54






                  • 1




                    I like your aws + column solution but in output i am seeing foo-8-1.example.com: ( 49) how do i reduce this space in ` ( look like tab space 49)` ?
                    – Satish
                    Aug 8 at 22:55











                  • column: illegal option -- o :(
                    – Satish
                    Aug 8 at 23:10






                  • 1




                    nevermid my bad, i was on MacBookPro after moving my file on Linux it seems looking good
                    – Satish
                    Aug 8 at 23:17






                  • 1




                    Damn!!! it you are the beast in awk take a bow!!!
                    – Satish
                    Aug 8 at 23:22







                  2




                  2




                  you are just awesome!!!
                  – Satish
                  Aug 8 at 22:54




                  you are just awesome!!!
                  – Satish
                  Aug 8 at 22:54




                  1




                  1




                  I like your aws + column solution but in output i am seeing foo-8-1.example.com: ( 49) how do i reduce this space in ` ( look like tab space 49)` ?
                  – Satish
                  Aug 8 at 22:55





                  I like your aws + column solution but in output i am seeing foo-8-1.example.com: ( 49) how do i reduce this space in ` ( look like tab space 49)` ?
                  – Satish
                  Aug 8 at 22:55













                  column: illegal option -- o :(
                  – Satish
                  Aug 8 at 23:10




                  column: illegal option -- o :(
                  – Satish
                  Aug 8 at 23:10




                  1




                  1




                  nevermid my bad, i was on MacBookPro after moving my file on Linux it seems looking good
                  – Satish
                  Aug 8 at 23:17




                  nevermid my bad, i was on MacBookPro after moving my file on Linux it seems looking good
                  – Satish
                  Aug 8 at 23:17




                  1




                  1




                  Damn!!! it you are the beast in awk take a bow!!!
                  – Satish
                  Aug 8 at 23:22




                  Damn!!! it you are the beast in awk take a bow!!!
                  – Satish
                  Aug 8 at 23:22












                  up vote
                  1
                  down vote













                  $ sed -e '/--/d;N;s/n/ /;s/[[:blank:]]+//;s/[[:blank:]]+[^(]+/, /' file | column -ts ','
                  foo-6-25.example.com: ( 49)
                  foo-5-4.example.com: ( 19)
                  foo-8-28.example.com: ( 43)
                  foo-9-7.example.com: ( 91)
                  foo-5-19.idmz.example.com: ( 19)
                  foo-7-3.example.com: ( 20)


                  If you don't want any spacing/alignment of the 2nd column you can omit the column command:



                  $ sed -e '/--/d;N;s/n/ /;s/[[:blank:]]+//;s/[[:blank:]]+[^(]+/ /' file
                  foo-6-25.example.com: ( 49)
                  foo-5-4.example.com: ( 19)
                  foo-8-28.example.com: ( 43)
                  foo-9-7.example.com: ( 91)
                  foo-5-19.idmz.example.com: ( 19)
                  foo-7-3.example.com: ( 20)


                  If you're dealing with GNU sed you can reduce it further:



                  $ sed -e '/--/d;N;s/n/ /;s/[ ]+//;s/[ ]+[^(]+/ /' file





                  share|improve this answer






















                  • I have tired your solution but its printing everything in single like like foo-010101-5-4.example.com: 1 Var. Speed System Board Normal Yes Normal ( 19)
                    – Satish
                    Aug 8 at 23:03










                  • I suspect your output isn't just spaces as you're showing in the example data, is it?
                    – slm♦
                    Aug 8 at 23:05










                  • my file is correct and i am replacing file with my filename.txt
                    – Satish
                    Aug 8 at 23:12










                  • I showed this style of example b/c unlike the awk you can more easily add/remove portions of this solution to see how it's working. Each /../; block in the sed is doing something specific, you can take them all out and then add them back in 1 at a time to see how they work and work towards you solution rather than have to have a lot of knowledge about awk.
                    – slm♦
                    Aug 8 at 23:13















                  up vote
                  1
                  down vote













                  $ sed -e '/--/d;N;s/n/ /;s/[[:blank:]]+//;s/[[:blank:]]+[^(]+/, /' file | column -ts ','
                  foo-6-25.example.com: ( 49)
                  foo-5-4.example.com: ( 19)
                  foo-8-28.example.com: ( 43)
                  foo-9-7.example.com: ( 91)
                  foo-5-19.idmz.example.com: ( 19)
                  foo-7-3.example.com: ( 20)


                  If you don't want any spacing/alignment of the 2nd column you can omit the column command:



                  $ sed -e '/--/d;N;s/n/ /;s/[[:blank:]]+//;s/[[:blank:]]+[^(]+/ /' file
                  foo-6-25.example.com: ( 49)
                  foo-5-4.example.com: ( 19)
                  foo-8-28.example.com: ( 43)
                  foo-9-7.example.com: ( 91)
                  foo-5-19.idmz.example.com: ( 19)
                  foo-7-3.example.com: ( 20)


                  If you're dealing with GNU sed you can reduce it further:



                  $ sed -e '/--/d;N;s/n/ /;s/[ ]+//;s/[ ]+[^(]+/ /' file





                  share|improve this answer






















                  • I have tired your solution but its printing everything in single like like foo-010101-5-4.example.com: 1 Var. Speed System Board Normal Yes Normal ( 19)
                    – Satish
                    Aug 8 at 23:03










                  • I suspect your output isn't just spaces as you're showing in the example data, is it?
                    – slm♦
                    Aug 8 at 23:05










                  • my file is correct and i am replacing file with my filename.txt
                    – Satish
                    Aug 8 at 23:12










                  • I showed this style of example b/c unlike the awk you can more easily add/remove portions of this solution to see how it's working. Each /../; block in the sed is doing something specific, you can take them all out and then add them back in 1 at a time to see how they work and work towards you solution rather than have to have a lot of knowledge about awk.
                    – slm♦
                    Aug 8 at 23:13













                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  $ sed -e '/--/d;N;s/n/ /;s/[[:blank:]]+//;s/[[:blank:]]+[^(]+/, /' file | column -ts ','
                  foo-6-25.example.com: ( 49)
                  foo-5-4.example.com: ( 19)
                  foo-8-28.example.com: ( 43)
                  foo-9-7.example.com: ( 91)
                  foo-5-19.idmz.example.com: ( 19)
                  foo-7-3.example.com: ( 20)


                  If you don't want any spacing/alignment of the 2nd column you can omit the column command:



                  $ sed -e '/--/d;N;s/n/ /;s/[[:blank:]]+//;s/[[:blank:]]+[^(]+/ /' file
                  foo-6-25.example.com: ( 49)
                  foo-5-4.example.com: ( 19)
                  foo-8-28.example.com: ( 43)
                  foo-9-7.example.com: ( 91)
                  foo-5-19.idmz.example.com: ( 19)
                  foo-7-3.example.com: ( 20)


                  If you're dealing with GNU sed you can reduce it further:



                  $ sed -e '/--/d;N;s/n/ /;s/[ ]+//;s/[ ]+[^(]+/ /' file





                  share|improve this answer














                  $ sed -e '/--/d;N;s/n/ /;s/[[:blank:]]+//;s/[[:blank:]]+[^(]+/, /' file | column -ts ','
                  foo-6-25.example.com: ( 49)
                  foo-5-4.example.com: ( 19)
                  foo-8-28.example.com: ( 43)
                  foo-9-7.example.com: ( 91)
                  foo-5-19.idmz.example.com: ( 19)
                  foo-7-3.example.com: ( 20)


                  If you don't want any spacing/alignment of the 2nd column you can omit the column command:



                  $ sed -e '/--/d;N;s/n/ /;s/[[:blank:]]+//;s/[[:blank:]]+[^(]+/ /' file
                  foo-6-25.example.com: ( 49)
                  foo-5-4.example.com: ( 19)
                  foo-8-28.example.com: ( 43)
                  foo-9-7.example.com: ( 91)
                  foo-5-19.idmz.example.com: ( 19)
                  foo-7-3.example.com: ( 20)


                  If you're dealing with GNU sed you can reduce it further:



                  $ sed -e '/--/d;N;s/n/ /;s/[ ]+//;s/[ ]+[^(]+/ /' file






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Aug 8 at 23:12

























                  answered Aug 8 at 22:49









                  slm♦

                  237k65483657




                  237k65483657











                  • I have tired your solution but its printing everything in single like like foo-010101-5-4.example.com: 1 Var. Speed System Board Normal Yes Normal ( 19)
                    – Satish
                    Aug 8 at 23:03










                  • I suspect your output isn't just spaces as you're showing in the example data, is it?
                    – slm♦
                    Aug 8 at 23:05










                  • my file is correct and i am replacing file with my filename.txt
                    – Satish
                    Aug 8 at 23:12










                  • I showed this style of example b/c unlike the awk you can more easily add/remove portions of this solution to see how it's working. Each /../; block in the sed is doing something specific, you can take them all out and then add them back in 1 at a time to see how they work and work towards you solution rather than have to have a lot of knowledge about awk.
                    – slm♦
                    Aug 8 at 23:13

















                  • I have tired your solution but its printing everything in single like like foo-010101-5-4.example.com: 1 Var. Speed System Board Normal Yes Normal ( 19)
                    – Satish
                    Aug 8 at 23:03










                  • I suspect your output isn't just spaces as you're showing in the example data, is it?
                    – slm♦
                    Aug 8 at 23:05










                  • my file is correct and i am replacing file with my filename.txt
                    – Satish
                    Aug 8 at 23:12










                  • I showed this style of example b/c unlike the awk you can more easily add/remove portions of this solution to see how it's working. Each /../; block in the sed is doing something specific, you can take them all out and then add them back in 1 at a time to see how they work and work towards you solution rather than have to have a lot of knowledge about awk.
                    – slm♦
                    Aug 8 at 23:13
















                  I have tired your solution but its printing everything in single like like foo-010101-5-4.example.com: 1 Var. Speed System Board Normal Yes Normal ( 19)
                  – Satish
                  Aug 8 at 23:03




                  I have tired your solution but its printing everything in single like like foo-010101-5-4.example.com: 1 Var. Speed System Board Normal Yes Normal ( 19)
                  – Satish
                  Aug 8 at 23:03












                  I suspect your output isn't just spaces as you're showing in the example data, is it?
                  – slm♦
                  Aug 8 at 23:05




                  I suspect your output isn't just spaces as you're showing in the example data, is it?
                  – slm♦
                  Aug 8 at 23:05












                  my file is correct and i am replacing file with my filename.txt
                  – Satish
                  Aug 8 at 23:12




                  my file is correct and i am replacing file with my filename.txt
                  – Satish
                  Aug 8 at 23:12












                  I showed this style of example b/c unlike the awk you can more easily add/remove portions of this solution to see how it's working. Each /../; block in the sed is doing something specific, you can take them all out and then add them back in 1 at a time to see how they work and work towards you solution rather than have to have a lot of knowledge about awk.
                  – slm♦
                  Aug 8 at 23:13





                  I showed this style of example b/c unlike the awk you can more easily add/remove portions of this solution to see how it's working. Each /../; block in the sed is doing something specific, you can take them all out and then add them back in 1 at a time to see how they work and work towards you solution rather than have to have a lot of knowledge about awk.
                  – slm♦
                  Aug 8 at 23:13











                  up vote
                  1
                  down vote













                  % perl -ne 'print(($_ .= <>) =~ s/n.*(/ (/r) if /:/' input.file


                  outputs:



                  foo-6-25.example.com: ( 49)
                  foo-5-4.example.com: ( 19)
                  foo-8-28.example.com: ( 43)
                  foo-9-7.example.com: ( 91)
                  foo-5-19.idmz.example.com: ( 19)
                  foo-7-3.example.com: ( 20)


                  explanation:



                  1. on a line comprising :, it appends the next line to the current record.

                  2. then deletes from the newline of the original line till the last ( and replaces this with ( and prints the record.

                  3. Since perl was invoked with -n, records not affected are not printed.

                  Sed has a very compact way to write the same:



                  sed -ne '/:/N;s/n.*(/ (/p' input.file





                  share|improve this answer
























                    up vote
                    1
                    down vote













                    % perl -ne 'print(($_ .= <>) =~ s/n.*(/ (/r) if /:/' input.file


                    outputs:



                    foo-6-25.example.com: ( 49)
                    foo-5-4.example.com: ( 19)
                    foo-8-28.example.com: ( 43)
                    foo-9-7.example.com: ( 91)
                    foo-5-19.idmz.example.com: ( 19)
                    foo-7-3.example.com: ( 20)


                    explanation:



                    1. on a line comprising :, it appends the next line to the current record.

                    2. then deletes from the newline of the original line till the last ( and replaces this with ( and prints the record.

                    3. Since perl was invoked with -n, records not affected are not printed.

                    Sed has a very compact way to write the same:



                    sed -ne '/:/N;s/n.*(/ (/p' input.file





                    share|improve this answer






















                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      % perl -ne 'print(($_ .= <>) =~ s/n.*(/ (/r) if /:/' input.file


                      outputs:



                      foo-6-25.example.com: ( 49)
                      foo-5-4.example.com: ( 19)
                      foo-8-28.example.com: ( 43)
                      foo-9-7.example.com: ( 91)
                      foo-5-19.idmz.example.com: ( 19)
                      foo-7-3.example.com: ( 20)


                      explanation:



                      1. on a line comprising :, it appends the next line to the current record.

                      2. then deletes from the newline of the original line till the last ( and replaces this with ( and prints the record.

                      3. Since perl was invoked with -n, records not affected are not printed.

                      Sed has a very compact way to write the same:



                      sed -ne '/:/N;s/n.*(/ (/p' input.file





                      share|improve this answer












                      % perl -ne 'print(($_ .= <>) =~ s/n.*(/ (/r) if /:/' input.file


                      outputs:



                      foo-6-25.example.com: ( 49)
                      foo-5-4.example.com: ( 19)
                      foo-8-28.example.com: ( 43)
                      foo-9-7.example.com: ( 91)
                      foo-5-19.idmz.example.com: ( 19)
                      foo-7-3.example.com: ( 20)


                      explanation:



                      1. on a line comprising :, it appends the next line to the current record.

                      2. then deletes from the newline of the original line till the last ( and replaces this with ( and prints the record.

                      3. Since perl was invoked with -n, records not affected are not printed.

                      Sed has a very compact way to write the same:



                      sed -ne '/:/N;s/n.*(/ (/p' input.file






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Aug 9 at 2:21









                      Rakesh Sharma

                      47813




                      47813




















                          up vote
                          0
                          down vote













                          < file perl -pe 's/n//g;s/(foo)/n$1/g' | perl -pe 's/:.*(/: (/g'





                          share|improve this answer
























                            up vote
                            0
                            down vote













                            < file perl -pe 's/n//g;s/(foo)/n$1/g' | perl -pe 's/:.*(/: (/g'





                            share|improve this answer






















                              up vote
                              0
                              down vote










                              up vote
                              0
                              down vote









                              < file perl -pe 's/n//g;s/(foo)/n$1/g' | perl -pe 's/:.*(/: (/g'





                              share|improve this answer












                              < file perl -pe 's/n//g;s/(foo)/n$1/g' | perl -pe 's/:.*(/: (/g'






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Aug 8 at 22:42









                              user1133275

                              2,247412




                              2,247412




















                                  up vote
                                  0
                                  down vote













                                  Using awk in paragraph mode



                                  awk -vRS='n[ t]*--' '
                                  match($0, /( *[0-9]+)/) print $1, substr($0,RSTART,RLENGTH)
                                  ' file
                                  foo-6-25.example.com: ( 49)
                                  foo-5-4.example.com: ( 19)
                                  foo-8-28.example.com: ( 43)
                                  foo-9-7.example.com: ( 91)
                                  foo-5-19.idmz.example.com: ( 19)
                                  foo-7-3.example.com: ( 20)





                                  share|improve this answer


























                                    up vote
                                    0
                                    down vote













                                    Using awk in paragraph mode



                                    awk -vRS='n[ t]*--' '
                                    match($0, /( *[0-9]+)/) print $1, substr($0,RSTART,RLENGTH)
                                    ' file
                                    foo-6-25.example.com: ( 49)
                                    foo-5-4.example.com: ( 19)
                                    foo-8-28.example.com: ( 43)
                                    foo-9-7.example.com: ( 91)
                                    foo-5-19.idmz.example.com: ( 19)
                                    foo-7-3.example.com: ( 20)





                                    share|improve this answer
























                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      Using awk in paragraph mode



                                      awk -vRS='n[ t]*--' '
                                      match($0, /( *[0-9]+)/) print $1, substr($0,RSTART,RLENGTH)
                                      ' file
                                      foo-6-25.example.com: ( 49)
                                      foo-5-4.example.com: ( 19)
                                      foo-8-28.example.com: ( 43)
                                      foo-9-7.example.com: ( 91)
                                      foo-5-19.idmz.example.com: ( 19)
                                      foo-7-3.example.com: ( 20)





                                      share|improve this answer














                                      Using awk in paragraph mode



                                      awk -vRS='n[ t]*--' '
                                      match($0, /( *[0-9]+)/) print $1, substr($0,RSTART,RLENGTH)
                                      ' file
                                      foo-6-25.example.com: ( 49)
                                      foo-5-4.example.com: ( 19)
                                      foo-8-28.example.com: ( 43)
                                      foo-9-7.example.com: ( 91)
                                      foo-5-19.idmz.example.com: ( 19)
                                      foo-7-3.example.com: ( 20)






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Aug 8 at 23:20

























                                      answered Aug 8 at 22:40









                                      steeldriver

                                      31.8k34979




                                      31.8k34979




















                                          up vote
                                          0
                                          down vote













                                          $ sed -nE '/:/N;s/^[[:space:]]+//; s/:[^(]+/: /p;' file
                                          foo-6-25.example.com: ( 49)
                                          foo-5-4.example.com: ( 19)
                                          foo-8-28.example.com: ( 43)
                                          foo-9-7.example.com: ( 91)
                                          foo-5-19.idmz.example.com: ( 19)
                                          foo-7-3.example.com: ( 20)


                                          If there are no initial spaces or tabs on each line, then just



                                          sed -nE '/:/N;s/:[^(]+/: /p;' file


                                          The (first) sed script annotated (assumes -E and -n):



                                          /:/ # this line contains a host name
                                          N; # append the next line from the input
                                          s/^[[:space:]]+//; # remove the initial spaces or tabs
                                          s/:[^(]+/: /p; # remove the bit between the ":" and "(" and print






                                          share|improve this answer
























                                            up vote
                                            0
                                            down vote













                                            $ sed -nE '/:/N;s/^[[:space:]]+//; s/:[^(]+/: /p;' file
                                            foo-6-25.example.com: ( 49)
                                            foo-5-4.example.com: ( 19)
                                            foo-8-28.example.com: ( 43)
                                            foo-9-7.example.com: ( 91)
                                            foo-5-19.idmz.example.com: ( 19)
                                            foo-7-3.example.com: ( 20)


                                            If there are no initial spaces or tabs on each line, then just



                                            sed -nE '/:/N;s/:[^(]+/: /p;' file


                                            The (first) sed script annotated (assumes -E and -n):



                                            /:/ # this line contains a host name
                                            N; # append the next line from the input
                                            s/^[[:space:]]+//; # remove the initial spaces or tabs
                                            s/:[^(]+/: /p; # remove the bit between the ":" and "(" and print






                                            share|improve this answer






















                                              up vote
                                              0
                                              down vote










                                              up vote
                                              0
                                              down vote









                                              $ sed -nE '/:/N;s/^[[:space:]]+//; s/:[^(]+/: /p;' file
                                              foo-6-25.example.com: ( 49)
                                              foo-5-4.example.com: ( 19)
                                              foo-8-28.example.com: ( 43)
                                              foo-9-7.example.com: ( 91)
                                              foo-5-19.idmz.example.com: ( 19)
                                              foo-7-3.example.com: ( 20)


                                              If there are no initial spaces or tabs on each line, then just



                                              sed -nE '/:/N;s/:[^(]+/: /p;' file


                                              The (first) sed script annotated (assumes -E and -n):



                                              /:/ # this line contains a host name
                                              N; # append the next line from the input
                                              s/^[[:space:]]+//; # remove the initial spaces or tabs
                                              s/:[^(]+/: /p; # remove the bit between the ":" and "(" and print






                                              share|improve this answer












                                              $ sed -nE '/:/N;s/^[[:space:]]+//; s/:[^(]+/: /p;' file
                                              foo-6-25.example.com: ( 49)
                                              foo-5-4.example.com: ( 19)
                                              foo-8-28.example.com: ( 43)
                                              foo-9-7.example.com: ( 91)
                                              foo-5-19.idmz.example.com: ( 19)
                                              foo-7-3.example.com: ( 20)


                                              If there are no initial spaces or tabs on each line, then just



                                              sed -nE '/:/N;s/:[^(]+/: /p;' file


                                              The (first) sed script annotated (assumes -E and -n):



                                              /:/ # this line contains a host name
                                              N; # append the next line from the input
                                              s/^[[:space:]]+//; # remove the initial spaces or tabs
                                              s/:[^(]+/: /p; # remove the bit between the ":" and "(" and print







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Aug 9 at 7:25









                                              Kusalananda

                                              104k14206324




                                              104k14206324



























                                                   

                                                  draft saved


                                                  draft discarded















































                                                   


                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f461389%2fregex-to-format-files-output%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest













































































                                                  Comments

                                                  Popular posts from this blog

                                                  List of Gilmore Girls characters

                                                  What does second last employer means? [closed]

                                                  Confectionery