Help with grep inside an if loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with grep inside an if loop
# 1  
Old 04-10-2011
Help with grep inside an if loop

Hello All,

I have been reading posts on here for a while, but this is my first post. I have a document in which many sentences appear, and I am piping it through an exterior script which will tag each word in the document with its part of speech (not part of my script, just background). The output of this working script is vertical; that is, column 1 has the original word, column 2 has its part of speech, and column 3 is unimportant. A typical line of text becomes this when run through the part of speech tagger:

Code:
adam01.cha    JJ    adam01.cha
:    :    :
*CHI    NN    *chi
:    :    :
I    PP    i
beat    VVP    beat
drum    VV    drum
.    SENT    .

I need to find every instance that certain words appear when tagged as 'VVN', find that specific instance of that word in the original document, and delete it.

I want to search the document line by line using a series of if statements (each if statement should be evaluated on each line). I also decided to run a counter, and use that number to find the line of the original document, where I could then delete that line. Currently, though, my code is only outputting the list of words in the for loop, and I can't get it to enter the first if inside of the while loop.

My code is below:

Code:
for var in bent bound bled bred brought built burned burnt bought caught clung crept dealt dug dived dreamed dreamt fed felt fought found fled flung ground hung heard held kept knelt laid led leaped leapt learned learnt left lent lighted lost made meant met misspelled misspelt mowed mown paid pled proved proven sawed sawn said sought sold sent sewed sewn shaved shaven shone shoed shod shot showed sat slept slid slung sowed sown sped spent silted spilt spun sprung stood stuck stung struck strung swept swelled swollen swung taught told thought thrived understood upheld waved woven wept wound won withheld withstood wrung 
do
    cd ~
    cd Documents/UPenn/'Senior Year'/'Spring 2011'/Thesis/Results/
    echo "$var"
    NUMLINE=0
    while read -r line
    do
        if grep "#|Number|CHI"* >/dev/null; then
            NUMLINE=`expr $NUMLINE + 1`
        else 
            if grep "$var    VVN"* >/dev/null; then 
            sed -i '$NUMLINE' d Brown_Adam_CIVForms.txt
            fi
        fi
    done < foradam.txt
done

Help is greatly appreciated - thank you all in advance!
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 04-10-2011 at 06:46 PM.. Reason: code tags, please!
# 2  
Old 04-10-2011
search a directory by key word

Here is a basic script to search a given directory for a key word. To execute it if it was saves with the file name of test then it would be executed by typing ./test (directory path) (keyword)
of course this is after the permissions was edited by chmod 755 (filename)
Code:
#!/bin/bash

count=0
   currcount=0
   maxcountfile=0
ls $1 > directory
exec 0<directory
   while read filename
   do
      nums=$(grep -o $2 $1/"$filename" | wc -l)
      if [ $nums -gt $currcount ]
         then
         currcount=$nums
         say="File $filename has $nums occurrences of $2"         
      fi 
   done
echo $say


Last edited by Scott; 04-10-2011 at 08:16 PM.. Reason: Please use code tags
# 3  
Old 04-10-2011
still not working

Thanks for the help! I made some changes, but I am still having problems. Now I am getting thrown the following:


./Present.sh: line 35: syntax error near unexpected token `done'
./Present.sh: line 35: ` done '


Any help is appreciated!

The current code I am using:

