Unconfigured Ad

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

    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
              Advanced Sequencing Platforms Tackle Neuroscience’s Toughest Genomics Problems
              by SEQadmin2



              Genomics studies in neuroscience face a special challenge due to the brain’s complexity and scarcity of samples. Mapping changes in cell type and state using conventional next-generation sequencing methods remains challenging. Advances in technologies like single-cell sequencing, spatial transcriptomics, and long-read sequencing have opened the door to deeper studies of the brain and diseases like Alzheimer’s, amyotrophic lateral sclerosis (ALS), and schizophrenia.
              ...
              07-09-2026, 11:10 AM
            • SEQadmin2
              Cancer Drug Resistance: The Lingering Barrier to Rising Survival
              by SEQadmin2



              Cancer survival rates have significantly increased in the last few decades in the United States, reaching a combined 70% 5-year survival rate by 2021. Behind this number, there are years of research to find new therapies, drug targets, and early detection methods. But there is one core challenge that keeps slowing down these advances, and it’s about drug resistance.

              There is no single reason why many patients don’t respond to treatment as expected. Cancer is...
              07-08-2026, 05:17 AM
            • GATTACAT
              Reply to Nine Things a Sample Prep Scientist Thinks About Before Sequencing
              by GATTACAT
              Love this - good data definitely starts from good input, and poor input can only give relatively poor data. I particularly like the mention of Nanodrop/absorbance based methods for quantification. It's such a toss up if you'll get an accurate reading or what amounts to a randomly generated number, and a lot of library/sequencing related issues can be traced back to poor quant.
              07-01-2026, 11:43 AM

            ad_right_rmr

            Collapse

            News

            Collapse

            Topics Statistics Last Post
            Started by SEQadmin2, 07-13-2026, 10:26 AM
            0 responses
            20 views
            0 reactions
            Last Post SEQadmin2  
            Started by SEQadmin2, 07-09-2026, 10:04 AM
            0 responses
            30 views
            0 reactions
            Last Post SEQadmin2  
            Started by SEQadmin2, 07-08-2026, 10:08 AM
            0 responses
            20 views
            0 reactions
            Last Post SEQadmin2  
            Started by SEQadmin2, 07-07-2026, 11:05 AM
            0 responses
            34 views
            0 reactions
            Last Post SEQadmin2  
            Working...