-1

I would like to delete all entries with (L=6), (L=7).....(L=12) occurring in a file randomly the following format;

my input file:

TRINITY_DN1910_c0_g1_i13    GO:0005975(L=2) GO:0006022(L=4) GO:0006026(L=5) GO:0006030(L=9) GO:0006032(L=11)    GO:0006040(L=6)
TRINITY_DN1452_c0_g1_i11    GO:0005975(L=3) GO:0006022(L=9) GO:0006026(L=12)    GO:0006030(L=2) GO:0006032(L=4) GO:0006040(L=5)

                                        

The file is tab delimited.

Expected output

TRINITY_DN1910_c0_g1_i13    GO:0005975(L=2) GO:0006022(L=4) GO:0006026(L=5)
TRINITY_DN1452_c0_g1_i11    GO:0005975(L=3) GO:0006030(L=2) GO:0006032(L=4) GO:0006040(L=5) 
5
  • 2
    Please edit your question to show us what you've tried, where you are stuck, and what your question is.
    – Jim L.
    Dec 21, 2021 at 23:19
  • Wll the entries you want to delete always be contiguous numbers like 6, 7, 8, 9, 10, 11, 12 or could there be holes like 6, 8, 11, 12?
    – Ed Morton
    Dec 21, 2021 at 23:41
  • I want to delete all the GO entries with (L=[>5]) retain everything else Dec 21, 2021 at 23:51
  • 2
    You should have said THAT in your question instead of I would like to delete all entries with (L=6), (L=7).....(L=12). The end result might be the same for your current specific data but deleting everything >5 is a different problem from deleting within a specific range.
    – Ed Morton
    Dec 21, 2021 at 23:55
  • Don't change it now though as you already have answers to the question you asked.
    – Ed Morton
    Dec 22, 2021 at 0:02

6 Answers 6

2

You can do this by using sed as follows:

sed 's/GO:[^   ]*(L=[6-9])//g
     s/GO:[^ ]*(L=1[0-2])//g' filename

You can enter the tab character by pressing <ctrl-v><tab>. That is what I have in square brackets starting with ^.

3
  • Thanks @Ed Morton, It works! I added removal of multiple tabs sed 's/GO:[^\t]*(L=[6-9])//g' test.txt | sed 's/GO:[^\t]*(L=1[0-2])//g' | sed -r 's:\t+:\t:g' >l.txt Dec 22, 2021 at 0:08
  • 2
    @alexkiarie, It is bad practice to call the editor several times in the pipeline: sed 's/\s*\S*(L=\([6-9]\|1[0-2]\))//g' file
    – nezabudka
    Dec 22, 2021 at 0:39
  • thanks for the info. Dec 23, 2021 at 16:08
1

Using any awk in any shell on every Unix box:

$ awk '
    BEGIN {
        for (i=6; i<=12; i++) {
            re = re sep i
            sep = "|"
        }
        re = "\t[^\t]+[(]L=(" re ")[)]"
    }
    {
        gsub(re,"")
        print
    }
' file
TRINITY_DN1910_c0_g1_i13        GO:0005975(L=2) GO:0006022(L=4) GO:0006026(L=5)
TRINITY_DN1452_c0_g1_i11        GO:0005975(L=3) GO:0006030(L=2) GO:0006032(L=4) GO:0006040(L=5)

If you have any other contiguous range of numbers to deal with just change the start/end numbers in the loop. If you have non contiguous numbers just set re in the BEGIN however makes sense for those numbers, e.g. an option might be to replace the loop with re = "7|12|957|1045".

0
awk '{gsub(/\s*\S*L=([6-9]|1[0-9])\)/, "")}1' file
0
0

Setting FS to the what you need and when this match rebuild the whole line :

awk -F'GO:[0-9]*[(]L=([6-9]|1[0-2])[)] *' '{
    s = ""
    for(i = 1; i<= NF; i++) 
        s = s ? s $i : $i
    print s
}' file
0

Using Raku (formerly known as Perl_6)

raku -ne '.=words; print .[0] ~ "\t"; \
      put $_.skip.grep( { / "(L=" (\d+) ")" $ / && $0 < 6 } ).join: "\t";'  

Sample Input (tab-separated columns):

TRINITY_DN1910_c0_g1_i13    GO:0005975(L=2) GO:0006022(L=4) GO:0006026(L=5) GO:0006030(L=9) GO:0006032(L=11)    GO:0006040(L=6)
TRINITY_DN1452_c0_g1_i11    GO:0005975(L=3) GO:0006022(L=9) GO:0006026(L=12)    GO:0006030(L=2) GO:0006032(L=4) GO:0006040(L=5)

Sample Output (tab-separated columns):

TRINITY_DN1910_c0_g1_i13    GO:0005975(L=2) GO:0006022(L=4) GO:0006026(L=5)
TRINITY_DN1452_c0_g1_i11    GO:0005975(L=3) GO:0006030(L=2) GO:0006032(L=4) GO:0006040(L=5)
  • Briefly, lines are read in using the -ne non-autoprinting linewise flags,

  • Input is broken into whitespace-delimited columns using the .=words routine wherein the .= operator saves the resultant elements back into Raku's $_ topic variable ( .=words is shorthand for $_ = $_.words ),

  • In the second statement, the first column is printed, followed by \t tab, and

  • Finally in the third statement, the first element is skipped (since it has already been printed), and grep is used to return elements that first match "(L=" (\d+) ")" $ and then && $0 < 6 have the $0 capture value less than 6.

https://raku.org

-1
sed -e  "s/GO:[0-9]*(L=[6-9])/ /g" -e "s/GO:[0-9]*(L=[0-9]\{2,\})/ /g" FILENAME

OUTPUT

TRINITY_DN1910_c0_g1_i13    GO:0005975(L=2) GO:0006022(L=4) GO:0006026(L=5)         
TRINITY_DN1452_c0_g1_i11    GO:0005975(L=3)        GO:0006030(L=2) GO:0006032(L=4) GO:0006040(L=5)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .