Extract and replace string with bash [closed]

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











up vote
-1
down vote

favorite












I have a string like: func showFetchNextSliceFailed(result: UserResult<[Product]>) {



From this, I would like to 1). extract just showFetchNextSliceFailed 2). Insert it right above the given line, as #Mark showFetchNextSliceFailed.



 func showProductsList(result: UserResult<[Product]>) 



func showFetchNextSliceFailed(result: UserResult<[Product]>)




Expected output:



 #Mark showProductsList
func showProductsList(result: UserResult<[Product]>)



#Mark showFetchNextSliceFailed
func showFetchNextSliceFailed(result: UserResult<[Product]>)




I tried with grep and sed, but still can't figure out correctly how to extract and replace strings.







share|improve this question











closed as off-topic by Kusalananda, Jeff Schaller, vonbrand, X Tian, Anthony Geoghegan May 17 at 16:58


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question has been posted on multiple sites. Cross-posting is strongly discouraged; see the help center and community FAQ for more information." – Kusalananda, Jeff Schaller, vonbrand, X Tian, Anthony Geoghegan












  • Cross-posted: stackoverflow.com/questions/50382726/…
    – Kusalananda
    May 17 at 5:36














up vote
-1
down vote

favorite












I have a string like: func showFetchNextSliceFailed(result: UserResult<[Product]>) {



From this, I would like to 1). extract just showFetchNextSliceFailed 2). Insert it right above the given line, as #Mark showFetchNextSliceFailed.



 func showProductsList(result: UserResult<[Product]>) 



func showFetchNextSliceFailed(result: UserResult<[Product]>)




Expected output:



 #Mark showProductsList
func showProductsList(result: UserResult<[Product]>)



#Mark showFetchNextSliceFailed
func showFetchNextSliceFailed(result: UserResult<[Product]>)




I tried with grep and sed, but still can't figure out correctly how to extract and replace strings.







share|improve this question











closed as off-topic by Kusalananda, Jeff Schaller, vonbrand, X Tian, Anthony Geoghegan May 17 at 16:58


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question has been posted on multiple sites. Cross-posting is strongly discouraged; see the help center and community FAQ for more information." – Kusalananda, Jeff Schaller, vonbrand, X Tian, Anthony Geoghegan












  • Cross-posted: stackoverflow.com/questions/50382726/…
    – Kusalananda
    May 17 at 5:36












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I have a string like: func showFetchNextSliceFailed(result: UserResult<[Product]>) {



From this, I would like to 1). extract just showFetchNextSliceFailed 2). Insert it right above the given line, as #Mark showFetchNextSliceFailed.



 func showProductsList(result: UserResult<[Product]>) 



func showFetchNextSliceFailed(result: UserResult<[Product]>)




Expected output:



 #Mark showProductsList
func showProductsList(result: UserResult<[Product]>)



#Mark showFetchNextSliceFailed
func showFetchNextSliceFailed(result: UserResult<[Product]>)




I tried with grep and sed, but still can't figure out correctly how to extract and replace strings.







share|improve this question











I have a string like: func showFetchNextSliceFailed(result: UserResult<[Product]>) {



From this, I would like to 1). extract just showFetchNextSliceFailed 2). Insert it right above the given line, as #Mark showFetchNextSliceFailed.



 func showProductsList(result: UserResult<[Product]>) 



func showFetchNextSliceFailed(result: UserResult<[Product]>)




Expected output:



 #Mark showProductsList
func showProductsList(result: UserResult<[Product]>)



#Mark showFetchNextSliceFailed
func showFetchNextSliceFailed(result: UserResult<[Product]>)




I tried with grep and sed, but still can't figure out correctly how to extract and replace strings.









share|improve this question










share|improve this question




share|improve this question









asked May 17 at 2:50









Mehul Parmar

1014




1014




