How can I remove rest of file from string for all files?Remove all text before last space in text file from CLIHow to remove domain part of each line from text file containing email addressesFilter consecutive identical characters using Sed and GrepHow to find logs that contain certain string in lots of gz/regular files and save it as one/more txt files?command to remove specific string from multiple filesRemove all non-numeric characters from text filesDelete all lines from middle of a line matching a string until the second string match is foundHow sed can be used to replace multiple patterns within a string for different patternsBash, get values from json file, all in one line

Why don't all States switch to all postal voting?

Do I have to start a new career after installing Breaking Grounds expansion to Kerbal Space Program?

Why does China have so few nuclear weapons?

Possible to read "lines" from a variable?

Why is casting a DATE field to VARCHAR datatype non-deterministic and is there a way to make it deterministic?

Left and right brace around different number of rows without tikzmark

Is using Swiss Francs (CHF) cheaper than Euros (EUR) in Switzerland?

How much of a discount should I seek when prepaying a whole year's rent?

Will the same Javascript fetched by HTTP and HTTPS be cached separately by the browser?

What are the downsides of being a debt-free country (no foreign national debt)?

I keep rewriting the same section of my story. How do I move forward?

Monthly budget screen - need to take into account whether it's early/late in the month

Why do we have to make the Sign of the Cross physically?

finding IP return hex address

How do I take a photo so the moon doesn't have bright "rays"?

How to say No to idea given by team member, when I know from my experience that it is going to fail?

What is the reason for difference between integer division and float to int conversion in python?

Is it possible for a tiger's tail to be taken off and replaced with a living cobra, with both creatures still alive?

Why is there no FPU on (most) DSP chips?

Applying field calculator for multiple layers at once

What is considered room temperature in Celsius?

Are there languages with verb tenses, but no conjugation?

How does a religion based around destroying the world attract followers

What is a Aged Rope Phrase™?



How can I remove rest of file from string for all files?


Remove all text before last space in text file from CLIHow to remove domain part of each line from text file containing email addressesFilter consecutive identical characters using Sed and GrepHow to find logs that contain certain string in lots of gz/regular files and save it as one/more txt files?command to remove specific string from multiple filesRemove all non-numeric characters from text filesDelete all lines from middle of a line matching a string until the second string match is foundHow sed can be used to replace multiple patterns within a string for different patternsBash, get values from json file, all in one line






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









9


















How can I remove rest of file from string for all files?



For example, input files 1 and 2, string is ddd.



Input file 1.



aaa
bbb
ccc
ddfbbd dddaaa
eee


Output file 1.



aaa
bbb
ccc
ddfbbd


Input file 2.



ccc
aergddd
dasdvsdb


Output file 2.



ccc
aerg









