2

I have a file that looks like so:

chr22   43089055    43089055    -   NM_017436   C   300 903delC
chr22   43089715-43089717   43089715-43089717   -   NM_017436   CTT 79

I want to remove all the characters before the - in column 3 to give me an output as depicted below:

chr22   43089055    43089055    -   NM_017436   C   300 903delC
chr22   43089715-43089717   43089717    -   NM_017436   CTT 79

I've used awk '{$2+=0}1' file in the past to remove characters after the -, but I don't believe I can use this same technique for my current problem. Any suggestions?

5

Perl to the rescue:

perl -lane 'BEGIN { $, = "\t" } $F[2] =~ s/.*-//; print @F' < file
  • -l appends newlines to print
  • -n reads the input line by line
  • -a splits each line on whitespace and populates the @F array
  • $, separates list members when printed, set it to tab
  • s/.*-// substitutes everything up to a dash with nothing, it's bound to the third column (arrays are indexed from 0)
3

As some different variants

  1. awk

    awk '{$3=A[split($3,A,"-")]}1' file

  2. sed

    sed -r 's/((\S+\s+){2})[^- ]+-/\1/' file

0

Using awk:

< input awk 'BEGIN {FS=OFS="   "} {gsub(/[^-]*-/, "", $3); print}'

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.