grep on multiple words to match text template


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep on multiple words to match text template
# 1  
Old 05-20-2008
grep on multiple words to match text template

hi,

I have few text templates
as a simple ex:
template 1

city Name:
zip code:
state Name:

template2:

employee Name:
Phone number:

I wish to grep on given text file and make sure the text file matches one of these templates. Please give your ideas.
# 2  
Old 05-20-2008
Question one entry per file, or many entries per file?

Please clarify... for example
option #1 (each record in own file)
file1
City: Brockton
Zip: 02330
State: MA

file2
City: Boston
Zip: 02109
State: MA

OR option #2 (all records in one file)
file1
City: Brockton
Zip: 02330
State: MA
City: Boston
Zip: 02109
State: MA

Also, clarify on if variable names (like CitySmilie preceed all data.
# 3  
Old 05-20-2008
Thank you for looking into the post.


option 1 is correct. Each file will comply with one template only and Variables will always precede the data.Please give your idea to write a script that will
read the text and match these templates.

Last edited by rider29; 05-21-2008 at 02:11 AM..
# 4  
Old 05-21-2008
Tools ok, here goes...

As a test, I created 3 sample files. The first two have the proper layout, but the third is missing a field. The program is quite simple in that it counts each successful element. If three, message saying ok but if not then message saying bad.

> cat file1
City: Brockton
Zip: 02330
State: MA

> cat file2
City: Boston
Zip: 02109
State: MA

> cat file3
City: Boston
Zip: 02109
>

Code:
> cat ck_format 
#! /bin/bash
xf="file"
cnt=1
max=5

while [ $cnt -le $max ]
   do
   zf="$xf""$cnt"
#   echo $zf
#verify file integrity
   if [ -s $zf ]
      then
      flag=0
      testf=$(cat $zf | grep "^City:")
      if [ -n "$testf" ]
         then
         flag=$((flag+1))
      fi
      testf=$(cat $zf | grep "^Zip:")
      if [ -n "$testf" ] 
         then
         flag=$((flag+1))
      fi
      testf=$(cat $zf | grep "^State:")
      if [ -n "$testf" ] 
         then
         flag=$((flag+1))
      fi
      if [ $flag -eq 3 ]
         then
          echo "The file "$zf" is a good file"
         else
          echo "The file "$zf" is a bad file"
      fi   
   fi
   cnt=$((cnt+1))
done

program execution is:
> ck_format
The file file1 is a good file
The file file2 is a good file
The file file3 is a bad file
# 5  
Old 05-21-2008
I thank you for the help.
# 6  
Old 05-21-2008
You can try awk:
Code:
#!/bin/ksh
# prints a 1 if file is correctly either template 1 or template2,
#          else print 0

template_check()
{
	awk ' /^City/     {template1++}
	      /^Zip/      {template1++}
	      /^State/    {template1++}
          /^Employee/ {template2++}
          /Phone/     {template2++}
          END { if(!template1 && template2==2 || template1==3 && !template2)
                {print 1}
                else
                {print 0}
              }' "$1"
}

# test of template_check
echo "File1
City: Brockton
Zip: 02330
State: MA
" > file1

echo "template_check gives $(template_check file1)"

echo "file2
City: Boston
Zip: 02109
State: MA
" > file2

echo "template_check gives $(template_check file2)"

echo "file3
City: Boston
Zip: 02109
" > file3

echo "template_check gives $(template_check file3)"

echo "file4
Employee: John
Phone: 3456
" > file4

echo "template_check gives $(template_check file4)"

echo "file5
City: Boston
Phone: 3456
" > file5

echo "template_check gives $(template_check file5)"

output
Code:
template_check gives 1
template_check gives 1
template_check gives 0
template_check gives 1
template_check gives 0

# 7  
Old 05-23-2008
@Jim

thank you ! I am new to Unix scripting, your logic helped me a lot to build my script. I have few more questions

How can include " case Ignore " while checking for the template? &
Is there a way to check the order of the Words "City", "Zip" & "state" can be verified ie.. can We should the file is bad in case the file containts text like

Zip:
City:
State:

something like awk '/City/' ---> only in first line and then update template1 to template1+1??

Last edited by rider29; 05-28-2008 at 03:47 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep multiple words in a file with help of fixed string switch

I have multiple strings in a file which have special character $, when i search strings by ignoring $ with \ using single quotes it returns empty results. My search strings are set char_1($lock) and set new_char_clear_3($unlock) I tried searching with but it returns empty results.However... (3 Replies)
Discussion started by: g_eashwar
3 Replies

2. Shell Programming and Scripting

Gawk gensub, match capital words and lowercase words

Hi I have strings like these : Vengeance mitt Men Vengeance gloves Women Quatro Windstopper Etip gloves Quatro Windstopper Etip gloves Girls Thermobite hooded jacket Thermobite Triclimate snow jacket Boys Thermobite Triclimate snow jacket and I would like to get the lower case words at... (2 Replies)
Discussion started by: louisJ
2 Replies

3. Shell Programming and Scripting

Grep two words with exact match

HI Input : Counters Counter Int Ints Counters Counters Ints Ints I want to grep Counter|Int Output : Counter (1 Reply)
Discussion started by: pareshkp
1 Replies

4. Shell Programming and Scripting

Confused with grep for multiple words

Hi guys and gals, I have many files that contains many lines of data. I am trying to find a needle in a haystack in that I'm looking only for files that contain word1 AND word2. I'm using ... ... but this is finding files that contains word1 OR word2. No good for me. How can I grep to... (7 Replies)
Discussion started by: bbbngowc
7 Replies

5. Shell Programming and Scripting

Grep multiple words with not null value

Hi, I want to grep a file if any one (GH, IJ, KL) is not null. If it is null i dont want to pull anything. cat file | awk '{print ($1)}' Parameters are : AB=123;CD=456;EF=6789; cat file | awk '{print ($2)}' GH=456;IJ=789;KL=1011 eg: Contents in file: Parameters are :... (10 Replies)
Discussion started by: Neethu
10 Replies

6. Shell Programming and Scripting

grep - Extracting multiple key words from stdout

Hello. From command line, the command zypper info nxclient return a bloc of data : linux local # zypper info nxclient Loading repository data... Reading installed packages... Information for package nxclient: Repository: zypper_local Name: nxclient Version: 3.5.0-7 Arch: x86_64... (7 Replies)
Discussion started by: jcdole
7 Replies

7. Shell Programming and Scripting

awk or grep to match # of words before and after pattern

Pipe binary file matches grep results to file I am using grep to match a pattern, but the output is strange. $ grep -r -o "pattern" * Gives me: Binary file foo1 matches Binary file foo2 matches Binary file foo3 matches To find the lines before/after, I then have to use the... (1 Reply)
Discussion started by: chipperuga
1 Replies

8. Shell Programming and Scripting

Grep multiple words in a single file

Hello All, I'm a newbie/rookie in Shell scipting. I've done oracle export of a table using Export utility. When I do export, it generates 2 files. 1> .dmp file 2> .dmp.log file. In .dmp.log file I have to search for a sentence which goes like '0 records have been inserted' and then... (2 Replies)
Discussion started by: samfisher
2 Replies

9. Shell Programming and Scripting

grep multiple words in a single line

Hi.. How to search for multiple words in a single line using grep?. Eg: Jack and Jill went up the hill Jack and Jill were best friends Humpty and Dumpty were good friends too ---------- I want to extract the 2nd statement(assuming there are several statements with... (11 Replies)
Discussion started by: anduzzi
11 Replies

10. UNIX for Dummies Questions & Answers

search multiple words using grep

Hi frnds i want to desplay file names that should be word1 and word2 ex : i have 10 *.log files 5 files having word1 and word2 5 files having only word1, i have used below command egrep -l 'word1|word2' *.log its giving all 10 files, but i want to display only 5... (20 Replies)
Discussion started by: pb18798
20 Replies
Login or Register to Ask a Question