Seqanswers Leaderboard Ad

Collapse

Announcement

Collapse
No announcement yet.
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Awk command

    Hello,

    I have a file like the following

    chr1 1234
    chr1 2345
    chr2 94837
    chr2 73457

    how can I split this data into two files

    chr1.txt

    chr1 1234
    chr1 2345

    chr2.txt

    chr2 94837
    chr2 73457

    Thanks in advance.

  • #2
    Originally posted by rkk View Post
    Hello,

    I have a file like the following

    chr1 1234
    chr1 2345
    chr2 94837
    chr2 73457

    how can I split this data into two files

    chr1.txt

    chr1 1234
    chr1 2345

    chr2.txt

    chr2 94837
    chr2 73457

    Thanks in advance.
    $ awk '$1 =="chr1"' file > file1
    $ awk '$1 =="chr2"' file > file2

    This in therapy should work..

    Comment


    • #3
      Code:
      awk '{print > $1".txt"}' input

      Comment


      • #4
        A more "universal" way to do it:
        awk '{print > $1 ".txt"}' Input.file.txt

        Comment


        • #5
          Good to learn a easier way to do this.. can you explain a bit how did it work?

          Comment


          • #6
            awk: syntax error at source line 1
            context is
            {print > $1 >>> ".txt" <<<
            awk: illegal statement at source line 1


            I am getting the above error...

            Comment


            • #7
              Originally posted by rkk View Post
              awk: syntax error at source line 1
              context is
              {print > $1 >>> ".txt" <<<
              awk: illegal statement at source line 1


              I am getting the above error...
              make sure you pay attention to sigle quote, double quote, brackets etc. It worked for me.

              Comment


              • #8
                $head -5 test.txt

                1 9992
                1 9992
                1 9993
                1 9994
                1 9994


                $awk '{print > $1 ".txt"}' test.txt

                awk: syntax error at source line 1
                context is
                {print > $1 >>> ".txt" <<<
                awk: illegal statement at source line 1

                This is what I get for my test.txt file

                Comment


                • #9
                  Originally posted by rkk View Post
                  $head -5 test.txt

                  1 9992
                  1 9992
                  1 9993
                  1 9994
                  1 9994


                  $awk '{print > $1 ".txt"}' test.txt

                  awk: syntax error at source line 1
                  context is
                  {print > $1 >>> ".txt" <<<
                  awk: illegal statement at source line 1

                  This is what I get for my test.txt file

                  It worked for me.... not sure why it's not working for you.

                  Comment


                  • #10
                    Originally posted by rkk View Post
                    $head -5 test.txt

                    1 9992
                    1 9992
                    1 9993
                    1 9994
                    1 9994


                    $awk '{print > $1 ".txt"}' test.txt

                    awk: syntax error at source line 1
                    context is
                    {print > $1 >>> ".txt" <<<
                    awk: illegal statement at source line 1

                    This is what I get for my test.txt file
                    Where r u running it on?

                    Are you on linux server or running at your Mac's terminal?

                    Try using nawk or gawk instead of awk.

                    Comment


                    • #11
                      Originally posted by gene_x View Post
                      Good to learn a easier way to do this.. can you explain a bit how did it work?

                      Code:
                      awk '{print > $1".txt"}' input
                      $1 refers to the first column.

                      for each distinct column1,
                      Code:
                      print
                      to another file
                      Code:
                      >
                      with the same column name
                      Code:
                      $1

                      Comment


                      • #12
                        Originally posted by gokhulkrishnakilaru View Post
                        Code:
                        awk '{print > $1".txt"}' input
                        $1 refers to the first column.

                        for each distinct column1,
                        Code:
                        print
                        to another file
                        Code:
                        >
                        with the same column name
                        Code:
                        $1
                        I can understand print to another file with the same column name. What I don't get is where the separation based on first column contents happened..

                        Comment


                        • #13
                          I should use that command in LINUX...

                          Now, I have another issue

                          I have a file like following..I need to bin the first column in 100bp regions and count the second column value for that bin
                          10175 1
                          10179 1
                          10189 1
                          10191 1
                          10201 1
                          10243 1
                          10249 1
                          10262 1
                          10313 1
                          10414 1
                          10485 1
                          10499 1

                          The output should be something like this..

                          10101-10200 4
                          10201-10300 4
                          10301-10400 1
                          10401-10500 3

                          Can someone help with this..

                          Thanks in advance..

                          Comment


                          • #14
                            Originally posted by rkk View Post
                            I should use that command in LINUX...

                            Now, I have another issue

                            I have a file like following..I need to bin the first column in 100bp regions and count the second column value for that bin
                            10175 1
                            10179 1
                            10189 1
                            10191 1
                            10201 1
                            10243 1
                            10249 1
                            10262 1
                            10313 1
                            10414 1
                            10485 1
                            10499 1

                            The output should be something like this..

                            10101-10200 4
                            10201-10300 4
                            10301-10400 1
                            10401-10500 3

                            Can someone help with this..

                            Thanks in advance..
                            Do you already know your bins?

                            If not, what are your start values and end values to consider bins at 100bp?

                            Comment


                            • #15
                              command has to identify min and max value from col1 values.. and then bin that into 100bp regions...

                              Comment

                              Latest Articles

                              Collapse

                              • seqadmin
                                Recent Developments in Metagenomics
                                by seqadmin





                                Metagenomics has improved the way researchers study microorganisms across diverse environments. Historically, studying microorganisms relied on culturing them in the lab, a method that limits the investigation of many species since most are unculturable1. Metagenomics overcomes these issues by allowing the study of microorganisms regardless of their ability to be cultured or the environments they inhabit. Over time, the field has evolved, especially with the advent...
                                09-23-2024, 06:35 AM
                              • seqadmin
                                Understanding Genetic Influence on Infectious Disease
                                by seqadmin




                                During the COVID-19 pandemic, scientists observed that while some individuals experienced severe illness when infected with SARS-CoV-2, others were barely affected. These disparities left researchers and clinicians wondering what causes the wide variations in response to viral infections and what role genetics plays.

                                Jean-Laurent Casanova, M.D., Ph.D., Professor at Rockefeller University, is a leading expert in this crossover between genetics and infectious...
                                09-09-2024, 10:59 AM

                              ad_right_rmr

                              Collapse

                              News

                              Collapse

                              Topics Statistics Last Post
                              Started by seqadmin, 10-02-2024, 04:51 AM
                              0 responses
                              13 views
                              0 likes
                              Last Post seqadmin  
                              Started by seqadmin, 10-01-2024, 07:10 AM
                              0 responses
                              21 views
                              0 likes
                              Last Post seqadmin  
                              Started by seqadmin, 09-30-2024, 08:33 AM
                              0 responses
                              25 views
                              0 likes
                              Last Post seqadmin  
                              Started by seqadmin, 09-26-2024, 12:57 PM
                              0 responses
                              18 views
                              0 likes
                              Last Post seqadmin  
                              Working...
                              X