0

I have the following informations:

'>16_R_I_1_0 M01230:42:000000000-AWMRD:1:1101:15012:1778 1:N:0:0 orig_bc=CGGAGCTATGGT new_bc=CGGAGCTATGGT bc_diffs=0  
'>0_V_I_2_2 M01230:42:000000000-AWMRD:1:1101:16237:1790 1:N:0:0 orig_bc=CTAGCGAACATC new_bc=CTAGCGAACATC bc_diffs=0

I want to remove the "_" only from the first column. I used the command like this: sed "$1=/s/\_//g", but it is not working. I want the output like this:

'>16RI10 M01230:42:000000000-AWMRD:1:1101:15012:1778 1:N:0:0 orig_bc=CGGAGCTATGGT new_bc=CGGAGCTATGGT bc_diffs=0  
'>0VI22 M01230:42:000000000-AWMRD:1:1101:16237:1790 1:N:0:0 orig_bc=CTAGCGAACATC new_bc=CTAGCGAACATC bc_diffs=0

any suggestion please

3
awk '{ gsub("_", "", $1); print }'

Explanation:

awk '{                         for each line
      gsub(                    globally replace
           "_", ""             underscore by the empty string
           $1);                in the first column
      print                    and print the modified line
     }'
  • thanks for your nice suggestion and breakdown of the command. – Masum Billah May 12 '17 at 5:28
3
sed -e ':a;s/^\( *[^ _]*\)_/\1/;ta' data.genefile

Will remove all _ from the first field only in the input genefile.


Perl

perl -F'(\s+)' -lne 'print map { /\S/ && !$a++ && s/_//g; $_ } @F' datagene.txt

Explanation

  • -F'(\s+)' => FS = one or more whitespace(s). The brackets modify the behavior in that now the fields array @F comprises the intervening whitespace which in the normal case is lost. We do this to maintain the whitesoace before and after the change.
  • map { ... } command operates on the fields array @F elements one-by-one and the boolean logic is put in place to ensure that only the first nonempty field is operated upon.
1
sed 'h;s/ .*//;s/_//g;G;s/\n[^ ]*//'

Explanation: The h copies the line in the hold space, the first s removes everything but the first field, the second does the desired replacement. Then you append the hold space with G and remove the newline with the first field. Done.

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.