Operating System - HP-UX
1751868 Members
5172 Online
108782 Solutions
New Discussion юеВ

Re: 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 13
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...