Solved

Searching for misspelled words and the line number

Posted on 2001-07-26
6
204 Views
Last Modified: 2012-06-21
I'm trying to write a script that searches for specific misspelled words.  For example, to find any misspelling for the word ISSN at the beginning of a line, I started doing this:


#! /usr/bin/perl -n

/^I(S|[^S])[^S]N/ && print;
/^I(S|[^S])[^S]N/ && print;
/^I[^S]([S]|[^S])[^N]/ && print;

The first line looks for possible misspelling in the 2nd & 3rd letters, the second line looks for possible misspelling in the 2nd letter, etc., etc.

This is a nightmare and I have about 70 such key words; is there an easier way?

If the line number that the error appears can be printed also would be great too.




0
Comment
Question by:hadrons
6 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6325874
write the correct words in a array, like
   @ok=('ISSN', 'etc.');
then match the first word against the array:
   if (!grep(/^$firstword$/,@ok) ) { print "missmatch\n"; }
0
 
LVL 84

Accepted Solution

by:
ozo earned 200 total points
ID: 6327271
Faster to write the correct words in a hash, like
  @ok{'ISSN', 'etc.'}=();
then
  if( !exists $ok{$firstword} ){ print "missmatch\n"; }

Or,
   use String::Approx;
0
 

Author Comment

by:hadrons
ID: 6329736
You guys are going to think I'm so clueless, but I wasn't able to get the desired results.  I'm sure it me not setting this up corrected, so I'll go thru it blow by blow.

I used a file with these lines:

ISSN: right1
ISBN: wrong1
NAME: right1
NAEM: wrong1

created the command script:

#! /usr/bin/perl
@ok=('ISSN', 'NAME');
if (!grep(/^$firstword$/,@ok) ) { print "missmatch\n"; }

it printed just missmatch; and when I edited the file with just the correct names, it did the same thing.


Afterwards, I looked at this line: if (!grep(/^$firstword$/,@ok) ) { print "missmatch\n"; }

and changed it to: if (!grep(/^ISSN/,@ok) ) { print "missmatch\n"; }

And it compiled, but nothing occurred.  I did the same thing to ozo code and the results matched (so this is why I think the problem is with the way I'm setting it up)





0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 8

Expert Comment

by:bebonham
ID: 6332493
I don't know grep, but here is the hash version
###this simulates readin from file###
$arr[0]="ISSN: right1";
$arr[1]="ISBN: wrong1";
$arr[2]="NAME: right1";
$arr[3]="NAEM: wrong1";
###if you want to readin from file you can use
###@arr=<FILE>;

@ok{'ISSN', 'NAME'}=();
foreach $firstword(@arr)
{
$firstword=~s/^([^:]+):.*/\1/;
if(!exists $ok{$firstword} ){ print  $firstword . " missmatch\n"; }
}
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6332495
I was just fleshing out ozo's solution, he already provided all the code.
0
 

Author Comment

by:hadrons
ID: 6391138
sorry for the long delay; there was a medical emergency and I was away from everything.  It was String::Approx; that did the trick for me, it was new to me to use modules, but I liked the results.

And I was able to use parts of the code from all of you to improve other pieces of code I was using.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SIMPLE Perl Regex 1 144
Filtering a file to table 9 98
perl: Cleaning meta tags using RegEX 12 42
Regex rule to match two different url 5 37
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now