Results 1 to 9 of 9
Im writing a script for unix programming class and i've hit a wall.
it asks me to writ a shell program called wgrep that searches a file for a given ...
- 10-31-2009 #1Just Joined!
- Join Date
- Sep 2007
- Location
- Ann Arbor, MI
- Posts
- 22
[SOLVED] BASH script works like grep with a twist
Im writing a script for unix programming class and i've hit a wall.
it asks me to writ a shell program called wgrep that searches a file for a given pattern, just like grep does. for each line in the file that matches, print a window around the matched line. that is, print the line that preceding the matching line, the matching line, and the line that follows the matched line. Be sure to properly handle the special cases where the pattern matches the first line of the file and where the pattren matches the last line of the file.
lets say that a file named 1234file contains the lines:
this is one
this is two
this is three
this is four
so if you run the command
output would be :$> ./wgrep "e" 1234file
>>Start of file
this is one
this is two
this is two
this is three
this is four
2 matches
here is the code i have so far and all i'm matching is the line itself and preforming the count.
like i said im at a brick wall and just need help getting on the right trackCode:# the line that preceeds it and follows it. This version of grep # also makes the exception if the file matches 1st or last lines # in the file then it displays in the line missing ->start of file # or -> end of file where the first or third line would be. count=$( grep "$1" $2 | wc -l ) regexp="$1" file="$2" grep "$regexp" $file > match.tmp while read line do if grep "$line" match.tmp > /dev/null then echo $( sed -n "/$line/p" $file ) echo -e "\n" else : fi done < $file echo $count matches
we are not allowed to use perl or awk
thank you all for help ahead of time
Joseph
- 10-31-2009 #2Just Joined!
- Join Date
- Sep 2007
- Location
- Ann Arbor, MI
- Posts
- 22
the commented nested if is the logic im thinking im needing but i have no idea what need to tests???
Code:# Matches and prints like grep with this exception of printing # the line that preceeds it and follows it. This version of grep # also makes the exception if the file matches 1st or last lines # in the file then it displays in the line missing ->start of file # or -> end of file where the first or third line would be. count=$( grep "$1" $2 | wc -l ) regexp="$1" file="$2" grep "$regexp" $file > match.tmp while read line do if grep "$line" match.tmp > /dev/null then #if [at the begining of the file] #then #echo BOF>>> #sed ........ #echo -e "\n" #elif [ at the end of the file match] #then #sed ...... #echo EOF>> #else #sed ..... #echo -e "\n" #fi else : fi done < $file echo $count matches
- 11-01-2009 #3Linux User
- Join Date
- Aug 2006
- Posts
- 458
you are not allowed to use perl/awk but are allowed to use grep and sed?? makes no sense.
- 11-01-2009 #4Just Joined!
- Join Date
- Sep 2007
- Location
- Ann Arbor, MI
- Posts
- 22
he has a rule that we cant use what we have not been shown in class.
we have learned sed and grep and the basics of looping and if statements
I know a little perl and have read up enough that this could be solved in like three lines of perl but since he has not showed us in class we can not use.
and getting the print out with a sed statement will not be hard,
my problem is coming up with a way to test with nested if statement on if its matches the line at a beginning of the line or the last line so that i can print below or above the matched line >>start of file or >> end of file if it matches those linesCode:sed -n "+1,/$line/,+1p" $file
thats where ive hit the wall and i have read and read
- 11-01-2009 #5Linux User
- Join Date
- Aug 2006
- Posts
- 458
ok, so you can use grep??? then surely you can use -A or -B option. Check grep's man page.
- 11-01-2009 #6Just Joined!
- Join Date
- Sep 2007
- Location
- Ann Arbor, MI
- Posts
- 22
thank you ghostdog74 I dont know how i looked over these options.
- 11-01-2009 #7Just Joined!
- Join Date
- Sep 2007
- Location
- Ann Arbor, MI
- Posts
- 22
It worked on my local machine which is ubuntu 8.04
however the server he tests our script on is a sun blade box running solaris 9
neither the grep or egrep work with -B or -A. So im back to square one.
however thank you it works beautifully on my local box
- 11-01-2009 #8Just Joined!
- Join Date
- Sep 2007
- Location
- Ann Arbor, MI
- Posts
- 22
works perfect on home ubuntu system which is has GNU grep and egrep
however the system my instructor tests is a solaris 9 box and it fails saying invalid options. What would be the sed equivalent?
here is the code with the grep :
Code:# Matches and prints like grep with this exception of printing # the line that precedes it and follows it. This version of grep # also makes the exception if the file matches 1st or last lines # in the file then it displays in the line missing ->start of file # or -> end of file where the first or third line would be. count=$( grep "$1" $2 | wc -l ) regexp="$1" file="$2" grep "$regexp" $file > match.tmp lastline="$(tac $file | sed -n "1p" )" firstline="$(cat $file | sed -n "1p" )" while read line do if grep "$line" match.tmp > /dev/null then if [ "$firstline" = "$line" ] then echo ">> Start of File >>" grep -A 1 "$line" $file echo -e "\n" elif [ "$lastline" = "$line" ] then grep -B 1 "$line" $file echo ">> End of the File >>" else grep -B 1 -A 1 "$line" $file echo -e "\n" fi else : fi done < $file echo $count matches
- 11-01-2009 #9Just Joined!
- Join Date
- Sep 2007
- Location
- Ann Arbor, MI
- Posts
- 22
Solved
thanks all for your help i finally solved this one.
Code:# Matches and prints like grep with this exception of printing # the line that preceeds it and follows it. This version of grep # also makes the exception if the file matches 1st or last lines # in the file then it displays in the line missing ->start of file # or -> end of file where the first or third line would be. count=$( grep "$1" $2 | wc -l ) regexp="$1" file="$2" grep "$regexp" $file > match.tmp lastline="$(tac $file | sed -n "1p" )" firstline="$(cat $file | sed -n "1p" )" while read line do if grep "$line" match.tmp > /dev/null then if [ "$firstline" = "$line" ] then echo ">> Start of File >>" sed -n "/$line/,2p" $file echo -e "\n" elif [ "$lastline" = "$line" ] then linebefore=$(tac $file| sed -n "/$line/{n;p;}") echo $linebefore sed -n "/$line/p" $file echo ">> End of the File >>" else lb=$(tac $file | sed -n "/$line/{n;p;}") echo $lb sed -n "/$line/P" $file sed -n "/$line/{n;p;}" $file echo -e "\n" fi else : fi done < $file echo $count matches rm match.tmp


