Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


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 !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-03-2012
Registered User
 
Join Date: Apr 2012
Posts: 58
Thanks: 30
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

Sponsored Links
    #2  
Old 07-03-2012
itkamaraj's Avatar
^Kamaraj^
 
Join Date: Apr 2010
Posts: 3,025
Thanks: 33
Thanked 647 Times in 625 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)
Sponsored Links
    #3  
Old 07-03-2012
elixir_sinari's Avatar
Gotham Knight
 
Join Date: Mar 2012
Location: India
Posts: 1,372
Thanks: 87
Thanked 478 Times in 458 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  
Old 07-03-2012
Registered User
 
Join Date: Apr 2012
Posts: 58
Thanks: 30
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>
...

Sponsored Links
    #5  
Old 07-03-2012
itkamaraj's Avatar
^Kamaraj^
 
Join Date: Apr 2010
Posts: 3,025
Thanks: 33
Thanked 647 Times in 625 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)
Sponsored Links
    #6  
Old 07-03-2012
Registered User
 
Join Date: Apr 2012
Posts: 58
Thanks: 30
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}
...

Sponsored Links
    #7  
Old 07-03-2012
elixir_sinari's Avatar
Gotham Knight
 
Join Date: Mar 2012
Location: India
Posts: 1,372
Thanks: 87
Thanked 478 Times in 458 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)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Printing Number of Fields with the line number machomaddy Shell Programming and Scripting 5 05-23-2012 06:31 AM
delete all characters that aren't a letter or number lewisdenny Shell Programming and Scripting 7 01-21-2012 10:41 AM
Get letter from number and assign to a variable Ophiuchus Shell Programming and Scripting 8 10-18-2011 06:05 PM
Assigning number of days in the month to a variable skaptakalian Shell Programming and Scripting 3 02-05-2010 12:10 AM
number of occurence using grep -c then assigning it to a variable khestoi UNIX for Dummies Questions & Answers 1 02-23-2009 02:59 PM



All times are GMT -4. The time now is 06:10 AM.