General
cancel
Showing results for 
Search instead for 
Did you mean: 

shell script - for loop statement

SOLVED
Go to solution
amonamon
Regular Advisor

shell script - for loop statement

Hello

I have file:

0011
5566
1265
7889
7878

I should match pattern aabb so that output could be:

0011
5566

so far I did this:

set -A arr `echo "0:1:2:3:4:5:6:7:8:9" | nawk -F":" '{for(i=1;i<=NF;i++) print $i}'`
for num in ${arr[@]}
do


grep $num$num00 tmp
grep $num$num11 tmp
grep $num$num22 tmp
grep $num$num33 tmp
grep $num$num44 tmp
grep $num$num55 tmp
grep $num$num66 tmp
grep $num$num77 tmp
grep $num$num88 tmp
grep $num$num99 tmp


done


is there more effective way?
13 REPLIES
amonamon
Regular Advisor

Re: shell script - for loop statement

it could be nice if possible to have another for statement inside this one to loop from 0 - 9
amonamon
Regular Advisor

Re: shell script - for loop statement

again me
also I tryed this one..

#!/bin/ksh

k=0
l=0

while [ $k -lt 10 ]; do

while [ $l -lt 10 ]; do
grep $k$k$l$l tmp
l=`expr $l + 1`
done

k=`expr $k + 1`
done

again stupid result..
Hemmetter
Esteemed Contributor

Re: shell script - for loop statement

Hi

try this one:


$ grep "\([0-9]\)\1\([0-9]\)\2" file

and see regexp(5)

rgds
HGH

Ollie Rowland
Frequent Advisor

Re: shell script - for loop statement

Hi,

Or you could use perl...

Create the script match.pl
#!/usr/bin/perl
while () {
($a, $b) = /(.).(.)./;
print $_ if ($_ =~ /$a$a$b$b/);
}

and put your numbers in match.txt

Execute with

cat match.txt | match.pl

Don't forget to make your match.pl executable!
amonamon
Regular Advisor

Re: shell script - for loop statement

no perl pleasee..

Hemmetter that was nice..but also with this grep numbers 7777 and 3333 and 1111 etc. are also colected..but they should not be taken.
Ollie Rowland
Frequent Advisor
Solution

Re: shell script - for loop statement

#!/usr/contrib/bin/perl

while () {
($a, $b) = /(.).(.)./;
print $_ if ($_ =~ /$a$a$b$b/ and $a != $b);
}


<<< GRIN >>>
Hemmetter
Esteemed Contributor

Re: shell script - for loop statement

Hi again,

it is as easy as:

$ grep "\([0-9]\)\1\([0-9]\)\2" tmp | grep -v "\([0-9]\)\1\1\1"



Rgds
HGH
amonamon
Regular Advisor

Re: shell script - for loop statement

nice..I would never come up with that easy line..

thanks
James R. Ferguson
Acclaimed Contributor

Re: shell script - for loop statement

Hi Amonamon:

> no perl please...

Why? Perl runs on virtually any kind of platform you can find --- that "other" operating system, too.

A shell script can just as easily run a Perl snippet as it can a 'grep' process.

All that aside, here's a regular expression in Perl (using negative look-ahead) to do your job:

# X="0011\n5566\n1265\n7889\n7878" #...your data

# echo ${X} | perl -ne 'print if m{ ((^\d)\2) (?!\2) }x'
0011
5566

Regards!

...JRF...
amonamon
Regular Advisor

Re: shell script - for loop statement

well...:( no perl becouse I do not have experience with it..and I do not have it installed..

also haw to match numbers taht are incremented?

like

0123
5678
3456
4567
1111
0011
9999

result should be:

0123
5678
3456
4567

so far I tryed this: please do not laugh..:)

grep "\([0-9]\)\(\1+1)\(\1+2)\(\1+3)" tmp

thanks.
Ollie Rowland
Frequent Advisor

Re: shell script - for loop statement

Hi,

I know you don't want perl, but here goes anyway:

#!/usr/contrib/bin/perl

while () {
($a, $b, $c, $d) = /(.)(.)(.)(.)/;
print $_ if ($_ =~ /^$a$b$c$d$/ and $b == $a+1
and $c == $b+1
and $d == $c+1 );
}
Hemmetter
Esteemed Contributor

Re: shell script - for loop statement

hi,

since all ascending 4 digit sequences are:
"0123|1234|2345|3456|4567|5678|6789"

it ist a simple grep statement:
$ egrep "0123|1234|2345|3456|4567|5678|6789" tmp


rgds
HGH
Ollie Rowland
Frequent Advisor

Re: shell script - for loop statement

Hi,

Thanks Hemmetter, I've also learned lots with this thread.

It's a shame I can't assign points to you for this......!