share|improve this question

































    9


















    How can I remove rest of file from string for all files?



    For example, input files 1 and 2, string is ddd.



    Input file 1.



    aaa
    bbb
    ccc
    ddfbbd dddaaa
    eee


    Output file 1.



    aaa
    bbb
    ccc
    ddfbbd


    Input file 2.



    ccc
    aergddd
    dasdvsdb


    Output file 2.



    ccc
    aerg









    share|improve this question





























      9













      9









      9


      1






      How can I remove rest of file from string for all files?



      For example, input files 1 and 2, string is ddd.



      Input file 1.



      aaa
      bbb
      ccc
      ddfbbd dddaaa
      eee


      Output file 1.



      aaa
      bbb
      ccc
      ddfbbd


      Input file 2.



      ccc
      aergddd
      dasdvsdb


      Output file 2.



      ccc
      aerg









      share|improve this question
















      How can I remove rest of file from string for all files?



      For example, input files 1 and 2, string is ddd.



      Input file 1.



      aaa
      bbb
      ccc
      ddfbbd dddaaa
      eee


      Output file 1.



      aaa
      bbb
      ccc
      ddfbbd


      Input file 2.



      ccc
      aergddd
      dasdvsdb


      Output file 2.



      ccc
      aerg






      text-processing sed






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 1 at 15:06









      Solomon Ucko

      1051 silver badge5 bronze badges




      1051 silver badge5 bronze badges










      asked Sep 30 at 14:31









      genderbeegenderbee

      5429 bronze badges




      5429 bronze badges























          3 Answers
          3






          active

          oldest

          votes


















          11



















          With GNU sed:



          str="ddd"
          for file in 1 2; do
          sed -i "/$str/ s/$str.*//; q" "$file"
          done


          This needs to be in a loop: otherwise the q command would abort the whole process after processing only the first file.






          share|improve this answer






















          • 1





            Found it interesting you answered a similar question in Unix & LInux 8 years ago: unix.stackexchange.com/a/11323/200094

            – WinEunuuchs2Unix
            Oct 1 at 10:56











          • I wonder if you could just d instead of q

            – D. Ben Knoble
            Oct 1 at 18:31











          • You're welcome to try. sed is not a go-to tool for me once things get more complicated that what I did.

            – glenn jackman
            Oct 1 at 19:08


















          7



















          With Perl:



          perl -i -0777 -pe 's/ddd[sS]*//' file


          or



          perl -i -0777 -pe 's/ddd.*//s' file


          • -i: modify the file in place.


          • -0777: force Perl to slurp the file as whole, not line by line.



          • -pe:




            • -p: loop Perl code.


            • -e: execute Perl code.


          • 's/ddd[sS]*//': replace everything (every whitespace (s) and non-whitespace (S) character) after ddd (including it) with an empty string.


          • 's/ddd.*//s': replace everything (.*) after ddd (including it) with an empty string. The s flag at the end makes .* also match newlines (thanks @glennjackman).


          More about Perl flags can be found here.






          share|improve this answer



























          • Why not just s/ddd.*//?

            – dessert
            Sep 30 at 16:41











          • @dessert .* stops matching at newline.

            – user3140225
            Sep 30 at 16:53












          • You can use..* if you add the m or s flag to the s/// command (I forget which one)

            – glenn jackman
            Sep 30 at 21:23











          • @glennjackman It's the s flag. Thanks!

            – user3140225
            Oct 1 at 7:33


















          4



















          with GNU awk, we can do:



          awk 'function output() print >>FILENAME".out" 
          /ddd/ sub(/ddd.*/,""); output(); nextfile
          output() ' file[12]


          to change inplace, which it makes command even simple:



          gawk -i inplace '/ddd/ sub(/ddd.*/,""); print ; nextfile 1' file[12]





          share|improve this answer




























            Your Answer








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

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

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.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%2faskubuntu.com%2fquestions%2f1177693%2fhow-can-i-remove-rest-of-file-from-string-for-all-files%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown


























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            11



















            With GNU sed:



            str="ddd"
            for file in 1 2; do
            sed -i "/$str/ s/$str.*//; q" "$file"
            done


            This needs to be in a loop: otherwise the q command would abort the whole process after processing only the first file.






            share|improve this answer






















            • 1





              Found it interesting you answered a similar question in Unix & LInux 8 years ago: unix.stackexchange.com/a/11323/200094

              – WinEunuuchs2Unix
              Oct 1 at 10:56











            • I wonder if you could just d instead of q

              – D. Ben Knoble
              Oct 1 at 18:31











            • You're welcome to try. sed is not a go-to tool for me once things get more complicated that what I did.

              – glenn jackman
              Oct 1 at 19:08















            11



















            With GNU sed:



            str="ddd"
            for file in 1 2; do
            sed -i "/$str/ s/$str.*//; q" "$file"
            done


            This needs to be in a loop: otherwise the q command would abort the whole process after processing only the first file.






            share|improve this answer






















            • 1





              Found it interesting you answered a similar question in Unix & LInux 8 years ago: unix.stackexchange.com/a/11323/200094

              – WinEunuuchs2Unix
              Oct 1 at 10:56











            • I wonder if you could just d instead of q

              – D. Ben Knoble
              Oct 1 at 18:31











            • You're welcome to try. sed is not a go-to tool for me once things get more complicated that what I did.

              – glenn jackman
              Oct 1 at 19:08













            11















            11











            11









            With GNU sed:



            str="ddd"
            for file in 1 2; do
            sed -i "/$str/ s/$str.*//; q" "$file"
            done


            This needs to be in a loop: otherwise the q command would abort the whole process after processing only the first file.






            share|improve this answer
















            With GNU sed:



            str="ddd"
            for file in 1 2; do
            sed -i "/$str/ s/$str.*//; q" "$file"
            done


            This needs to be in a loop: otherwise the q command would abort the whole process after processing only the first file.







            share|improve this answer















            share|improve this answer




            share|improve this answer








            edited Sep 30 at 15:29









            dessert

            30.5k7 gold badges89 silver badges123 bronze badges




            30.5k7 gold badges89 silver badges123 bronze badges










            answered Sep 30 at 15:09









            glenn jackmanglenn jackman

            14k1 gold badge28 silver badges48 bronze badges




            14k1 gold badge28 silver badges48 bronze badges










            • 1





              Found it interesting you answered a similar question in Unix & LInux 8 years ago: unix.stackexchange.com/a/11323/200094

              – WinEunuuchs2Unix
              Oct 1 at 10:56











            • I wonder if you could just d instead of q

              – D. Ben Knoble
              Oct 1 at 18:31











            • You're welcome to try. sed is not a go-to tool for me once things get more complicated that what I did.

              – glenn jackman
              Oct 1 at 19:08












            • 1





              Found it interesting you answered a similar question in Unix & LInux 8 years ago: unix.stackexchange.com/a/11323/200094

              – WinEunuuchs2Unix
              Oct 1 at 10:56











            • I wonder if you could just d instead of q

              – D. Ben Knoble
              Oct 1 at 18:31











            • You're welcome to try. sed is not a go-to tool for me once things get more complicated that what I did.

              – glenn jackman
              Oct 1 at 19:08







            1




            1





            Found it interesting you answered a similar question in Unix & LInux 8 years ago: unix.stackexchange.com/a/11323/200094

            – WinEunuuchs2Unix
            Oct 1 at 10:56





            Found it interesting you answered a similar question in Unix & LInux 8 years ago: unix.stackexchange.com/a/11323/200094

            – WinEunuuchs2Unix
            Oct 1 at 10:56













            I wonder if you could just d instead of q

            – D. Ben Knoble
            Oct 1 at 18:31





            I wonder if you could just d instead of q

            – D. Ben Knoble
            Oct 1 at 18:31













            You're welcome to try. sed is not a go-to tool for me once things get more complicated that what I did.

            – glenn jackman
            Oct 1 at 19:08





            You're welcome to try. sed is not a go-to tool for me once things get more complicated that what I did.

            – glenn jackman
            Oct 1 at 19:08













            7



















            With Perl:



            perl -i -0777 -pe 's/ddd[sS]*//' file


            or



            perl -i -0777 -pe 's/ddd.*//s' file


            • -i: modify the file in place.


            • -0777: force Perl to slurp the file as whole, not line by line.



            • -pe:




              • -p: loop Perl code.


              • -e: execute Perl code.


            • 's/ddd[sS]*//': replace everything (every whitespace (s) and non-whitespace (S) character) after ddd (including it) with an empty string.


            • 's/ddd.*//s': replace everything (.*) after ddd (including it) with an empty string. The s flag at the end makes .* also match newlines (thanks @glennjackman).


            More about Perl flags can be found here.






            share|improve this answer



























            • Why not just s/ddd.*//?

              – dessert
              Sep 30 at 16:41











            • @dessert .* stops matching at newline.

              – user3140225
              Sep 30 at 16:53












            • You can use..* if you add the m or s flag to the s/// command (I forget which one)

              – glenn jackman
              Sep 30 at 21:23











            • @glennjackman It's the s flag. Thanks!

              – user3140225
              Oct 1 at 7:33















            7



















            With Perl:



            perl -i -0777 -pe 's/ddd[sS]*//' file


            or



            perl -i -0777 -pe 's/ddd.*//s' file


            • -i: modify the file in place.


            • -0777: force Perl to slurp the file as whole, not line by line.



            • -pe:




              • -p: loop Perl code.


              • -e: execute Perl code.


            • 's/ddd[sS]*//': replace everything (every whitespace (s) and non-whitespace (S) character) after ddd (including it) with an empty string.


            • 's/ddd.*//s': replace everything (.*) after ddd (including it) with an empty string. The s flag at the end makes .* also match newlines (thanks @glennjackman).


            More about Perl flags can be found here.






            share|improve this answer



























            • Why not just s/ddd.*//?

              – dessert
              Sep 30 at 16:41











            • @dessert .* stops matching at newline.

              – user3140225
              Sep 30 at 16:53












            • You can use..* if you add the m or s flag to the s/// command (I forget which one)

              – glenn jackman
              Sep 30 at 21:23











            • @glennjackman It's the s flag. Thanks!

              – user3140225
              Oct 1 at 7:33













            7















            7











            7









            With Perl:



            perl -i -0777 -pe 's/ddd[sS]*//' file


            or



            perl -i -0777 -pe 's/ddd.*//s' file


            • -i: modify the file in place.


            • -0777: force Perl to slurp the file as whole, not line by line.



            • -pe:




              • -p: loop Perl code.


              • -e: execute Perl code.


            • 's/ddd[sS]*//': replace everything (every whitespace (s) and non-whitespace (S) character) after ddd (including it) with an empty string.


            • 's/ddd.*//s': replace everything (.*) after ddd (including it) with an empty string. The s flag at the end makes .* also match newlines (thanks @glennjackman).


            More about Perl flags can be found here.






            share|improve this answer
















            With Perl:



            perl -i -0777 -pe 's/ddd[sS]*//' file


            or



            perl -i -0777 -pe 's/ddd.*//s' file


            • -i: modify the file in place.


            • -0777: force Perl to slurp the file as whole, not line by line.



            • -pe:




              • -p: loop Perl code.


              • -e: execute Perl code.


            • 's/ddd[sS]*//': replace everything (every whitespace (s) and non-whitespace (S) character) after ddd (including it) with an empty string.


            • 's/ddd.*//s': replace everything (.*) after ddd (including it) with an empty string. The s flag at the end makes .* also match newlines (thanks @glennjackman).


            More about Perl flags can be found here.







            share|improve this answer















            share|improve this answer




            share|improve this answer








            edited Oct 1 at 12:40

























            answered Sep 30 at 15:57









            user3140225user3140225

            2,7104 gold badges12 silver badges24 bronze badges




            2,7104 gold badges12 silver badges24 bronze badges















            • Why not just s/ddd.*//?

              – dessert
              Sep 30 at 16:41











            • @dessert .* stops matching at newline.

              – user3140225
              Sep 30 at 16:53












            • You can use..* if you add the m or s flag to the s/// command (I forget which one)

              – glenn jackman
              Sep 30 at 21:23











            • @glennjackman It's the s flag. Thanks!

              – user3140225
              Oct 1 at 7:33

















            • Why not just s/ddd.*//?

              – dessert
              Sep 30 at 16:41











            • @dessert .* stops matching at newline.

              – user3140225
              Sep 30 at 16:53












            • You can use..* if you add the m or s flag to the s/// command (I forget which one)

              – glenn jackman
              Sep 30 at 21:23











            • @glennjackman It's the s flag. Thanks!

              – user3140225
              Oct 1 at 7:33
















            Why not just s/ddd.*//?

            – dessert
            Sep 30 at 16:41





            Why not just s/ddd.*//?

            – dessert
            Sep 30 at 16:41













            @dessert .* stops matching at newline.

            – user3140225
            Sep 30 at 16:53






            @dessert .* stops matching at newline.

            – user3140225
            Sep 30 at 16:53














            You can use..* if you add the m or s flag to the s/// command (I forget which one)

            – glenn jackman
            Sep 30 at 21:23





            You can use..* if you add the m or s flag to the s/// command (I forget which one)

            – glenn jackman
            Sep 30 at 21:23













            @glennjackman It's the s flag. Thanks!

            – user3140225
            Oct 1 at 7:33





            @glennjackman It's the s flag. Thanks!

            – user3140225
            Oct 1 at 7:33











            4



















            with GNU awk, we can do:



            awk 'function output() print >>FILENAME".out" 
            /ddd/ sub(/ddd.*/,""); output(); nextfile
            output() ' file[12]


            to change inplace, which it makes command even simple:



            gawk -i inplace '/ddd/ sub(/ddd.*/,""); print ; nextfile 1' file[12]





            share|improve this answer































              4



















              with GNU awk, we can do:



              awk 'function output() print >>FILENAME".out" 
              /ddd/ sub(/ddd.*/,""); output(); nextfile
              output() ' file[12]


              to change inplace, which it makes command even simple:



              gawk -i inplace '/ddd/ sub(/ddd.*/,""); print ; nextfile 1' file[12]





              share|improve this answer





























                4















                4











                4









                with GNU awk, we can do:



                awk 'function output() print >>FILENAME".out" 
                /ddd/ sub(/ddd.*/,""); output(); nextfile
                output() ' file[12]


                to change inplace, which it makes command even simple:



                gawk -i inplace '/ddd/ sub(/ddd.*/,""); print ; nextfile 1' file[12]





                share|improve this answer
















                with GNU awk, we can do:



                awk 'function output() print >>FILENAME".out" 
                /ddd/ sub(/ddd.*/,""); output(); nextfile
                output() ' file[12]


                to change inplace, which it makes command even simple:



                gawk -i inplace '/ddd/ sub(/ddd.*/,""); print ; nextfile 1' file[12]






                share|improve this answer















                share|improve this answer




                share|improve this answer








                edited Oct 1 at 10:05

























                answered Sep 30 at 15:39









                αғsнιηαғsнιη

                28.8k23 gold badges107 silver badges170 bronze badges




                28.8k23 gold badges107 silver badges170 bronze badges































                    draft saved

                    draft discarded















































                    Thanks for contributing an answer to Ask Ubuntu!


                    • 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%2faskubuntu.com%2fquestions%2f1177693%2fhow-can-i-remove-rest-of-file-from-string-for-all-files%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

                    Tamil (spriik) Luke uk diar | Nawigatjuun

                    Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

                    Training a classifier when some of the features are unknownWhy does Gradient Boosting regression predict negative values when there are no negative y-values in my training set?How to improve an existing (trained) classifier?What is effect when I set up some self defined predisctor variables?Why Matlab neural network classification returns decimal values on prediction dataset?Fitting and transforming text data in training, testing, and validation setsHow to quantify the performance of the classifier (multi-class SVM) using the test data?How do I control for some patients providing multiple samples in my training data?Training and Test setTraining a convolutional neural network for image denoising in MatlabShouldn't an autoencoder with #(neurons in hidden layer) = #(neurons in input layer) be “perfect”?