awk finding counting sequence


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk finding counting sequence
# 1  
Old 10-27-2011
awk finding counting sequence

Can awk count numbers until it reaches the end of the sequence after the slash?

input:
HTML Code:
serv1a, 32, 41/47, 53, 89/100, 108/11, 113.
serv1b, 1/2, 114/18, 121/35, 139/40, 143/55, 159/64,
serv2, 255/56, 274/77, 763, 774/75, 777, 1434/35, 1444/50, 1715, 2025/31, 2048.
serv10b, 804, 808, 929/32, 940/42, 977/1020, 1240/41, 1246, 1254/56, 1353, 1377/82.
serv11, 492/538, 674/75, 927/281, 403/06, 1444/50, 1510/19, 1530/37, 1613/18, 1621/23.
serv1a, 255/56, 274/77, 763, 774/75, 777, 1434/35, 1444/50, 1715, 2025/31, 2048.
serv3, 804, 808, 929/32, 940/42, 977/1020, 1240/41, 1246, 1254/56, 1353, 1377/82.
serv4b, 492/538, 674/75, 927/28, 483/87, 492/538, 674/75, 681/85, 689/90, 704/11 762,.
serv5, 1044/47, 483/87, 492/538, 674/75, 681/85, 689/90, 704/11.
serv6, 39/40, 54/55, 80, 82/85, 123/24, 256, 264/70, 275/76, 332/67, 369/80, 401/04. 
serv8, 1044/47, 483/87, 492/538, 674/75, 681/85, 689/90, 704/11 762, 810, 839, 927/28.
serv9a, 255/56, 274/77, 763, 774/75, 777, 1434/35, 1444/50, 1715, 2025/31, 2048.
output
HTML Code:
serv1a, 32, 41, 42, 43, 44, 45, 46, 47, 53, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 108, 109, 110, 111, 113.
serv1b, 1, 2, 114, 115, 116, 117, 118, 121, 122, 123, 124, 125, 126, 127.....
serv2, 255, 256, 274, 275, 276, 277, 763, 774, 775, 777, 1434, , 1435, 1444, 1450, 1715, 2025, 2026, 2027, 2028, 2029, 2030, 2031, 2048.
...
# 2  
Old 10-27-2011
I've written quick and dirty solution on perl, but got an infinitive loop. What's this:
927/281
???
# 3  
Old 10-27-2011
Quote:
Originally Posted by yazu
I've written quick and dirty solution on perl, but got an infinitive loop. What's this:
927/281
???
Sorry a mistake i must have done by hand. It should read 927/28
# 4  
Old 10-27-2011
Try this...

Code:
awk -F"," '
{
        printf $1 FS
        for(i=2;i<=NF;i++)
        {
                if(!match($i,"/")){
                        printf $i FS
                        continue
                }
                split($i,arr,"/")
                a=arr[1]; b=arr[2]
                if(arr[1]>arr[2])
                {
                        num=substr(arr[1], 1, length(arr[1])-length(arr[2]))
                        b=num arr[2]

                }
                for(j=a;j<=b+0;j++)
                {
                        printf " "j FS
                }
        }
        printf "\n"
}' input_file

--ahamed

---------- Post updated at 05:50 AM ---------- Previous update was at 05:47 AM ----------

I just saw the post from yazu and was wondering what did my code do with that condition 927/281 and guess what it skipped it... Smilie

--ahamed

Last edited by ahamed101; 10-27-2011 at 09:52 AM.. Reason: Corrected the code for field separater
# 5  
Old 10-27-2011
Quote:
Originally Posted by ahamed101
Try this...

--ahamed



--ahamed
Thanks ahamed, your script does a great job. Though some of the numbers are ommited. Like in the First line the sequence from 108 to 111:

