Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Korn shell pattern matching 3

Status
Not open for further replies.

ernieah

Programmer
Oct 27, 2002
42
US
(1) I have read in "several" books on the Korn shell, that the Korn shell does "pattern matching". I had presumed that meant that it matches on ANY text-file-content which is read by a Korn shell program. (Similar to the patterm matching done with a perl program.)

(2) However, when trying to do 'patterm matching" inside a "Korn shell" program which is reading a text-file, I have NOT been able to devise any "if statements" that will successfully "match" (uing regular expressions") any of the text phrases in the test file.

(3) Perhaps my "programing statement" are NOT CORRECT for pattern matching in Korn shell? OR, mayber korn shell does pattern matching just on FILENAMES, but NOT on text it reads form a text file.

(4) Program:
1 #!/bin/ksh
2 # program ksh_pattern_matching.ksh
3
4
5 while readle read -u3 g1
6 do
7
8 if [[ $g1 = "[0-9]{1}the" ]]
9 then
10 echo "Found one: \$g1 = $g1"
11 fi
12
13
14 done 3< $1

# looking to find strings like &quot;456the&quot;, or
# &quot;789the&quot;, when these kind of strings are
# EMBEDDED in a text file: $1.


(5) I dont get error messages with the above program, but I don't &quot;echo&quot; any output either. Is there somethinhg wrong with the program, or is that Korn shell just DOESN'T do this 'textual&quot; pattern matching?

(6) Maybe I just have to use perl, instead, which does this klind of thing VERY WELL!

thanx in advance.



End-of-memo: Best to you..
from ernieah.
 
#!/bin/ksh
# program ksh_pattern_matching.ksh


while read -u3 g1
do

if [[ $g1 = @(*[0-9]the*) ]]
then
echo &quot;Found one: \$g1 = $g1&quot;
fi
done 3< $1


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
As vlad showed, it does work, but Korn shell doesn't use the same regex language as Perl; it uses the same pattern matching as is used for filename expansion on the command line. This is detailed on the Korn shell man page. Also you shouldn't surround the pattern to be matched in quotes.

Annihilannic.
 
(1) Thanx very much for your PROMPT reply, vlad. However, after making the changes\s you suggested, I still get NO error messages, and NO output printed to the screen with the following usage:

./ksh_pattern_matching.ksh sample <enter>


(2) This is my ''sample &quot; data file&quot;

$cat sample

Now is 456the time for all
good men to come to 789the
aid of 123their country.
Zero response is not required.

(3) I had wanted to print-out lines
#1 because it contains: &quot;456the&quot;
#2 because it contains: &quot;789the
#3 becasue it contains: &quot;123their&quot;
and, of course NOT print
lines #2 and line #4.

(4) Thanx in advance, again.

End-of-memo: Best to you..
from ernieah.
 
Seems fine for me:

[tt]$ cat ksh_pattern_matching.ksh
#!/bin/ksh
# program ksh_pattern_matching.ksh


while read -u3 g1
do

if [[ $g1 = @(*[0-9]the*) ]]
then
echo &quot;Found one: \$g1 = $g1&quot;
fi
done 3< $1

$ cat sample
Now is 456the time for all
good men to come to 789the
aid of 123their country.
Zero response is not required.
$ ./ksh_pattern_matching.ksh sample
Found one: $g1 = Now is 456the time for all
Found one: $g1 = good men to come to 789the
Found one: $g1 = aid of 123their country.
$[/tt]

Annihilannic.
 
Given your input file, my output looks like this:
Found one: $g1 = Now is 456the time for all
Found one: $g1 = good men to come to 789the
Found one: $g1 = aid of 123their country.


But, for your desired output you might be better off with 'awk'.

nawk -f ernie.awk myFile.txt

#------------------ earnie.awk
BEGIN {
pat=&quot;[0-9]+the[^ ]*&quot;
}

match($0, pat) { print substr($0, RSTART, RLENGTH+1) }



vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanx again, everybody. I don't know yet what is different abpout the way I have written the program, or the way I am using this (same) program (which I have already changed in the ways you all suggested). I will review my work to see if I have a &quot;typo&quot; of some kind.

Is the VERSION of ksh important, here? maybe I have an outdated version?


End-of-memo: Best to you..
from ernieah.
 
If [[ don't work as expected, you can try this:
Code:
while read -u3 g1; do
  case &quot;$g1&quot; in &quot;*[0-9]the*&quot;)
    echo &quot;Found one: \$g1  =  $g1&quot;;;
  esac
done 3<$1

Hope This Help
PH.
 
Thanx everybody. I discovered I had a &quot;typo&quot;, and once I corrected it, the programs by vgersh99 and by Annihilannic worked fine.

In retrospect, I think perl is probably MUCH better than ksh in pattern matching, but I was puzzeled about why I couldn't do it all all with ksh. Thanx again.


End-of-memo: Best to you..
from ernieah.
 
Perl is an over-kill used ismply for its regex.
Consider tools with lighter foot-print.

End-of-memo
vlad

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top