Need help with Regular Expressions and nested loops


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with Regular Expressions and nested loops
# 1  
Old 05-26-2011
Need help with Regular Expressions and nested loops

Ok... am going slightly loopy trying to get this working (no pun intended)
What I need is to modify this code which takes a string input then echo's each character on a seperate line, to do the same thing but to put DIGIT: in front of numbers and LETTER: in front of letters.

I know a regular expression must be used to check if it is a number, this should also be nested inside an IF statement.

Thanks again for all your help everyone Smilie

Mikey

Code:
 
#!/bin/bash
clear
 
read -p "Please enter a word: " str1
 
strLength=${#str1}

for ((i=1;i<=$strLength;i++)){
        echo $str1 | cut -c$i
}
 
#must use REG-EX  ^[0-9]$
#incorporate into an IF/ELSE statement

# 2  
Old 05-26-2011
Hi,

Test this 'Perl' script:
Code:
$ cat infile
Is th1s 0k?
I hop3 s0
$ cat script.pl
use strict;
use warnings;
use autodie;

@ARGV == 1 or die "Usage: perl $0 input-file\n";
open my $fh, "<", $ARGV[0];
my @chars;

while ( <$fh> ) {
    chomp;
    @chars = split //;
    foreach ( @chars ) {
        if ( /[[:digit:]]/ ) {
            print "DIGIT: ", $_, "\n";    
        } elsif ( /[[:space:]]/ ) {
            next;
        } else {
            print "LETTER: ", $_, "\n";
        }
    }
}
    
close $fh;
$ perl script.pl infile
LETTER: I
LETTER: s
LETTER: t
LETTER: h
DIGIT: 1
LETTER: s
DIGIT: 0
LETTER: k
LETTER: ?
LETTER: I
LETTER: h
LETTER: o
LETTER: p
DIGIT: 3
LETTER: s
DIGIT: 0

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 3  
Old 05-26-2011
hey birei thanks for your quick response, however i have no idea how to use perl !! does this code simply run in unix vi?

if possible i would like to learn how to do this using purely unix syntax i.e the regular expression ^[0-9]$ with an if/else statement.

thanks again Smilie
# 4  
Old 05-26-2011
Why must use reg exp? Sounds like homework.
danmero wrote a nice example how to use case/esac in your other thread:
https://www.unix.com/shell-programmin...-possible.html

Code:
#!/bin/bash
clear

read -p "Please enter a word: " str1

strLength=${#str1}

for ((i=1;i<=$strLength;i++)){
      VAR=$(echo $str1 | cut -c$i)
      case $VAR in
         [0-9])      echo "digit: $VAR";;
         [A-Za-z])   echo "alpha: $VAR";;
      esac
}

exit 0

This User Gave Thanks to zaxxon For This Post:
# 5  
Old 05-26-2011
Hi, it's not that i must use them, im just trying to familiarise myself with unix shell scripting, it gets to the point where i know what im trying to do but cannot get the correct syntax.

Nooo not homework ! Im not a student or anything am just working through project examples with only google and this wonderful site as reference!

Have just covered case statements and am getting the hang of those, it was just how to use a regX properly as I couldnt get it to work previously.

I will have a play with this though.. thanks so much for your help !
# 6  
Old 05-26-2011
Tools like grep, sed, awk etc. use reg exp. If you want to get familiar with reg exps, maybe try to start working with those 3 tools. For grep there is grep -E or egrep, which gives access to a broader range of regular expressions than a simple grep.

For reg exps checkout this:
Regular Expressions

For the use of regular expressions in a shell (test in double brackets) I just found this in the bash man page:
Code:
              An additional binary operator, =~, is available, with the same precedence as == and !=.  When it is used, the string to the right of  the
              operator  is  considered  an  extended  regular expression and matched accordingly (as in regex(3)).  The return value is 0 if the string
              matches the pattern, and 1 otherwise.  If the regular expression is syntactically incorrect, the conditional expression's return value is ...

Can also try playing around with this.
This User Gave Thanks to zaxxon For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh - Need Help Reducing Nested Loops

Hello, I pulled out some old code from an unfinished project the other day and wanted to stream line it better. I know anything beyond a double loop is usually bad practice, and I came up with some logic for later that would no longer require the first loop in the following code that works: ... (5 Replies)
Discussion started by: Azrael
5 Replies

2. Shell Programming and Scripting

two while nested loops

for server in $(echo `cat /tmp/ScanHosts_${USERSNAME}.TXT`) do for portnumber in $(echo `cat /tmp/ScanPorts_${USERSNAME}.TXT`) do #echo ${server} ${portnumber} ... (3 Replies)
Discussion started by: SkySmart
3 Replies

3. UNIX for Dummies Questions & Answers

Executing nested loops+foreach

It's been a while since I used csh formatting and I am having a little bit of trouble with a few things. Things seem so much easier to execute in Matlab, however I need to do this on the terminal because of the programs I am trying to interact with. So here's what I want to do: I have a file... (0 Replies)
Discussion started by: katia
0 Replies

4. Shell Programming and Scripting

Nested for loops

Greetings All, The following script attempts to enumerate all users in all groups in the group file(GROUP) and echo the following information: GROUP ---> USER The script is as follows: IFS="," for GROUP in `ypcat -k group | cut -d" " -f1` do for USER in `ypcat -k group... (13 Replies)
Discussion started by: jacksolm
13 Replies

5. Shell Programming and Scripting

KSH nested loops?

KSH isn't my strong suit but it's what my company has to offer. I've got a script with two nested loops, a FOR and UNTIL, and that works fine. When I add a CASE into the mix I end up getting "Unexpected 'done' at line xx" errors. Any suggestions on this? for divi in at ce ci cm co de di fl... (9 Replies)
Discussion started by: mrice
9 Replies

6. Shell Programming and Scripting

Nested while loops (ksh scripting)

You can use one while inside another? I made the following script (without really knowing if I can use two while) to get 3 numbers different from each other at random: num1=$(( $RANDOM % 10 )) num2=$num1 while do num2=$(( $RANDOM % 10 )) done num3=$num1 while do while do... (1 Reply)
Discussion started by: ale.dle
1 Replies

7. Shell Programming and Scripting

nested for loops

I need help getting over this bump on how nested for loops work in shell. Say i was comparing files in a directory in any other language my for loop would look like so for(int i=0;to then end; i++) for(int y = i+1; to the end; y++) I can't seem to understand how i can translate that... (5 Replies)
Discussion started by: taiL
5 Replies

8. Shell Programming and Scripting

Grepping within nested for loops

Good morning - I have publication lists from 34 different faculty members. I need to end up with the numbers of publications in common across all 34 faculty. I need to grep person1 (last name) in list2, person1 in list3, person1 in list 4, etc., then person2 in list3, person 2 in list4, etc.,... (2 Replies)
Discussion started by: Peggy White
2 Replies

9. Shell Programming and Scripting

Complex problem about nested for loops

Hey, I'm writing this bash script that will test print me many copies of the same program but with different combos of 4 variables being between 1 and 100. Here's the code: #! /bin/bash x=0 for ((a=1; a < 101; a++)) do for ((b=1; b < 101; b++)) do for ((c=1; c < 101; c++)) do for... (4 Replies)
Discussion started by: Silverlining
4 Replies
Login or Register to Ask a Question