3

I have a file like this with tab separated two columns:

 ENSG00000242268.2    0.07563
 ENSG00000270112.3    0.09976
 ENSG00000167578.15   4.38608
 ENSG00000273842.1    0.0
 ENSG00000078237.5    4.08856

I would like to remove the numeric extensions from the end in the 1st column, so the output will be:

 ENSG00000242268    0.07563
 ENSG00000270112    0.09976
 ENSG00000167578    4.38608
 ENSG00000273842    0.0
 ENSG00000078237    4.08856

Simply doing sed 's/\..*$//' returns only first column value and using awk with field separator '.' , awk -F'.' removes the values from second column too as there are decimal numbers.

A similar question has been answered here: removing extensions in a column

I am still not being able to delete just from column 1st only.

  • Does every line start with a space? – John Goofy Aug 25 '17 at 20:08
  • 1
    With TAB between columns use - sed 's/(.[0-9]\+)\t/\t/' file – Egor Vasilyev Aug 25 '17 at 20:08
5

awk solution:

awk -F'\t' '{sub(/\..+$/,"",$1)}1' OFS='\t' file
  • -F'\t' - field separator

  • sub(/\..+$/,"",$1) - removes . with following chars from the 1st field at once

The output:

ENSG00000242268 0.07563
ENSG00000270112 0.09976
ENSG00000167578 4.38608
ENSG00000273842 0.0
ENSG00000078237 4.08856

Or with simple sed approach:

sed 's/\.[0-9]*//' file
5

just do:

sed 's/\(.[0-9]\+\) / /' 

it will only match the first decimal part and delete it.

on your sample:

echo "ENSG00000242268.2    0.07563
>  ENSG00000270112.3    0.09976
>  ENSG00000167578.15   4.38608
>  ENSG00000273842.1    0.0
>  ENSG00000078237.5    4.08856" | sed 's/\(.[0-9]\+\) / /'
ENSG00000242268    0.07563
ENSG00000270112    0.09976
ENSG00000167578   4.38608

edit if you have tabs then try this:

sed 's/\(.[0-9]\+\)\( \|\t\)\2/' 
  • This is doing exactly opposite on my data. This is removing the everything after '.' from second column and not on first column. – Roli Aug 25 '17 at 19:51
  • 3
    This command will not work if used TAB between columns – Egor Vasilyev Aug 25 '17 at 19:57
  • 1
    I have edited in my original question, in my file both columns are tab separated – Roli Aug 25 '17 at 19:58
  • yeah that is most likely what is happening. – Rob Aug 25 '17 at 20:00
  • 1
    no sense to put captured group here sed 's/\(.[0-9]\+\) / /' if it's not used – RomanPerekhrest Aug 25 '17 at 20:12
4

We may match a versioned Ensembl human gene "stable" ID with the extended regular expression ENSG[0-9]{11}\.[0-9]+.

Using this with sed:

$ sed -r 's/(ENSG[0-9]{11})\.[0-9]+/\1/' file.in >file.out

This does not rely on the delimiter between columns or where on the line the identifier occurs.

  • Thanks you so much and for the regular expression too. very helpful. – Roli Aug 25 '17 at 23:53
  • @Roli You're welcome. Note too that if you're using the Ensembl Perl API for coding, you do not need to remove the versions from the identifiers. Also, if you publish something with a list of Ensembl stable IDs, it's better to keep the versions (so that a reader knows exactly what gene sequence you looked at). Ether that, or be careful to mention what Ensembl release you used. (And don't forget to cite Ensembl too!) – Kusalananda Aug 26 '17 at 6:01
0
$ awk -F'[\t.]' -v OFS='\t' '{print $1,$3 "." $4}'
ENSG00000242268 0.07563
ENSG00000270112 0.09976
ENSG00000167578 4.38608
ENSG00000273842 0.0
ENSG00000078237 4.08856
0

With Bash using substring removal:

#!/usr/bin/env bash

file='file.txt'

while read -r i; do

  a=$( <<< "${i}" cut -d $'\t' -f 1 )
  a=${a%.*}
  b=$( <<< "${i}" cut -d $'\t' -f 2- )

  printf '%s\t%s\n' "${a}" "${b}"

done < "${file}"
  • Tabs are already the default delimiter for cut. – Kusalananda Aug 26 '17 at 12:11
0

Perl solution:

perl -pe 's/\.\d+//

  • \.\d+ matches the first '.' followed by 1 or more digit.
  • s/pattern// will delete the selected pattern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.