find a value in a file and echo if found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find a value in a file and echo if found
# 1  
Old 05-21-2009
find a value in a file and echo if found

I need to awk a value out of a file and see if it exists in another file.
My if statement below returns a positive even if the value doesn't exist.
The kky3 is finding the correct field for the value.


cat $PRE | while read a
do
kky2=`echo $a | awk -F: '{print $2}'`
echo "kky2 " $kky2
kky3=`echo $kky2 | awk -F '{print $1}'`
echo "kky3 " $kky3
if [ /usr/bin/egrep "$kky1" $PST ] ; then
echo "found invoice"
else
echo "not found"
fi
done
# 2  
Old 05-21-2009
From my (limited) understanding of your code, both kky1 nor PST get set outside the while loop but never ever changed inside of it ...!?
# 3  
Old 05-21-2009
kky3=`echo $kky2 | awk -F '{print $1}'`
echo "kky3 " $kky3
if [ /usr/bin/egrep "$kky1" $PST ] ; then
# 4  
Old 05-21-2009
I wrote a different version to do a more controlled test of my statements.

This is what I have.
My if statement finds the value Olive from test file one when it doesn't exist.
Perhaps if is not the way to do this?

> more testfile1
apple
orange
grapes
olive
> more testfile2
apple
grapes
orange

#!/bin/ksh
FL1=testfile1
FL2=testfile2
#
cat $FL1 | while read a
do
kky2=`echo $a | awk '{print $1}'`
echo "kky2 " $kky2
if [ '/usr/bin/egrep "$kky2" $FL2' ] ; then
echo "found fruit"
else
echo "fruit not found"
fi
done


kky2 apple
found fruit
kky2 orange
found fruit
kky2 grapes
found fruit
kky2 olive
found fruit
# 5  
Old 05-21-2009
Code:
if [ '/usr/bin/egrep "$kky2" $FL2' ] ; then

this will always be true because it is just a single quoted string in a test bracket. A string is always true.

maybe try something like this:

Code:
/usr/bin/egrep "$kky2" $FL2 >/dev/null 2>&1 && echo found fruit || echo fruit not found

# 6  
Old 05-21-2009
That works ! thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assistance with my Find command to identify last part of a file name and report the name found

Hello Forum, We have two bootstraps of Chef in our environment which are identified by colour: /var/chef/cache/cookbooks/bootstrap_cookbooks_version_green and /var/chef/cache/cookbooks/bootstrap_cookbooks_version_red I'm attempting to identify which version is installed based on the name... (11 Replies)
Discussion started by: greavette
11 Replies

2. Shell Programming and Scripting

Find header in a text file and prepend it to all lines until another header is found

I've been struggling with this one for quite a while and cannot seem to find a solution for this find/replace scenario. Perhaps I'm getting rusty. I have a file that contains a number of metrics (exactly 3 fields per line) from a few appliances that are collected in parallel. To identify the... (3 Replies)
Discussion started by: verdepollo
3 Replies

3. UNIX for Dummies Questions & Answers

How to find a file based on pattern & return the filename if found?

Hi all, I am a newbie here. I have this requirement to find a file based on a pattern then return the filename if found. I created a script based on online tutorials. Though, I am stuck & really appreciate if anyone can have a quick look & point me to the right direction? #Script starts... (10 Replies)
Discussion started by: buster_t
10 Replies

4. Linux

Find String in FileName and move the String to new File if not found

Hi all, I have a question.. Here is my requirement..I have 500 files in a path say /a/b/c I have some numbers in a file which are comma seperated...and I wanted to check if the numbers are present in the FileName in the path /a/b/c..if the number is there in the file that is fine..but if... (1 Reply)
Discussion started by: us_pokiri
1 Replies

5. Shell Programming and Scripting

Bash: Find and echo value in Array

Newbie to bash here. I think this is fairly simple, but I have searched and cannot figure it out. In the code below, I am searching an array for an IP address, and then printing the IP address if found. However, I would like to print the actual variable found such as 2.2.2.2=2, but cannot figure... (1 Reply)
Discussion started by: lozwell
1 Replies

6. Shell Programming and Scripting

echo x - returns: x: command not found

I have been experiencing this problem intermittantly, I thought the problem was '/bin/sh -> /bin/dash' but I changed that to bash and the problem persists. I am writing functions to be included in user's '.bash_profile' through source or '.' filename a quick example of the problem is illustrated... (3 Replies)
Discussion started by: bsquared
3 Replies

7. UNIX for Dummies Questions & Answers

File not found using find

Hi, i'm currently writing a script which tidys up old files. When using the find command I found that some files were not being listed /export/home/ops***/test: ls -l processed total 0 -rw-rw-r-- 1 ops*** ****** 0 Apr 20 11:53 test99 /export/home/ops***/test: ls -l total 4... (9 Replies)
Discussion started by: chris01010
9 Replies

8. UNIX for Dummies Questions & Answers

find and remove rows from file where multi occurrences of character found

I have a '~' delimited file of 6 - 7 million rows. Each row should contain 13 columns delimited by 12 ~'s. Where there are 13 tildes, the row needs to be removed. Each row contains alphanumeric data and occasionally a ~ ends up in a descriptive field and therefore acts as a delimiter, resulting in... (1 Reply)
Discussion started by: kpd
1 Replies

9. Shell Programming and Scripting

Help with find command and list in a long format each found file

The purpose of those comands are to find the newest file in a directory acvrdind to system date, and it has to be recursively found in each directory. The problem is that i want to list in a long format every found file, but the commands i use produce unexpected results ,so the output lists in a... (5 Replies)
Discussion started by: alexcol
5 Replies

10. UNIX for Dummies Questions & Answers

How do I output or echo NONE if grep does not find anything?

I am performing a grep command and I need to know how to echo "NONE" or "0" to my file if grep does not find what i am looking for. echo What i found >> My_File grep "SOMETHING" >> My_File I am sure this is easy, I am sort of new at this! Thanks (2 Replies)
Discussion started by: jojojmac5
2 Replies
Login or Register to Ask a Question