HTML Code:
serv1a, 32,  41  42  43  44  45  46  47 53,  89  90  91  92  93  94  95  96  97  98  99  100 113. 
serv1b,  1  2 
serv2, 763, 777, 1715, 2048. 
serv10b, 804, 808,  977  978  979  980  981  982  983  984  985  986  987  988  989  990  991  992  993  994  995  996  997  998  999  1000  1001  1002  1003  1004  1005  1006  1007  1008  1009  1010  1011  1012  1013  1014  1015  1016  1017  1018  1019  1020 1246, 1353, 
serv11,  492  493  494  495  496  497  498  499  500  501  502  503  504  505  506  507  508  509  510  511  512  513  514  515  516  517  518  519  520  521  522  523  524  525  526  527  528  529  530  531  532  533  534  535  536  537  538 
serv1a, 763, 777, 1715, 2048. 
serv3, 804, 808,  977  978  979  980  981  982  983  984  985  986  987  988  989  990  991  992  993  994  995  996  997  998  999  1000  1001  1002  1003  1004  1005  1006  1007  1008  1009  1010  1011  1012  1013  1014  1015  1016  1017  1018  1019  1020 1246, 1353, 
serv4b,  492  493  494  495  496  497  498  499  500  501  502  503  504  505  506  507  508  509  510  511  512  513  514  515  516  517  518  519  520  521  522  523  524  525  526  527  528  529  530  531  532  533  534  535  536  537  538  492  493  494  495  496  497  498  499  500  501  502  503  504  505  506  507  508  509  510  511  512  513  514  515  516  517  518  519  520  521  522  523  524  525  526  527  528  529  530  531  532  533  534  535  536  537  538  704  705  706  707  708  709  710  711 762,. 
serv5,  492  493  494  495  496  497  498  499  500  501  502  503  504  505  506  507  508  509  510  511  512  513  514  515  516  517  518  519  520  521  522  523  524  525  526  527  528  529  530  531  532  533  534  535  536  537  538 
serv6,  39  40  54  55 80,  82  83  84  85 256, 
serv8,  492  493  494  495  496  497  498  499  500  501  502  503  504  505  506  507  508  509  510  511  512  513  514  515  516  517  518  519  520  521  522  523  524  525  526  527  528  529  530  531  532  533  534  535  536  537  538  704  705  706  707  708  709  710  711 762, 810, 839, 
serv9a, 763, 777, 1715, 2048. 
Can you identify the reason?
# 6  
Old 10-27-2011
Here is my code but it doesn't work. It's because of line ends. Somewhere you have dots, somewhere commas and in one line - 704/11 762,.
Code:
perl -F',\s+' -lane '                                                     :( 
BEGIN { $" = ", " }
sub normalize {
  my ($start, $end) = split "/", $_[0];
  if ($start > $end) {
    $end = substr($start, 0, length($start) - length($end)) . $end;
  }
  $_[0] = "$start/$end";
}
sub unwind {
  return if $_[0] !~ m|/|;
  normalize $_[0];
  my ($start, $end) = split "/", $_[0];
  my @v = $start .. $end;
  $_[0] = "@v";
}
$" = ", ";
unwind($_) for @F;
print "@F";
' INPUTFILE

This User Gave Thanks to yazu For This Post:
# 7  
Old 10-27-2011
It works for me though...
Code:
root@bt:/tmp# awk -F"," '
> {
>         printf $1 FS
>         for(i=2;i<=NF;i++)
>         {
>                 if(!match($i,"/")){
>                         printf $i FS
>                         continue
>                 }
>                 split($i,arr,"/")
>                 a=arr[1]; b=arr[2]
>                 if(arr[1]>arr[2])
>                 {
>                         num=substr(arr[1], 1, length(arr[1])-length(arr[2]))
>                         b=num arr[2]
> 
>                 }
>                 for(j=a;j<=b+0;j++)
>                 {
>                         printf " "j FS
>                 }
>         }
>         printf "\n"
> }' input_file

serv1a, 32,  41, 42, 43, 44, 45, 46, 47, 53,  89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,  108, 109, 110, 111, 113.,
serv1b,  1, 2,  114, 115, 116, 117, 118,  121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,  
139, 140,  143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155,  159, 160, 161, 162, 163, 164,,
serv2,  255, 256,  274, 275, 276, 277, 763,  774, 775, 777,  1434, 1435,  1444, 1445, 1446, 1447, 1448, 1449, 1450, 
1715,  2025, 2026, 2027, 2028, 2029, 2030, 2031, 2048.,
serv10b, 804, 808,  929, 930, 931, 932,  940, 941, 942,  977, 978, 979, 980, 981, 982, 983, 984, 985, 986, 987, 988, 
989, 990, 991, 992, 993, 994, 995, 996, 997, 998, 999, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 
1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020,  1240, 1241, 1246,  1254, 1255, 1256, 1353,
serv11,  492, 493, 494, 495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511, 512, 513, 
514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 
538,  674, 675,  403, 404, 405, 406,  1444, 1445, 1446, 1447, 1448, 1449, 1450,  1510, 1511, 1512, 1513, 1514, 1515, 
1516, 1517, 1518, 1519,  1530, 1531, 1532, 1533, 1534, 1535, 1536, 1537,  1613, 1614, 1615, 1616, 1617, 1618,
serv1a,  255, 256,  274, 275, 276, 277, 763,  774, 775, 777,  1434, 1435,  1444, 1445, 1446, 1447, 1448, 1449, 1450, 
1715,  2025, 2026, 2027, 2028, 2029, 2030, 2031, 2048.,
serv3, 804, 808,  929, 930, 931, 932,  940, 941, 942,  977, 978, 979, 980, 981, 982, 983, 984, 985, 986, 987, 988, 989, 990, 
991, 992, 993, 994, 995, 996, 997, 998, 999, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011, 1012, 
1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020,  1240, 1241, 1246,  1254, 1255, 1256, 1353,
serv4b,  492, 493, 494, 495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511, 512, 513, 514, 
515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538,  674, 
675,  927, 928,  483, 484, 485, 486, 487,  492, 493, 494, 495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 
509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530, 531, 532, 533, 
534, 535, 536, 537, 538,  674, 675,  681, 682, 683, 684, 685,  689, 690,.,
serv5,  1044, 1045, 1046, 1047,  483, 484, 485, 486, 487,  492, 493, 494, 495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 
505, 506, 507, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 
530, 531, 532, 533, 534, 535, 536, 537, 538,  674, 675,  681, 682, 683, 684, 685,  689, 690,
serv6,  39, 40,  54, 55, 80,  82, 83, 84, 85,  123, 124, 256,  264, 265, 266, 267, 268, 269, 270,  275, 276,  332, 333, 334, 335, 
336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 
361, 362, 363, 364, 365, 366, 367,  369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380,
serv8,  1044, 1045, 1046, 1047,  483, 484, 485, 486, 487,  492, 493, 494, 495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 
505, 506, 507, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 
530, 531, 532, 533, 534, 535, 536, 537, 538,  674, 675,  681, 682, 683, 684, 685,  689, 690, 810, 839,
serv9a,  255, 256,  274, 275, 276, 277, 763,  774, 775, 777,  1434, 1435,  1444, 1445, 1446, 1447, 1448, 1449, 1450, 1715,  
2025, 2026, 2027, 2028, 2029, 2030, 2031, 2048.,

--ahamed

Last edited by ahamed101; 10-27-2011 at 10:16 AM..
This User Gave Thanks to ahamed101 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Sequence of conditions awk

hello gurus, I want to use an associative array from a file to populate a field of another file, by matching several columns in order of priority. If the first column matches, then i dont want to match $2. Similarly I only want to match $3 when $1 and $2 are not in associative array. For the... (6 Replies)
Discussion started by: ritakadm
6 Replies

2. UNIX for Dummies Questions & Answers

awk code to reconstruct sequence from alignment

Hi Everyone, I need some help to construct a long 'Sbjct' string from the following input using incremental order of 'Sbjct' starting number (e.g. 26325115,33716368,33769033,34869860 etc.) Different 'Sbject' string will be separated by 'NNNN's as: ... (6 Replies)
Discussion started by: Fahmida
6 Replies

3. Shell Programming and Scripting

awk counting question

Probably a simple to this, but unsure how to do it. I would prefer an AWK solution. Below is the data set. 1 2 3 2 5 7 4 6 9 1 5 4 8 5 7 1 1 10 15 3 12 3 7 9 9 8 10 4 5 2 9 1 10 4 7 9 7 12 6 9 13 8 For the second... (11 Replies)
Discussion started by: mollydog11
11 Replies

4. Shell Programming and Scripting

Counting Fields with awk

ok, so a user can specify options as is shown below: ExA: cpu.pl!23!25!-allow or ExB: cpu.pl!23!25!-block!all options are delimited by the exclamation mark. now, in example A, there are 4 options provided by the user. in example B, there are 5 options provided by the user. ... (3 Replies)
Discussion started by: SkySmart
3 Replies

5. Shell Programming and Scripting

find common entries and match the number with long sequence and cut that sequence in output

Hi all, I have a file like this ID 3BP5L_HUMAN Reviewed; 393 AA. AC Q7L8J4; Q96FI5; Q9BQH8; Q9C0E3; DT 05-FEB-2008, integrated into UniProtKB/Swiss-Prot. DT 05-JUL-2004, sequence version 1. DT 05-SEP-2012, entry version 71. FT COILED 59 140 ... (1 Reply)
Discussion started by: manigrover
1 Replies

6. Shell Programming and Scripting

counting using awk

Hi, I want to perform a task using shell script. I am new to awk programming and any help would be greatly appreciated. I have the following 3 files (for example) file1: Name count Symbol chr1_1_50 10 XXXX chr3_101_150 30 YYYY File2: Name ... (13 Replies)
Discussion started by: Diya123
13 Replies

7. Shell Programming and Scripting

suffix a sequence in awk

hi I have a string pattern like ... ... 000446448742 00432265 040520100408 21974435 DEWSWATER GARRIER AAG IK4000 N 017500180000000000000000077000000000100 000446448742 00580937 040520100408 32083576 PEWSWATER BARRIER DAG GK4000 ... (6 Replies)
Discussion started by: zainravi
6 Replies

8. Shell Programming and Scripting

Counting with Awk

I need "awk solution" for simple counting! File looks like: STUDENT GRADE student1 A student2 A student3 B student4 A student5 B Desired Output: GRADE No.of Students A 3 B 2 Thanks for awking! (4 Replies)
Discussion started by: saint2006
4 Replies

9. Shell Programming and Scripting

Counting records with AWK

I've been working with an awk script and I'm wondeing id it's possible to count records in a file which DO NOT contain, in this instance fields 12 and 13. With the one script I am wanting to display the count for the records WITH fields 12 and 13 and a seperate count of records WITHOUT fields... (2 Replies)
Discussion started by: Glyn_Mo
2 Replies

10. UNIX for Dummies Questions & Answers

how can i isolate the random sequence of numbers using awk?

as you can see there is a delimiter after c8 "::". Awk sees the rest as fields because it doesn't recognize spaces and tabs as delimiters. So i am basically looking to isolate 20030003ba13f6cc. Can anyone help? c8::20030003ba13f6cc disk connected configured unknown (2 Replies)
Discussion started by: rcon1
2 Replies
Login or Register to Ask a Question