1

I have a file

had my.file
    Ha1_00044012__C 1 51035805 10.35639322
    Ha1_00045184__C 1 86676829 10.07929888
    Ha1_00045056__C 1 150178524 19.44076068
    Ha10_00000491__C 10 73473488 10.6038893
    Ha10_00000577__C 10 78976728 12.45451149
    Ha10_00002949__C 10 113658711 14.94370396
    Ha10_00001805__C 10 220068670 10.87452401

I want to remove the last 3 characters "__C" from the first column. I tried

awk '{gsub(/[__c ]/,"",$1)}3'

but it removes all "_" in the column one. my desired output should look like this:

head desired
Ha1_00044012 1 51035805 10.35639322
Ha1_00045184 1 86676829 10.07929888
Ha1_00045056 1 150178524 19.44076068
Ha10_00000491 10 73473488 10.6038893
Ha10_00000577 10 78976728 12.45451149
Ha10_00002949 10 113658711 14.94370396
Ha10_00001805 10 220068670 10.87452401
  • You have numerous typos in your question, e.g. had, should there be leading spaces in the input? Please check carefully and edit your question. Also, please show what output you get. – Sparhawk Jul 30 '18 at 23:13
2

If the text is rigid enough that it's merely __C that you want to remove you can do this quite easily using sed too:

$ sed 's/__C//' file
    Ha1_00044012 1 51035805 10.35639322
    Ha1_00045184 1 86676829 10.07929888
    Ha1_00045056 1 150178524 19.44076068
    Ha10_00000491 10 73473488 10.6038893
    Ha10_00000577 10 78976728 12.45451149
    Ha10_00002949 10 113658711 14.94370396
    Ha10_00001805 10 220068670 10.87452401

Further cleanup can be done with sed:

$ sed 's/__C//;s/^[ ]*//g' a

This would remove all the leading spaces starting at the beginning of each line, up to the H in Ha1.

1

The square brackets around [__c ] make it into a set rather than a sequence of characters. With gsub, that's going to make it match (and replace) every _, c (lower case, note) and space character in $1

What you seem to want is to replace a single instance of the sequence __C (upper case) which you can do with sub(/__C/,"",$1)

If you need it to be case insensitive, you can use /__[Cc]/

0

You could do this using the following way:

perl -lane '
  substr($F[0], -3) = ""; # remove the last 3 chars from the 1st field
  print "@F";             # print the fields, space separated
' my.file
0
bash-4.1$ awk -v ncr=3 '{$1=substr($1,0,length($1)-ncr)}1' test.txt
Ha1_00044012 1 51035805 10.35639322
Ha1_00045184 1 86676829 10.07929888
Ha1_00045056 1 150178524 19.44076068
Ha10_00000491 10 73473488 10.6038893
Ha10_00000577 10 78976728 12.45451149
Ha10_00002949 10 113658711 14.94370396
Ha10_00001805 10 220068670 10.87452401

ncr = it's variable. here you can specify how many characters you want to remove from the particular column.

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.