Assign 1,2,3 according to the names using Awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assign 1,2,3 according to the names using Awk
# 1  
Old 06-03-2010
Assign 1,2,3 according to the names using Awk

Print same letters as 1 or 3 (ex:a/a)and different letters as 2 (ex:a/b)
based on Name and subname

1st column indicates main names (ex: ID1 is one main name) and 2nd column indicates sub names (ex: a1 is a subname of ID1) and 3rd column indicates sub-sub names (ex: a/b is a sub-sub name of ID1)

input

Code:
ID1     a1      a/b      
ID1     a10     a/b      
ID1     a2      a/b      
ID1     a3      a/a      
ID1     a6      b/b      
ID2     a1      b/b      
ID2     a10     b/b      
ID2     a2      b/b      
ID2     a3      b/b      
ID2     a6      b/c      
ID2     a8      d/d      
ID3     a1      c/c      
ID3     a10     c/c      
ID3     a2      c/d      
ID3     a3      e/e      
ID3     a6      e/e      
ID3     a8      e/e 

output

Code:
ID1     a1      a/b      2
ID1     a10     a/b      2
ID1     a2      a/b      2
ID1     a3      a/a      1
ID1     a6      b/b      3
ID2     a1      b/b      1
ID2     a10     b/b      1
ID2     a2      b/b      1
ID2     a3      b/b      1
ID2     a6      b/c      2
ID2     a8      d/d      3
ID3     a1      c/c      1
ID3     a10     c/c      1
ID3     a2      c/d      2
ID3     a3      e/e      3
ID3     a6      e/e      3
ID3     a8      e/e      3 

# 2  
Old 06-03-2010
Something like this:

Code:
 
awk 'substr($3,1,1)==substr($3,3,1) { $4=2;print;next} {$4=1;print}'  input_file

Not sure , how you are assigning "3" for the d/d combination.
# 3  
Old 06-03-2010
Ha

It is rather simple. b/b is first similar letter pair for ID2 so it is 1 and d/d is second similar so it is 3. Dissimilar letter should be always 2 (b/c is 2).

I think you misunderstand my question but thanx for your time.

Code:
ID2     a1      b/b      1
ID2     a10     b/b      1
ID2     a2      b/b      1
ID2     a3      b/b      1
ID2     a6      b/c      2
ID2     a8      d/d      3

# 4  
Old 06-04-2010
Something like this:

Code:
 
awk '
  NR==1 {
         if(substr($3,1,1)==substr($3,3,1)) 
         {
         f=$1;co=0;$4=1;c1=substr($3,1,1);print;a[c1]=1;next
         }
         else 
         {
         f=$1;co=0;$4=2;c1=substr($3,1,1);print;next
         }
        }
        { 
          if(f==$1)
        {
          if(substr($3,1,1)==substr($3,3,1))
          {
          f=$1;c1=substr($3,1,1); if(co==0) {$4=1;print;a[c1]=1;co++;next}; for(i in a) { if(i==c1&&co>0){$4=a[c1];print;next}}; $4=3;a[c1]=3;print
          }
          else
          {
          f=$1;$4=2;print;next
          };
   
        }
        else
        {
          co=0;
   for(i in a) 
          {
          a[i]=0
          }
          if(substr($3,1,1)==substr($3,3,1))
          {
          f=$1;c1=substr($3,1,1); if(co==0) {$4=1;print;a[c1]=1;co++;next}; for(i in a) {if(i==c1&&co>0) {$4=a[c1];print;next}}; $4=3;a[c1]=3;print
          }
          else
          {
          f=$1;$4=2;print;next
          };
   }
       }
' input_file

hoooop!!!tried for my best!!!
This User Gave Thanks to panyam For This Post:
# 5  
Old 06-06-2010
hehe

Thanx. But It's working great.
# 6  
Old 06-06-2010
Shorter Smilie
Code:
awk '{i=$NF;split(i,a,"/");if(a[1]!=a[2]){l=2}else{if(!b[$1]){b[$1]=1;c[$1i]=i};l=((c[$1i]==i)?1:3)}{$0=$0l}}1' file

ID1     a1      a/b      2
ID1     a10     a/b      2
ID1     a2      a/b      2
ID1     a3      a/a      1
ID1     a6      b/b      3
ID2     a1      b/b      1
ID2     a10     b/b      1
ID2     a2      b/b      1
ID2     a3      b/b      1
ID2     a6      b/c      2
ID2     a8      d/d      3
ID3     a1      c/c      1
ID3     a10     c/c      1
ID3     a2      c/d      2
ID3     a3      e/e      3
ID3     a6      e/e      3
ID3     a8      e/e      3