closed as off-topic by Kusalananda, Jeff Schaller, vonbrand, X Tian, Anthony Geoghegan May 17 at 16:58


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question has been posted on multiple sites. Cross-posting is strongly discouraged; see the help center and community FAQ for more information." – Kusalananda, Jeff Schaller, vonbrand, X Tian, Anthony Geoghegan




closed as off-topic by Kusalananda, Jeff Schaller, vonbrand, X Tian, Anthony Geoghegan May 17 at 16:58


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question has been posted on multiple sites. Cross-posting is strongly discouraged; see the help center and community FAQ for more information." – Kusalananda, Jeff Schaller, vonbrand, X Tian, Anthony Geoghegan











  • Cross-posted: stackoverflow.com/questions/50382726/…
    – Kusalananda
    May 17 at 5:36
















  • Cross-posted: stackoverflow.com/questions/50382726/…
    – Kusalananda
    May 17 at 5:36















Cross-posted: stackoverflow.com/questions/50382726/…
– Kusalananda
May 17 at 5:36




Cross-posted: stackoverflow.com/questions/50382726/…
– Kusalananda
May 17 at 5:36










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










With sed



  • copy the matching pattern space to hold space

  • modify the pattern space

  • recover the original from hold space and append it

Ex.



$ sed '/func show/ h; s/func (show[^(]*).*/#Mark 1/; G;' file
#Mark showProductsList
func showProductsList(result: UserResult<[Product]>)



#Mark showFetchNextSliceFailed
func showFetchNextSliceFailed(result: UserResult<[Product]>)







