![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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. |
|
||||
|
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.. |
|
|||||
|
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
> ck_format The file file1 is a good file The file file2 is a good file The file file3 is a bad file |
|
||||
|
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)"
Code:
template_check gives 1 template_check gives 1 template_check gives 0 template_check gives 1 template_check gives 0 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|