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

How do I add a variable to this curl command?

Everyone is beautiful

Making an empty box next to text

Can I retract my name from an already published manuscript?

Auto Insert date into Notepad

Why do neural networks need so many training examples to perform?

Linux File Manager: Restore previous open session (folders and tab)

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

On what did Lego base the appearance of the new Hogwarts minifigs?

Do authors have to be politically correct in article-writing?

Is it a fallacy if someone claims they need an explanation for every word of your argument to the point where they don't understand common terms?

How to prepare vegetables for a sandwich that can last for several days in a fridge?

What is Crew Dragon approaching in this picture?

No rhyme nor reason

What is the wife of a henpecked husband called?

I am on the US no-fly list. What can I do in order to be allowed on flights which go through US airspace?

Why zero tolerance on nudity in space?

Can the SpaceX Dragon 2 crew vehicle still use the draco and super draco thrusters to slow down when landing?

Connecting top and bottom of adjacent circles

Am I using the wrong word all along?

Word to be used for "standing with your toes pointing out"

How to approximate rolls for potions of healing using only d6's?

Activating a Alphanet Faucet Wallet Remotely (without tezos-client)

raspberry pi change directory (cd) command not working with USB drive



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 contentJoin two files keeping the element order of one of themOnly 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?Replace all lines between two patterns (inclusive) with a file content













4















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 two 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.

























    4















    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 two 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.























      4












      4








      4








      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 two 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 two 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 12 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 17 hours ago









      Arslan TariqArslan Tariq

      212




      212




      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.






















          2 Answers
          2






          active

          oldest

          votes


















          7














          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





















          • 1





            cut -d| -f1 may be an alternative to awk -F| '{print $1}'

            – jno
            12 hours ago











          • @jno Yes, that's an option as well. Feel free to post that as your own answer

            – Sergiy Kolodyazhnyy
            12 hours ago



















          3














          For cutting out some text, I always think of cut first, separating by | as in jno's comment:



          cut -d| -f1 file


          Or it looks like the format is always 8 characters, then 2 spaces, then 5 more characters, so you could just cut the first 15 characters with



          cut -c 1-15 file


          Or you could separate fields by spaces, but since there's two spaces between the first & second fields that means you'd cut out fields 1, 2, and 3:



          cut -d" " -f 1-3 file




          Cut doesn't have in-place file editing like sed, but you can output to a new file, then move over the original file, for example



          for file in {file1,file2,file3}
          do
          cut -d" " -f 1-3 "$file" > "$file.2"
          mv "$file.2" "$file"
          done





          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

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            7














            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





















            • 1





              cut -d| -f1 may be an alternative to awk -F| '{print $1}'

              – jno
              12 hours ago











            • @jno Yes, that's an option as well. Feel free to post that as your own answer

              – Sergiy Kolodyazhnyy
              12 hours ago
















            7














            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





















            • 1





              cut -d| -f1 may be an alternative to awk -F| '{print $1}'

              – jno
              12 hours ago











            • @jno Yes, that's an option as well. Feel free to post that as your own answer

              – Sergiy Kolodyazhnyy
              12 hours ago














            7












            7








            7







            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 16 hours ago

























            answered 17 hours ago









            Sergiy KolodyazhnyySergiy Kolodyazhnyy

            73.5k9153318




            73.5k9153318








            • 1





              cut -d| -f1 may be an alternative to awk -F| '{print $1}'

              – jno
              12 hours ago











            • @jno Yes, that's an option as well. Feel free to post that as your own answer

              – Sergiy Kolodyazhnyy
              12 hours ago














            • 1





              cut -d| -f1 may be an alternative to awk -F| '{print $1}'

              – jno
              12 hours ago











            • @jno Yes, that's an option as well. Feel free to post that as your own answer

              – Sergiy Kolodyazhnyy
              12 hours ago








            1




            1





            cut -d| -f1 may be an alternative to awk -F| '{print $1}'

            – jno
            12 hours ago





            cut -d| -f1 may be an alternative to awk -F| '{print $1}'

            – jno
            12 hours ago













            @jno Yes, that's an option as well. Feel free to post that as your own answer

            – Sergiy Kolodyazhnyy
            12 hours ago





            @jno Yes, that's an option as well. Feel free to post that as your own answer

            – Sergiy Kolodyazhnyy
            12 hours ago













            3














            For cutting out some text, I always think of cut first, separating by | as in jno's comment:



            cut -d| -f1 file


            Or it looks like the format is always 8 characters, then 2 spaces, then 5 more characters, so you could just cut the first 15 characters with



            cut -c 1-15 file


            Or you could separate fields by spaces, but since there's two spaces between the first & second fields that means you'd cut out fields 1, 2, and 3:



            cut -d" " -f 1-3 file




            Cut doesn't have in-place file editing like sed, but you can output to a new file, then move over the original file, for example



            for file in {file1,file2,file3}
            do
            cut -d" " -f 1-3 "$file" > "$file.2"
            mv "$file.2" "$file"
            done





            share|improve this answer






























              3














              For cutting out some text, I always think of cut first, separating by | as in jno's comment:



              cut -d| -f1 file


              Or it looks like the format is always 8 characters, then 2 spaces, then 5 more characters, so you could just cut the first 15 characters with



              cut -c 1-15 file


              Or you could separate fields by spaces, but since there's two spaces between the first & second fields that means you'd cut out fields 1, 2, and 3:



              cut -d" " -f 1-3 file




              Cut doesn't have in-place file editing like sed, but you can output to a new file, then move over the original file, for example



              for file in {file1,file2,file3}
              do
              cut -d" " -f 1-3 "$file" > "$file.2"
              mv "$file.2" "$file"
              done





              share|improve this answer




























                3












                3








                3







                For cutting out some text, I always think of cut first, separating by | as in jno's comment:



                cut -d| -f1 file


                Or it looks like the format is always 8 characters, then 2 spaces, then 5 more characters, so you could just cut the first 15 characters with



                cut -c 1-15 file


                Or you could separate fields by spaces, but since there's two spaces between the first & second fields that means you'd cut out fields 1, 2, and 3:



                cut -d" " -f 1-3 file




                Cut doesn't have in-place file editing like sed, but you can output to a new file, then move over the original file, for example



                for file in {file1,file2,file3}
                do
                cut -d" " -f 1-3 "$file" > "$file.2"
                mv "$file.2" "$file"
                done





                share|improve this answer















                For cutting out some text, I always think of cut first, separating by | as in jno's comment:



                cut -d| -f1 file


                Or it looks like the format is always 8 characters, then 2 spaces, then 5 more characters, so you could just cut the first 15 characters with



                cut -c 1-15 file


                Or you could separate fields by spaces, but since there's two spaces between the first & second fields that means you'd cut out fields 1, 2, and 3:



                cut -d" " -f 1-3 file




                Cut doesn't have in-place file editing like sed, but you can output to a new file, then move over the original file, for example



                for file in {file1,file2,file3}
                do
                cut -d" " -f 1-3 "$file" > "$file.2"
                mv "$file.2" "$file"
                done






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 10 hours ago

























                answered 10 hours ago









                Xen2050Xen2050

                6,87612343




                6,87612343






















                    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

                    As a Security Precaution, the user account has been locked The Next CEO of Stack OverflowMS...

                    Список ссавців Італії Природоохоронні статуси | Список |...

                    Українські прізвища Зміст Історичні відомості |...