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
              16S/ITS Profiling vs. Shotgun Metagenomics: What Changes in Your Workflow?
              by SEQadmin2





              NGS has revolutionized clinical microbiology by offering new ways to identify and surveil pathogens without prior knowledge of the target. One popular approach is 16S rRNA and ITS amplicon sequencing, which targets specific microbial markers. But studies are increasingly incorporating shotgun metagenomics, a technique that sequences all DNA from a sample without targeting specific microbial markers. This approach allows more detailed taxonomic classification,...
              10-30-2026, 06:46 AM
            • 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

            ad_right_rmr

            Collapse

            News

            Collapse

            Topics Statistics Last Post
            Started by SEQadmin2, Yesterday, 07:59 AM
            0 responses
            15 views
            0 reactions
            Last Post SEQadmin2  
            Started by SEQadmin2, 11-05-2026, 09:25 AM
            0 responses
            32 views
            0 reactions
            Last Post SEQadmin2  
            Started by SEQadmin2, 11-03-2026, 11:54 AM
            0 responses
            46 views
            0 reactions
            Last Post SEQadmin2  
            Started by SEQadmin2, 10-28-2026, 09:57 AM
            0 responses
            172 views
            0 reactions
            Last Post SEQadmin2  
            Working...