share|improve this answer




























    up vote
    0
    down vote













    If you have the following code file:



    INPUT:



     $more code
    func showProductsList(result: UserResult<[Product]>)



    func showFetchNextSliceFailed(result: UserResult<[Product]>)




    COMMAND:



    perl -i.bak -pe "s/^^( *|t*)funcs(w+)funcs(w+)/1#Mark 2n1func 2/g" code


    OUTPUT:



     #Mark showProductsList
    func showProductsList(result: UserResult<[Product]>)



    #Mark showFetchNextSliceFailed
    func showFetchNextSliceFailed(result: UserResult<[Product]>)




    EXPLANATIONS:




    • -i.bak in-place mode for perl with backup of the input file


    • s/^( *|t*)funcs(w+)funcs(w+)/1#Mark 2n1func 2/g find and replace mode detailed at demo





    share|improve this answer




























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote



      accepted










      With sed



      • copy the matching pattern space to hold space

      • modify the pattern space

      • recover the original from hold space and append it

      Ex.



      $ sed '/func show/ h; s/func (show[^(]*).*/#Mark 1/; G;' file
      #Mark showProductsList
      func showProductsList(result: UserResult<[Product]>)



      #Mark showFetchNextSliceFailed
      func showFetchNextSliceFailed(result: UserResult<[Product]>)







      share|improve this answer

























        up vote
        3
        down vote



        accepted










        With sed



        • copy the matching pattern space to hold space

        • modify the pattern space

        • recover the original from hold space and append it

        Ex.



        $ sed '/func show/ h; s/func (show[^(]*).*/#Mark 1/; G;' file
        #Mark showProductsList
        func showProductsList(result: UserResult<[Product]>)



        #Mark showFetchNextSliceFailed
        func showFetchNextSliceFailed(result: UserResult<[Product]>)







        share|improve this answer























          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          With sed



          • copy the matching pattern space to hold space

          • modify the pattern space

          • recover the original from hold space and append it

          Ex.



          $ sed '/func show/ h; s/func (show[^(]*).*/#Mark 1/; G;' file
          #Mark showProductsList
          func showProductsList(result: UserResult<[Product]>)



          #Mark showFetchNextSliceFailed
          func showFetchNextSliceFailed(result: UserResult<[Product]>)







          share|improve this answer













          With sed



          • copy the matching pattern space to hold space

          • modify the pattern space

          • recover the original from hold space and append it

          Ex.



          $ sed '/func show/ h; s/func (show[^(]*).*/#Mark 1/; G;' file
          #Mark showProductsList
          func showProductsList(result: UserResult<[Product]>)



          #Mark showFetchNextSliceFailed
          func showFetchNextSliceFailed(result: UserResult<[Product]>)








          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered May 17 at 3:21









          steeldriver

          31.2k34978




          31.2k34978






















              up vote
              0
              down vote













              If you have the following code file:



              INPUT:



               $more code
              func showProductsList(result: UserResult<[Product]>)



              func showFetchNextSliceFailed(result: UserResult<[Product]>)




              COMMAND:



              perl -i.bak -pe "s/^^( *|t*)funcs(w+)funcs(w+)/1#Mark 2n1func 2/g" code


              OUTPUT:



               #Mark showProductsList
              func showProductsList(result: UserResult<[Product]>)



              #Mark showFetchNextSliceFailed
              func showFetchNextSliceFailed(result: UserResult<[Product]>)




              EXPLANATIONS:




              • -i.bak in-place mode for perl with backup of the input file


              • s/^( *|t*)funcs(w+)funcs(w+)/1#Mark 2n1func 2/g find and replace mode detailed at demo





              share|improve this answer

























                up vote
                0
                down vote













                If you have the following code file:



                INPUT:



                 $more code
                func showProductsList(result: UserResult<[Product]>)



                func showFetchNextSliceFailed(result: UserResult<[Product]>)




                COMMAND:



                perl -i.bak -pe "s/^^( *|t*)funcs(w+)funcs(w+)/1#Mark 2n1func 2/g" code


                OUTPUT:



                 #Mark showProductsList
                func showProductsList(result: UserResult<[Product]>)



                #Mark showFetchNextSliceFailed
                func showFetchNextSliceFailed(result: UserResult<[Product]>)




                EXPLANATIONS:




                • -i.bak in-place mode for perl with backup of the input file


                • s/^( *|t*)funcs(w+)funcs(w+)/1#Mark 2n1func 2/g find and replace mode detailed at demo





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  If you have the following code file:



                  INPUT:



                   $more code
                  func showProductsList(result: UserResult<[Product]>)



                  func showFetchNextSliceFailed(result: UserResult<[Product]>)




                  COMMAND:



                  perl -i.bak -pe "s/^^( *|t*)funcs(w+)funcs(w+)/1#Mark 2n1func 2/g" code


                  OUTPUT:



                   #Mark showProductsList
                  func showProductsList(result: UserResult<[Product]>)



                  #Mark showFetchNextSliceFailed
                  func showFetchNextSliceFailed(result: UserResult<[Product]>)




                  EXPLANATIONS:




                  • -i.bak in-place mode for perl with backup of the input file


                  • s/^( *|t*)funcs(w+)funcs(w+)/1#Mark 2n1func 2/g find and replace mode detailed at demo





                  share|improve this answer













                  If you have the following code file:



                  INPUT:



                   $more code
                  func showProductsList(result: UserResult<[Product]>)



                  func showFetchNextSliceFailed(result: UserResult<[Product]>)




                  COMMAND:



                  perl -i.bak -pe "s/^^( *|t*)funcs(w+)funcs(w+)/1#Mark 2n1func 2/g" code


                  OUTPUT:



                   #Mark showProductsList
                  func showProductsList(result: UserResult<[Product]>)



                  #Mark showFetchNextSliceFailed
                  func showFetchNextSliceFailed(result: UserResult<[Product]>)




                  EXPLANATIONS:




                  • -i.bak in-place mode for perl with backup of the input file


                  • s/^( *|t*)funcs(w+)funcs(w+)/1#Mark 2n1func 2/g find and replace mode detailed at demo






                  share|improve this answer













                  share|improve this answer



                  share|improve this answer











                  answered May 17 at 5:33









                  Allan

                  1012




                  1012












                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay