Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Liy
    Member
    • Feb 2012
    • 19

    #1

    bash help

    Hi there,

    I cant figure it out how to solve a problem in unix, so a little help would be appritiated.

    So, I want to delete a line (or swap the two column) only if the column3 is greater then column4. Its a tab delimited bed file. The expected output would be the same as the original file, but swapped coordinates if start is greater then stop. Maybe awk, sed?

    x1 I 139154 139256 y1 +
    x2 I 166268 166340 y2 +
    x3 I 181135 181248 y3 +
    x4 I 182597 182516 y4 +

    Thanks a lot!
    L
  • rhinoceros
    Senior Member
    • Apr 2013
    • 372

    #2
    Code:
    awk '$4>$3{print}' file > output
    Only prints lines where column4 > column3

    Code:
    awk '$3>$4{print $1" "$2" "$4" "$3" "$5}' file > output
    Only print lines where column3 > column4, but print them as "column1<space>column2<space>column4<space>column3<space>column5"
    Last edited by rhinoceros; 01-28-2014, 06:15 AM.
    savetherhino.org

    Comment

    • blakeoft
      Member
      • Oct 2013
      • 79

      #3
      Originally posted by rhinoceros View Post
      Code:
      awk '$4>$3{print}' file > output
      Only prints lines where column4 > column3

      Code:
      awk '$3>$4{print $1" "$2" "$4" "$3" "$5}' file > output
      Only print lines where column3 > column4, but print them as "column1<space>column2<space>column4<space>column3<space>column5"
      I suggest doing both of these except using ">>" instead of ">" when sending to the output file and then sorting the output file if you need to.

      Comment

      • GenoMax
        Senior Member
        • Feb 2008
        • 7142

        #4
        Since Liy wants to keep the file otherwise unchanged, building on rhinoceros's code.
        Code:
        BEGIN   { RS = "\n"}{
                if ($4 > $3) {
                print $0
                }
                else    {
                 #OFS = "\t" (uncomment if you need the output to be tab separated, your example above seems to have spaces)
                print $1, $2, $4, $3, $5, $6
                }
        }
        Code:
        $ cat file | awk -f code_above.awk > save_to_a_new_file
        Last edited by GenoMax; 01-28-2014, 07:02 AM. Reason: Clarification about output

        Comment

        • blakeoft
          Member
          • Oct 2013
          • 79

          #5
          You could also try:
          Code:
          awk '{if ($3>$4) {print $1,$2,$4,$3,$5,$6} else {print}}' file.bed > new_file.bed

          Comment

          • Liy
            Member
            • Feb 2012
            • 19

            #6
            Thank you very much, all of you!

            Comment

            Latest Articles

            Collapse

            • SEQadmin2
              Multiomic Liquid Biopsy: What DNA/RNA Panels Reveal That DNA Alone Can’t
              by SEQadmin2



              Tumors contain complex mixtures of cell populations that evolve in response to treatment and host-specific factors. Tissue biopsies provide only static snapshots of a tumor’s state. But what if we could follow that evolution over time using a minimally invasive technique?

              Liquid biopsies can detect tumor-derived material circulating in the bloodstream, providing a way to study treatment response, detect emerging resistance, and discover new biomarkers. Circulating
              ...
              10-14-2026, 09:55 AM
            • 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
              ...
              09-28-2026, 10:25 AM

            ad_right_rmr

            Collapse

            News

            Collapse

            Topics Statistics Last Post
            Started by SEQadmin2, Yesterday, 11:13 AM
            0 responses
            19 views
            0 reactions
            Last Post SEQadmin2  
            Started by SEQadmin2, Yesterday, 10:48 AM
            0 responses
            18 views
            0 reactions
            Last Post SEQadmin2  
            Started by SEQadmin2, 10-15-2026, 10:52 AM
            0 responses
            36 views
            0 reactions
            Last Post SEQadmin2  
            Started by SEQadmin2, 10-14-2026, 05:39 AM
            0 responses
            42 views
            0 reactions
            Last Post SEQadmin2  
            Working...