Last edited by danmero; 06-07-2010 at 11:19 AM.. Reason: remove comments
This User Gave Thanks to danmero For This Post:
# 7  
Old 06-07-2010
short awk hails :)

thanx. working great
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to assign a value to a variable in awk scripting?

Hi, I am trying to assign a value using below command and it is assigning the command to the variable not the output of the command? out_value="echo $0 | cut -c 9-11"; How can i assign the output to the variable instead of whole command? This is inside my awk script (7 Replies)
Discussion started by: bhagya123
7 Replies

2. Shell Programming and Scripting

Assign a variable with awk

I launch 'netstat -a', if string 'ESTABLISHED' found, then VAR=1 #!/bin/bash VAR=0; netstat -a | awk '$6 ~ /ESTABLISHED/ {VAR=1}' I cannot find the right syntax. thanx guys! (3 Replies)
Discussion started by: arpagon
3 Replies

3. Shell Programming and Scripting

how to assign file names to array variable?

I wish to assign file names with particular extention to array variables. For example if there are 5 files with .dat extention in /home/sam then i have to assign these 5 files to an array. plz help me how to accomplish this. Thanks in advance. (4 Replies)
Discussion started by: siteregsam
4 Replies

4. Shell Programming and Scripting

help on awk---- need to assign the output of awk to a variable

hi i want to find the size of a folder and assign it to a variable and then compare if it is greater than 1 gb. i am doin this script, but it is throwing error.... #!/bin/ksh cd . | du -s | size = awk '{print $1}' if size >= 112000 then echo size high fi ERROR : (4 Replies)
Discussion started by: Nithz
4 Replies

5. Shell Programming and Scripting

Read variables names from array and assign the values

Hi, I have requirement to assign values to variables which are created dynamically. Below is the code which i am using to achieve above requirement. #!/bin/ksh oIFS="$IFS"; IFS=',' STR_FAIL_PARENT_IF_FAILS="WF_F_P_IF_FAILS1,WF_F_P_IF_FAILS2,WF_F_P_IF_FAILS3" set -A... (1 Reply)
Discussion started by: tmalik79
1 Replies

6. Shell Programming and Scripting

awk: assign variable with -v didn't work in awk filter

I want to filter 2nd column = 2 using awk $ cat t 1 2 2 4 $ VAR=2 #variable worked in print $ cat t | awk -v ID=$VAR ' { print ID}' 2 2 # but variable didn't work in awk filter $ cat t | awk -v ID=$VAR '$2~/ID/ { print $0}' (2 Replies)
Discussion started by: honglus
2 Replies

7. Shell Programming and Scripting

Assign o/p of awk to a variable

:confused: Hi UNIX gurus, I am facing a typical problem while assigining while assigining output of awk to a variable. I have a fixed length file say myinputfile.txt When I allow the value/output of an awk to be redirected to a file, it works fine. i.e. awk "/^.{232}$acctNum/ {... (8 Replies)
Discussion started by: c2b2
8 Replies

8. Shell Programming and Scripting

assign awk array with printf

I am trying to assign a awk array for further processing later in the script. I can't seem to figure it out. If someone could look at this and help me, I would very much appreciate it. Thanks in Advance. for ( x = 1 ; x <= Var ; x++ ) { if ( x in varr ) { ... (2 Replies)
Discussion started by: timj123
2 Replies

9. Shell Programming and Scripting

assign values from awk output - help

Dear All, I have a command which gives the number of fields of each line of a comma-separated file. sthng like this : cat QDB_20071126_002.bad | awk -F"," '{ print NF }' I need to assign the first output and the last output of the above command to variables in a script. Need help to do... (4 Replies)
Discussion started by: KrishnaSaran
4 Replies

10. Shell Programming and Scripting

assign value to variable using AWK

Dear Friends I have text file as like below, AAAAA|BHBHBH|VERYSMART AAAAA| KKKKKK|GOOD BBBBBB|JJJJJJJ|VERYGOOD CCCCC|HJHJHJ|BETTER CCCCC|UUUUU|GOOD i need to split into seperate files based on column 1 like as below AAAAA.TXT contains -------------------- BHBHBH.VERYSMART... (4 Replies)
Discussion started by: HAA
4 Replies
Login or Register to Ask a Question