The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep Three Words murbina UNIX for Dummies Questions & Answers 12 08-14-2008 03:38 AM
Multiple line match using sed SiftinDotCom Shell Programming and Scripting 15 03-28-2008 02:12 PM
Match words moutaz1983 Shell Programming and Scripting 8 01-07-2008 06:26 AM
greping out multiple words Terrible Shell Programming and Scripting 5 08-20-2006 02:58 PM
grep multiple text files in folder into 1 text file? coppertone UNIX for Dummies Questions & Answers 7 08-23-2002 02:50 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-20-2008
rider29 rider29 is offline
Registered User
  
 

Join Date: May 2008
Posts: 33
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 (permalink)  
Old 05-20-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
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 City preceed all data.
  #3 (permalink)  
Old 05-20-2008
rider29 rider29 is offline
Registered User
  
 

Join Date: May 2008
Posts: 33
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 01:11 AM..
  #4 (permalink)  
Old 05-21-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Cool 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 (permalink)  
Old 05-21-2008
rider29 rider29 is offline
Registered User
  
 

Join Date: May 2008
Posts: 33
I thank you for the help.
  #6 (permalink)  
Old 05-21-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:11 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0