Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rkk
    Junior Member
    • Feb 2011
    • 9

    #1

    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.
  • gene_x
    Senior Member
    • May 2010
    • 108

    #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

    • gokhulkrishnakilaru
      Member
      • Jul 2011
      • 39

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

      Comment

      • alexdobin
        Senior Member
        • Feb 2009
        • 161

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

        Comment

        • gene_x
          Senior Member
          • May 2010
          • 108

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

          Comment

          • rkk
            Junior Member
            • Feb 2011
            • 9

            #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

            • gene_x
              Senior Member
              • May 2010
              • 108

              #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

              • rkk
                Junior Member
                • Feb 2011
                • 9

                #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

                • gene_x
                  Senior Member
                  • May 2010
                  • 108

                  #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

                  • gokhulkrishnakilaru
                    Member
                    • Jul 2011
                    • 39

                    #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

                    • gokhulkrishnakilaru
                      Member
                      • Jul 2011
                      • 39

                      #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

                      • gene_x
                        Senior Member
                        • May 2010
                        • 108

                        #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

                        • rkk
                          Junior Member
                          • Feb 2011
                          • 9

                          #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

                          • gokhulkrishnakilaru
                            Member
                            • Jul 2011
                            • 39

                            #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

                            • rkk
                              Junior Member
                              • Feb 2011
                              • 9

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

                              Comment

                              Latest Articles

                              Collapse

                              • SEQadmin2
                                New Genomics Technologies Take Aim at Long-Standing Limits
                                by SEQadmin2


                                Researchers using sequencing and genomics tools often have to make trade-offs. They can choose between speed or scale, short reads or long-range information, or targeted panels or a view of the whole transcriptome. New technologies that have been released this year are built to address those tough choices.

                                We asked six companies the same four questions to learn about their latest products. The new technologies bring a lot to the table, including rethinking sequencing
                                ...
                                Today, 10:25 AM
                              • SEQadmin2
                                How Immunogenomics Decodes Immunity’s Genetic Blueprint
                                by SEQadmin2




                                The immune system’s power comes from its genetic diversity, allowing myriad threats to be neutralized through first recognizing foreign antigens. That diversity is also what makes the immune system so difficult to study. Recent advances in sequencing technology and computational biology, however, are giving researchers new tools to understand immune responses and immune-related diseases in greater detail.

                                This convergence of genetics, immunology, and computation...
                                09-01-2026, 05:41 AM

                              ad_right_rmr

                              Collapse

                              News

                              Collapse

                              Topics Statistics Last Post
                              Started by SEQadmin2, 09-25-2026, 09:06 AM
                              0 responses
                              28 views
                              0 reactions
                              Last Post SEQadmin2  
                              Started by SEQadmin2, 09-23-2026, 11:05 AM
                              0 responses
                              24 views
                              0 reactions
                              Last Post SEQadmin2  
                              Started by SEQadmin2, 09-18-2026, 11:37 AM
                              1 response
                              46 views
                              0 reactions
                              Last Post pekgio
                              by pekgio
                               
                              Started by SEQadmin2, 09-16-2026, 10:23 AM
                              1 response
                              55 views
                              0 reactions
                              Last Post pekgio
                              by pekgio
                               
                              Working...