The UNIX and Linux Forums Assigning a number to a letter without printing it
 Member Name Remember Me? Password

 UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

# Assigning a number to a letter without printing it

## UNIX for Dummies Questions & Answers

#1
07-03-2012
 beca123456 Registered User Join Date: Apr 2012 Last Activity: 8 June 2013, 4:52 AM EDT Posts: 65 Thanks: 32 Thanked 0 Times in 0 Posts
Assigning a number to a letter without printing it

Hello Unix.com !

Newbie question.
With awk, i can we give a particular letter a number without printing the number?

input:

Code:
```X|X
A|X
A|A
X|A```

If field is "A", then it counts as 2
If field is "X", then it counts as 3
Multiply the 2 first field and give the result in the 3th field, but keep the original letters in the 2 first fields

output:

Code:
```X|X|9
A|X|6
A|A|4
X|A|6```

I tried this command below, but it replaces the letter by the numbers in the 2 first fields:

Code:
```BEGIN{FS=OFS="|"}
{for (i=1; i<=NF; i++)
if(\$i ~ /A/){
\$i = "2"}
else
\$i = "3"

\$3 = \$1 * \$2
}1```

like that:

Code:
```3|3|9
2|3|6
2|2|4
3|2|6```

#2
07-03-2012
 itkamaraj   ^Kamaraj^ Join Date: Apr 2010 Last Activity: 18 November 2013, 8:32 AM EST Posts: 3,039 Thanks: 33 Thanked 652 Times in 631 Posts

Code:
```\$ nawk -F\| -v OFS=\| 'BEGIN{a["X"]=3;a["A"]=2}{x=1;for(i=1;i<=NF;i++){x*=a[\$i]}\$NF=\$NF FS x}1' input.txt
X|X|X|27
A|X|X|18
A|A|X|12
A|A|A|8
X|A|X|18```

 The Following User Says Thank You to itkamaraj For This Useful Post: beca123456 (07-03-2012)
#3
07-03-2012
 elixir_sinari   Gotham Knight Join Date: Mar 2012 Last Activity: 9 December 2013, 11:36 AM EST Location: India Posts: 1,411 Thanks: 99 Thanked 495 Times in 472 Posts
That's because you are changing the fields in the loop instead of using values associated with those fields. Try this instead:

Code:
`awk 'BEGIN{OFS=FS="|";val["A"]=2;val["X"]=3} {\$3=val[\$1]*val[\$3]}1' inputfile`

 The Following User Says Thank You to elixir_sinari For This Useful Post: beca123456 (07-03-2012)
#4
07-03-2012
 beca123456 Registered User Join Date: Apr 2012 Last Activity: 8 June 2013, 4:52 AM EDT Posts: 65 Thanks: 32 Thanked 0 Times in 0 Posts
Thanks itkamaraj and elixir_sinari for your quick replies !

I'm just curious, but what if for example we have also some fields with different letters (or even strings) and we want to assign them specific values.

e.g.

Code:
```X|X|X
A|B|X
A|C|X
A|B|C
X|B|C```

with this conversion:
A = B = C = 2
X = 3
(it will not change the results)

Would not be easier to use a statement like:

Code:
```for (i=1; i<=NF; i++)
if (\$i = "X"){
<then assign X the value 3>
else
< assign the values different of X the value 2>
...```

#5
07-03-2012
 itkamaraj   ^Kamaraj^ Join Date: Apr 2010 Last Activity: 18 November 2013, 8:32 AM EST Posts: 3,039 Thanks: 33 Thanked 652 Times in 631 Posts

Code:
`awk -F\| -v OFS=\| '{x=1;for(i=1;i<=NF;i++)if(\$i=="X"){x*=3}else{x*=2}\$NF=\$NF FS x}1' input.txt`

 The Following User Says Thank You to itkamaraj For This Useful Post: beca123456 (07-03-2012)
#6
07-03-2012
 beca123456 Registered User Join Date: Apr 2012 Last Activity: 8 June 2013, 4:52 AM EDT Posts: 65 Thanks: 32 Thanked 0 Times in 0 Posts
My point is that we cannot use something like:

Code:
```BEGIN{OFS=FS="|";val["X"]=3;val[^"X"]=2}
...```

#7
07-03-2012
 elixir_sinari   Gotham Knight Join Date: Mar 2012 Last Activity: 9 December 2013, 11:36 AM EST Location: India Posts: 1,411 Thanks: 99 Thanked 495 Times in 472 Posts

Code:
`awk 'BEGIN{OFS=FS="|"} {\$3=(\$1=="X"?3:2)*(\$2=="X"?3:2)}1' inputfile`

 The Following User Says Thank You to elixir_sinari For This Useful Post: beca123456 (07-03-2012)