Code:
for var in bent bound bled bred brought built burned burnt bought caught clung crept dealt dug dived dreamed dreamt fed felt fought found fled flung ground hung heard held kept knelt laid led leaped leapt learned learnt left lent lighted lost made meant met misspelled misspelt mowed mown paid pled proved proven sawed sawn said sought sold sent sewed sewn shaved shaven shone shoed shod shot showed sat slept slid slung sowed sown sped spent silted spilt spun sprung stood stuck stung struck strung swept swelled swollen swung taught told thought thrived understood upheld waved woven wept wound won withheld withstood wrung 
do
	cd ~
	cd Documents/UPenn/'Senior Year'/'Spring 2011'/Thesis/Results/
	echo "$var"
	NUMLINE=0
	cat foradam.txt | while IFS=$'\t' read -r word pos other; do
		if [ # = "$word" ] || [ Number = "$word" ] || [ CHI = "$word" ]; then
			NUMLINE=`expr $NUMLINE + 1`
			echo "error 1"
			echo "$word"
			echo "$NUMLINE"
		if [ "$var" = "$word" ] && [ VVN = "$pos" ]; then 
			sed -i '$NUMLINE' d Brown_Adam_CIVForms.txt
			echo "error 4"
			echo "$word"
			echo "$NUMLINE"
		else
			echo "nothing on this line"
			echo "$word"
			echo "$NUMLINE"
		fi
	done 
done

# 4  
Old 04-11-2011
Put quotes around fixed strings you are testing for:

Code:
if [ "#" = "$word" ] || [ "Number" = "$word" ] || [ "CHI" = "$word" ]; then[
...
if [ "$var" = "$word" ] && [ "VVN" = "$pos" ]; then

Sed needs 1 argument for sed string and env vars must be in double quotes to be expanded, see use of ${var} when followed by alphanum:

Code:
sed -i "${NUMLINE}d" Brown_Adam_CIVForms.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody, I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it :o My code: #!/bin/sh set -e set -u export IFS= optl="Optl" LOCSTORCLI="/opt/lsi/storcli/storcli" ($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " "... (5 Replies)
Discussion started by: Subsonic66
5 Replies

2. UNIX for Dummies Questions & Answers

Write a while loop inside for loop?

I'm taking a unix class and need to countdown to 0 from whatever number the user inputs. I know how to do this with a while or until loop but using the for loop is throwing me off.... I know I can use an if-then statement in my for loop but can I include a while loop in my for loop? (3 Replies)
Discussion started by: xxhieixx
3 Replies

3. Shell Programming and Scripting

If inside If loop

Hi All, Below is the very simple code snippet but it si giving me syntax error #!/bin/bash #To ensure If JMS directory exists or not ServerName=$(hostname) #To ensure If JMS directory exists or not echo $ServerName if ; then echo "Inside First If" if ; then echo 'JMS... (4 Replies)
Discussion started by: sharsour
4 Replies

4. Shell Programming and Scripting

Ping inside FOR loop

Hello, I'm trying to write (my own primitive) traceroute command that would write out all IPs that packets are hoping through. :) So I've put the ping command inside a for loop #!/bin/bash domain=${1-www.google.com}; #If no argument, then do www.google.com. for ((i=1; i<=30; i++)) #... (9 Replies)
Discussion started by: courteous
9 Replies

5. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

6. Shell Programming and Scripting

Using 'su' inside a loop

Hi, I am using su within a for loop. As you might expect, it prompts for password during each loop execution. Here is my piece of code: for i in $LIST do if then DATABASE=`echo $i | awk -F "|" '{ print $1 }'` USER_ID=`echo $i | awk -F "|" '{ print $2 }'` su - apstage -c... (1 Reply)
Discussion started by: sugan
1 Replies

7. UNIX for Dummies Questions & Answers

SED inside while loop

Hi, im having problem creating a loop using my code: aside from the fact that the 1st variable (VAR) does not increment, it loops more than the expected output. for sample purposes, test csv contains 3 lines. #get number of lines in the file lines=$( wc -l < test.csv ) ... (5 Replies)
Discussion started by: paoie
5 Replies

8. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

9. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

10. UNIX for Dummies Questions & Answers

Reading inside a loop

Hi I am trying read in side a do statement but it is not working it is just printing abc before read but not stopping in abc for user input Can anybody please help #!/usr/bin/ksh cat sample_file | while read ln_source3 do param=`echo $ln_source3 | nawk... (1 Reply)
Discussion started by: ssuresh1999
1 Replies
Login or Register to Ask a Question