-3

I want to transform the letters in two columns into numbers.

In the following example I want to change the letters in column 2 and 3 for numbers, so that A is changed to 1, B into 2, C into 3 and G into 4.

The first column may also contain the letters A, B, C and G. These should not be changed.

Columns are separated by / (<space><slash><space>).

Id_animal / Column1 / Column2
ID01 / A / B
ID02 / B / A
ID03 / C / A
ID04 / A / G

The wanted output is:

Id_animal / Column1 / Column2
ID01 / 1 / 2
ID02 / 2 / 1
ID03 / 3 / 1
ID04 / 1 / 4
  • Another question: How a repeated all line? I need to repeated all lines twice. Thank you. And today I can't to do other question! – Amanda Botelho Alvarenga Jul 14 '16 at 11:50
  • 3
    Please edit your question with the complete requirement instead of posting follow-up comments. Also, are the blank lines in your input intentional, or just a formatting issue? – steeldriver Jul 14 '16 at 11:53
  • 3
    Are there always only A,B,C and G in your file ? Also, you can definitely post more than one question per day... – don_crissti Jul 14 '16 at 11:54
  • Please take some time to properly format your question, and to provide representative input data. For example, you have rejected a proposed solution because "this case change my animal identification" - where is this "animal identification" and what does it look like? – steeldriver Jul 14 '16 at 13:43
  • I do not know how to improve. I'm sorry. – Amanda Botelho Alvarenga Jul 14 '16 at 14:31
2
  1. Cut the columns into two sets, one with the first column and the other one with column two and three.

  2. Transliterate the characters into digits in the second of these sets.

  3. Paste everything together again.

On the command line:

$ cat data
Id_animal / Column1 / Column2
ID01 / A / B
ID02 / B / A
ID03 / C / A
ID04 / A / G
ABCG / G / G

$ paste -d '/' <(cut -d '/' -f 1 data) <(cut -d '/' -f 2,3 data | tr 'ABCG' '1234')
Id_animal / 3olumn1 / 3olumn2
ID01 / 1 / 2
ID02 / 2 / 1
ID03 / 3 / 1
ID04 / 1 / 4
ABCG / 4 / 4

You get bonus digits in the header... :-)

  • cut -d / -f 2,3 data | sed 1d ... or cut -d / -f 2,3 data | tail -n +2 ... to not translate the header; add in a preceding head -n 1 data to put it back – Jeff Schaller Jul 14 '16 at 18:08
1

Under the assumption that you only want to translate A, B, C, and G to 1, 2, 3, and 4, and that the first column never contains those letters, you could simplify bgStack15's answer to just:

tr 'ABCG' '1234' < input > output
  • I can to specify the column? Because in this case change my animal identification. – Amanda Botelho Alvarenga Jul 14 '16 at 13:01
  • 2
    You should edit your post to clarify what exactly you want to have happen. Which columns and which letters to change to which numbers. – Jeff Schaller Jul 14 '16 at 13:09
0

Adjust the variables as desired. Save this to a file, for example `/home/Amanda/script.sh

#!/bin/sh
infile=~/input.txt
outfile=~/output.txt
while read one two; do
   printf "%s %s\n" "${one}" "$( echo "${two}" | tr 'ABCG' '1234' )"
done < ${infile} > ${outfile}

Be sure to make the script executable: chmod +x /home/Amanda/script.sh

0

With perl you can apply trasnliterations to specific input colums using a map e.g.

perl -F"/" -alne 'print join "/", @F[0], map { tr/ABCG/1234/; $_ } @F[1,2]' input

For example, given input

Id_animal / Column1 / Column2
ID01 / A / B
ID02 / B / A
AG02 / B / A
ID03 / C / A
ID04 / A / G

then

$ perl -F"/" -alne 'print $. == 1? $_ : join "/", @F[0], map { tr/ABCG/1234/; $_ } @F[1,2]' input
Id_animal / Column1 / Column2
ID01 / 1 / 2
ID02 / 2 / 1
AG02 / 2 / 1
ID03 / 3 / 1
ID04 / 1 / 4

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.