grep with case or if else help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep with case or if else help
# 1  
Old 11-11-2008
Question grep with case or if else help

I am trying to do a directory search using grep to find all words from a txt file. And I am not getting correct results ..can someone help me out ...below is my code..

#1/bin/sh
for word in `cat test.txt`
do
grep -ir "$word" /cygdrive/c/test /cygdrive/d/test1
case $? in
0) echo "$word" >> found.txt ;;
*) echo "$word" >> not_found.txt;;
esac
done
# 2  
Old 11-11-2008
Code:
grep -Rif test.txt /cygdrive/c/test /cygdrive/d/test1

# 3  
Old 11-11-2008
so how do i capture which words are found and which are not into files ..
# 4  
Old 11-11-2008
Lose the "-ir" on grep. In Linux the "-r" searches all files in the directory. The "-i" allows upper/lower case match. If the files you are searching actually contain whole lines which are identical to those in "test.txt", use "grep -x" for and exact match.
By the way there is a minor typo on the first line of the script.

A tip. Avoid creating files with the same name as a unix commands. A file called "test" is well known for upsetting shell "if" statements.

#/bin/sh
for word in `cat test.txt`
do
grep "$word" /cygdrive/c/test /cygdrive/d/test1
case $? in
0) echo "$word" >> found.txt ;;
*) echo "$word" >> not_found.txt;;
esac
done
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using case instead of grep

im using a case statement to search for a pattern in a variable, like this: case "${VARIABLE}" in * unix is great *) echo Found it! ;; esac However, what happens if I want to make sure some other string DOES NOT exist on the same line that "unix is great" is on?? with egrep, i can... (4 Replies)
Discussion started by: SkySmart
4 Replies

2. Shell Programming and Scripting

Ksh: Send a mail in case grep finds something

I want to search a file if it contains special strings and if yes, the records found should be mailed. I can either do it with a temporary file: /usr/bin/grep somestring somefile > /tmp/tempfile && /usr/bin/mail -s "Found something" email@mycomp.com < /tmp/tempfile... or by running the grep... (10 Replies)
Discussion started by: Cochise
10 Replies

3. Shell Programming and Scripting

Change first letter of a word from lower case to upper case

Hi all, I am trying to find a way to change first letter in a word from lower case to upper case. It should be done for each first word in text or in paragraph, and also for each word after punctuation like . ; : ! ?I found the following command sed -i 's/\s*./\U&\E/g' $@ filenamebut... (7 Replies)
Discussion started by: georgi58
7 Replies

4. UNIX for Advanced & Expert Users

penalty for case insensitive grep

I just found out there were a big performance penalty for case insensitive "grep" on big files. It would be understandable, except that the penalty seems to be exaggerated out of proportion. A real example, if I only grep a single letter "V" (or "v") , without "-i", on a big file, (file... (10 Replies)
Discussion started by: phil518
10 Replies

5. Shell Programming and Scripting

[Solved] Change Upper case to Lower case in C shell

Is there a command that can switch a character variable from UPPER case to lower case? like foreach AC ( ABC BCD PLL QIO) set ac `COMMAND($AC)` ... end Thanks a lot! (3 Replies)
Discussion started by: rockytodd
3 Replies

6. Shell Programming and Scripting

sed ignoring case for search but respecting case for subtitute

Hi I want to make string substitution ignoring case for search but respecting case for subtitute. Ex changing all occurences of "original" in a file to "substitute": original becomes substitute Origninal becomes Substitute ORIGINAL becomes SUBSTITUTE I know this a little special but it's not... (1 Reply)
Discussion started by: kmchen
1 Replies

7. Shell Programming and Scripting

data array needs to change upper case to lower case

Hi all, i have a data array as followes. ARRAY=DFSG345GGG ARRAY=234FDFG090 ARRAY=VDFVGBGHH so on.......... i need all english letters to be change to lower case. So i am expecting to see ARRAY=dfsg345ggg ARRAY=234fdfg090 ARRAY=vdfvgbghh so on........ If i have to copy this data in... (8 Replies)
Discussion started by: usustarr
8 Replies

8. Shell Programming and Scripting

Script needed to select and delete lower case and mixed case records

HELLO ALL, URGENTLY NEEDED A SCRIPT TO SELECT AND DELETE LOWER AND MIXED CASE RECORDS FROM A COLUMN IN A TABLE. FOR EXAMPLE : Table name is EMPLOYEE and the column name is CITY and the CITY column records will be: Newyork washington ... (1 Reply)
Discussion started by: abhilash mn
1 Replies

9. UNIX for Dummies Questions & Answers

lower case to upper case string conversion in shell script

How can convert a Lower case variable value to an upper case in the kron shell script. (3 Replies)
Discussion started by: dchalavadi
3 Replies
Login or Register to Ask a Question