Assigning a number to a letter without printing it


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Assigning a number to a letter without printing it
# 1  
Old 07-03-2012
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

Smilie
# 2  
Old 07-03-2012
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

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 07-03-2012
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

This User Gave Thanks to elixir_sinari For This Post:
# 4  
Old 07-03-2012
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  
Old 07-03-2012
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

This User Gave Thanks to itkamaraj For This Post:
# 6  
Old 07-03-2012
My point is that we cannot use something like:

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

# 7  
Old 07-03-2012
Code:
awk 'BEGIN{OFS=FS="|"} {$3=($1=="X"?3:2)*($2=="X"?3:2)}1' inputfile

This User Gave Thanks to elixir_sinari For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning any number to the variable in cshell script

Hello Guys, I would like to ask you for a favor. Could you please help me how can I assign any number as the parameter to a, from stdin (-c), in the following command line by using the 'switch' in a script? awk '$8>a {print "File name:" $5,$8}' I would also appreciate if you can share any... (1 Reply)
Discussion started by: Padavan
1 Replies

2. Shell Programming and Scripting

Replace specific letter in a file by other letter

Good afternoon all, I want to ask how to change some letter in my file with other letter in spesific line eg. data.txt 1 1 1 0 0 0 0 for example i want to change the 4th line with character 1. How could I do it by SED or AWK. I have tried to run this code but actually did not... (3 Replies)
Discussion started by: weslyarfan
3 Replies

3. Shell Programming and Scripting

Getting phone number, its message and assigning them into 2 variables then screen output.

Hi Everyone, I have a flatfile "inbox.txt" which contains some information: Location 0, folder "Inbox", SIM memory, Inbox folder SMS message SMSC number : "+24800000023" Sent : Sat 04 Aug 2012 09:01:00 PM +0700 Coding : Default GSM alphabet... (5 Replies)
Discussion started by: testcase
5 Replies

4. Shell Programming and Scripting

Printing Number of Fields with the line number

Hi, How to print the number of fields in each record with the line number? Lets saw I have 3212|shipped|received| 3213|shipped|undelivered| 3214|shipped|received|delivered I tried the code awk -F '|' '{print NF}' This gives me ouput as 3 3 4 (5 Replies)
Discussion started by: machomaddy
5 Replies

5. Shell Programming and Scripting

delete all characters that aren't a letter or number

hey :) if i have a variable that is example=lewisdenny(copywrite symbol) so its not a nomal letter but a symbol, how can i remove everything in the varible that isnt letter or number thanks :) and as a side little question do you know how to remove .zip from a file like if i ls... (7 Replies)
Discussion started by: lewisdenny
7 Replies

6. Shell Programming and Scripting

Get letter from number and assign to a variable

Hi to all in forum, I'm trying to convert the letter number between 1 (A) and 26 (Z), that part is working, my issue is how to assign the printf output to a variable:LetterNumber=10 printf "\x$(printf %x $((${LetterNumber}+64)))" $ J #The problem, how to assign printf output (J in this... (8 Replies)
Discussion started by: Ophiuchus
8 Replies

7. Shell Programming and Scripting

Grabbing and printing only first number

I'm having quite a time figuring this one out. I want to print only the first number (n digits). So with this input: Title 1 2* the thing of it 3 more words 4 some more *5 what? 6 more and more *7 I love it 8* boom! 9 it's *8 still here 10 22 23 1 2134 4 2343 32 23 4 44 ... (7 Replies)
Discussion started by: jjgarcia310
7 Replies

8. Shell Programming and Scripting

Assigning number of days in the month to a variable

I am writing a script that requires the number of days in any given month. In the shell, I can use the command: cal `date +%m` `date +%Y`| grep -v '' | wc -w to give me the number of days in the month, but when I assign it to a variable: VAR=`cal `date +%m` `date +%Y`| grep -v '' | wc... (3 Replies)
Discussion started by: skaptakalian
3 Replies

9. Shell Programming and Scripting

regarding about printing line number

Hello, I am testing some data to get line number at cursor position 9 and found some problem, the code is below.Assume we got 3 attribute. At second attribute, there are some data(eg.A41/A6) missing like at the fourth and six line 11006 A41 1888 11006 ... (7 Replies)
Discussion started by: davidkhan
7 Replies

10. UNIX for Dummies Questions & Answers

number of occurence using grep -c then assigning it to a variable

Hi guys! I need to count the occurence of a certain pattern. For example the pattern is PC. the contents of the file sample.txt: A PC asdfgadfjkl asdfa PC sadfaf fdsPCasdfg if i use grep -c PC sample.txt it will display 3 as the number of occurence how do i save that number to a... (1 Reply)
Discussion started by: khestoi
1 Replies
Login or Register to Ask a Question