Help with grep inside an if loop


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with grep inside an if loop
# 1  
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 07:46 PM.. Reason: code tags, please!
# 2  
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 09:16 PM.. Reason: Please use code tags
# 3  
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  
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 for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #769
Difficulty: Medium
Intel Core i7 (2008) has an 8 MB on-die unified L3 cache that is inclusive, shared by all cores.
True or False?

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. Shell Programming and Scripting

Use xmlstarlet inside an if loop

I have a XML file of little huge size. I have to build a logic to get the count of the tag <capacity>. And have an if loop such that all the <capacity> blocks are captured one after the other. sample input file - sample1.xml <subcolumns><capacity><name>45.90</name> <index>0</index>... (1 Reply)
Discussion started by: ramprabhum
1 Replies

3. 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

4. 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

5. 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

6. 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

7. 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

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 Advanced & Expert Users

remsh inside of while loop

I have a space delimited file containing: hostname OracleSID connectstring I want to loop through the file and execute remsh to check the database processes. cat $filename | while read HOST SID CONNECT do { result=`remsh $HOST "ps -ef | grep pmon_${SID}$| grep -v grep"` if ........ (1 Reply)
Discussion started by: joettacm
1 Replies

Featured Tech Videos