How to replace the content to multiple files?How to replace a string on the 5th line of multiple text...

What formula could mimic the following curve?

What is the wife of a henpecked husband called?

How to acknowledge an embarrassing job interview, now that I work directly with the interviewer?

It took me a lot of time to make this, pls like. (YouTube Comments #1)

A starship is travelling at 0.9c and collides with a small rock. Will it leave a clean hole through, or will more happen?

What happens if a wizard reaches level 20 but has no 3rd-level spells that they can use with the Signature Spells feature?

Program that converts a number to a letter of the alphabet

How would an AI self awareness kill switch work?

Tikzing a circled star

Does the "particle exchange" operator have any validity?

Why zero tolerance on nudity in space?

Why can a 352GB NumPy ndarray be used on an 8GB memory macOS computer?

How is the Incom shipyard still in business?

How experienced do I need to be to go on a photography workshop?

Quenching swords in dragon blood; why?

"On one hand" vs "on the one hand."

Overfitting and Underfitting

If I delete my router's history can my ISP still provide it to my parents?

Why did the villain in the first Men in Black movie care about Earth's Cockroaches?

When does coming up with an idea constitute sufficient contribution for authorship?

Slow moving projectiles from a hand-held weapon - how do they reach the target?

What's a good word to describe a public place that looks like it wouldn't be rough?

Are Advaita and Karma theory completely contradictory?

What's the rationale behind the objections to these measures against human trafficking?



How to replace the content to multiple files?


How to replace a string on the 5th line of multiple text files?How do I replace multiple lines with single word in file(inplace replace)?Comparing two text filessplit a file based on pre-defined set of rowsReplace string with multiline file contentOnly delete files but not folders with rmcp directories and files, preserving directories and overwriting filesHow to replace a particular field in a file based on the content of another field?multiple file text replace with sedReplace all lines between two patterns (inclusive) with a file content













2















I have multiple files containing content like the following:



File 1

NC_12548 og789 |nd784 -2 -54 -6

NC_12548 og789 |nd784 -2 -54 -6

NC_12548 og789 |nd784 -2 -54 -6

File2

NC_54456 og789 |nd784 -5 -56 -6

NC_98123 og859 |nd784 -5 -84 -5

NC_689.1 og456 |nd784 -5 -54 +8

File3

NC_54456 og789 |nd784 -5 -56 -6

NC_98123 og859 |nd784 -5 -84 -5

NC_689.1 og456 |nd784 -5 -54 +8


I want to keep the only first to columns (NC_12345 og855) and discard rest of it. How can I do this?










share|improve this question









New contributor




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

























    2















    I have multiple files containing content like the following:



    File 1

    NC_12548 og789 |nd784 -2 -54 -6

    NC_12548 og789 |nd784 -2 -54 -6

    NC_12548 og789 |nd784 -2 -54 -6

    File2

    NC_54456 og789 |nd784 -5 -56 -6

    NC_98123 og859 |nd784 -5 -84 -5

    NC_689.1 og456 |nd784 -5 -54 +8

    File3

    NC_54456 og789 |nd784 -5 -56 -6

    NC_98123 og859 |nd784 -5 -84 -5

    NC_689.1 og456 |nd784 -5 -54 +8


    I want to keep the only first to columns (NC_12345 og855) and discard rest of it. How can I do this?










    share|improve this question









    New contributor




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























      2












      2








      2








      I have multiple files containing content like the following:



      File 1

      NC_12548 og789 |nd784 -2 -54 -6

      NC_12548 og789 |nd784 -2 -54 -6

      NC_12548 og789 |nd784 -2 -54 -6

      File2

      NC_54456 og789 |nd784 -5 -56 -6

      NC_98123 og859 |nd784 -5 -84 -5

      NC_689.1 og456 |nd784 -5 -54 +8

      File3

      NC_54456 og789 |nd784 -5 -56 -6

      NC_98123 og859 |nd784 -5 -84 -5

      NC_689.1 og456 |nd784 -5 -54 +8


      I want to keep the only first to columns (NC_12345 og855) and discard rest of it. How can I do this?










      share|improve this question









      New contributor




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












      I have multiple files containing content like the following:



      File 1

      NC_12548 og789 |nd784 -2 -54 -6

      NC_12548 og789 |nd784 -2 -54 -6

      NC_12548 og789 |nd784 -2 -54 -6

      File2

      NC_54456 og789 |nd784 -5 -56 -6

      NC_98123 og859 |nd784 -5 -84 -5

      NC_689.1 og456 |nd784 -5 -54 +8

      File3

      NC_54456 og789 |nd784 -5 -56 -6

      NC_98123 og859 |nd784 -5 -84 -5

      NC_689.1 og456 |nd784 -5 -54 +8


      I want to keep the only first to columns (NC_12345 og855) and discard rest of it. How can I do this?







      command-line bash text-processing sed perl






      share|improve this question









      New contributor




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











      share|improve this question









      New contributor




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









      share|improve this question




      share|improve this question








      edited 4 hours ago







      Arslan Tariq













      New contributor




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









      asked 4 hours ago









      Arslan TariqArslan Tariq

      112




      112




      New contributor




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





      New contributor





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






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






















          1 Answer
          1






          active

          oldest

          votes


















          2














          With awk you can just use | as column separator and print the first column:



          awk -F '|' '{print $1}' file1.txt file2.txt file3.txt


          output will be concatenaded. If it's necessary to keep output in separate files, consider using a for loop in shell around awk



          # assuming they're all in the same directory,  hence `*`
          for fname in ./file*.txt ; do
          # add extension to current file in "$fname" variable indicate new file
          # > does the actual redirection
          awk -F '|' '{print $1}' "$fname" > "$fname".new
          done


          Having new output in .new might be desirable for backup. Otherwise, we can use sed -i to perform in-file replacement. Run it without -i first for test trial



          # use file*.txt if they're all in the current directory
          sed -i 's/|.*$//' file1.txt file2.txt file3.txt
          sed -i 's/(^.*)|.*/1/g' file1.txt file2.txt file3.txt


          Another option is via Python:



          #!/usr/bin/env python3
          import sys

          for fname in sys.argv:
          with open(fname) as fd_read, open(fname+'.new','w') as fd_write:
          for line in fd_read:
          fd_write.write(line.split('|')[0] + 'n')


          This script is intended to be used as ./script.py file1.txt file2.txt file3.txt and will write output to new files with .new extension






          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/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });






            Arslan Tariq is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1122637%2fhow-to-replace-the-content-to-multiple-files%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            With awk you can just use | as column separator and print the first column:



            awk -F '|' '{print $1}' file1.txt file2.txt file3.txt


            output will be concatenaded. If it's necessary to keep output in separate files, consider using a for loop in shell around awk



            # assuming they're all in the same directory,  hence `*`
            for fname in ./file*.txt ; do
            # add extension to current file in "$fname" variable indicate new file
            # > does the actual redirection
            awk -F '|' '{print $1}' "$fname" > "$fname".new
            done


            Having new output in .new might be desirable for backup. Otherwise, we can use sed -i to perform in-file replacement. Run it without -i first for test trial



            # use file*.txt if they're all in the current directory
            sed -i 's/|.*$//' file1.txt file2.txt file3.txt
            sed -i 's/(^.*)|.*/1/g' file1.txt file2.txt file3.txt


            Another option is via Python:



            #!/usr/bin/env python3
            import sys

            for fname in sys.argv:
            with open(fname) as fd_read, open(fname+'.new','w') as fd_write:
            for line in fd_read:
            fd_write.write(line.split('|')[0] + 'n')


            This script is intended to be used as ./script.py file1.txt file2.txt file3.txt and will write output to new files with .new extension






            share|improve this answer






























              2














              With awk you can just use | as column separator and print the first column:



              awk -F '|' '{print $1}' file1.txt file2.txt file3.txt


              output will be concatenaded. If it's necessary to keep output in separate files, consider using a for loop in shell around awk



              # assuming they're all in the same directory,  hence `*`
              for fname in ./file*.txt ; do
              # add extension to current file in "$fname" variable indicate new file
              # > does the actual redirection
              awk -F '|' '{print $1}' "$fname" > "$fname".new
              done


              Having new output in .new might be desirable for backup. Otherwise, we can use sed -i to perform in-file replacement. Run it without -i first for test trial



              # use file*.txt if they're all in the current directory
              sed -i 's/|.*$//' file1.txt file2.txt file3.txt
              sed -i 's/(^.*)|.*/1/g' file1.txt file2.txt file3.txt


              Another option is via Python:



              #!/usr/bin/env python3
              import sys

              for fname in sys.argv:
              with open(fname) as fd_read, open(fname+'.new','w') as fd_write:
              for line in fd_read:
              fd_write.write(line.split('|')[0] + 'n')


              This script is intended to be used as ./script.py file1.txt file2.txt file3.txt and will write output to new files with .new extension






              share|improve this answer




























                2












                2








                2







                With awk you can just use | as column separator and print the first column:



                awk -F '|' '{print $1}' file1.txt file2.txt file3.txt


                output will be concatenaded. If it's necessary to keep output in separate files, consider using a for loop in shell around awk



                # assuming they're all in the same directory,  hence `*`
                for fname in ./file*.txt ; do
                # add extension to current file in "$fname" variable indicate new file
                # > does the actual redirection
                awk -F '|' '{print $1}' "$fname" > "$fname".new
                done


                Having new output in .new might be desirable for backup. Otherwise, we can use sed -i to perform in-file replacement. Run it without -i first for test trial



                # use file*.txt if they're all in the current directory
                sed -i 's/|.*$//' file1.txt file2.txt file3.txt
                sed -i 's/(^.*)|.*/1/g' file1.txt file2.txt file3.txt


                Another option is via Python:



                #!/usr/bin/env python3
                import sys

                for fname in sys.argv:
                with open(fname) as fd_read, open(fname+'.new','w') as fd_write:
                for line in fd_read:
                fd_write.write(line.split('|')[0] + 'n')


                This script is intended to be used as ./script.py file1.txt file2.txt file3.txt and will write output to new files with .new extension






                share|improve this answer















                With awk you can just use | as column separator and print the first column:



                awk -F '|' '{print $1}' file1.txt file2.txt file3.txt


                output will be concatenaded. If it's necessary to keep output in separate files, consider using a for loop in shell around awk



                # assuming they're all in the same directory,  hence `*`
                for fname in ./file*.txt ; do
                # add extension to current file in "$fname" variable indicate new file
                # > does the actual redirection
                awk -F '|' '{print $1}' "$fname" > "$fname".new
                done


                Having new output in .new might be desirable for backup. Otherwise, we can use sed -i to perform in-file replacement. Run it without -i first for test trial



                # use file*.txt if they're all in the current directory
                sed -i 's/|.*$//' file1.txt file2.txt file3.txt
                sed -i 's/(^.*)|.*/1/g' file1.txt file2.txt file3.txt


                Another option is via Python:



                #!/usr/bin/env python3
                import sys

                for fname in sys.argv:
                with open(fname) as fd_read, open(fname+'.new','w') as fd_write:
                for line in fd_read:
                fd_write.write(line.split('|')[0] + 'n')


                This script is intended to be used as ./script.py file1.txt file2.txt file3.txt and will write output to new files with .new extension







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 4 hours ago

























                answered 4 hours ago









                Sergiy KolodyazhnyySergiy Kolodyazhnyy

                73.4k9153318




                73.4k9153318






















                    Arslan Tariq is a new contributor. Be nice, and check out our Code of Conduct.










                    draft saved

                    draft discarded


















                    Arslan Tariq is a new contributor. Be nice, and check out our Code of Conduct.













                    Arslan Tariq is a new contributor. Be nice, and check out our Code of Conduct.












                    Arslan Tariq is a new contributor. Be nice, and check out our Code of Conduct.
















                    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%2f1122637%2fhow-to-replace-the-content-to-multiple-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

                    is 'sed' thread safeWhat should someone know about using Python scripts in the shell?Nexenta bash script uses...

                    How do i solve the “ No module named 'mlxtend' ” issue on Jupyter?

                    Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...