Finding whether a value exists or not in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding whether a value exists or not in a file
# 1  
Old 02-22-2012
Finding whether a value exists or not in a file

I am running a query against a database and spooling it to a file.
I need to check to see if a certain value is in a specific field.

I can use awk to check the specific field.
However, part of the value changes.

Part that does not change: MYPROD
Right now I have OPS$MYPROD

But sometimes I will just have MYPROD

I do not want to grep the whole file on the off chance that a value in another field, will have MYPROD.

1. Can I do a grep on a parameter in a script?
2. if so, how do i determine does exist or doesn't exist?
3. what is the easiest way to do this?
# 2  
Old 02-22-2012
Can't you change your query to spool only those records where the field equals what you are looking for?
I think that would be the easiest way.
# 3  
Old 02-22-2012
How about (Checking field# 6 of file infile):

Code:
if [ "$(awk '$6~/MYPROD/{print "Found";exit}' infile)" = "Found" ]
then
     echo "Found it"
else
    echo "Not there"
fi

# 4  
Old 02-24-2012
Thank you. I am not very good with awk. so I don't quite follow the syntax
I do understand what you are doing. Just wanted to break it down, so I can write this myself.

"$(awk '$6~/MYPROD/{print "Found";exit}' infile)" = "Found"

The only think I have used awk for is awk '{print $1}' , etc..
so let me see if i have this straight

awk '$6 -- awk on field 6
~/MYPROD/ what is '~' is that a wildcard search?
{print "Found;exit}' -- these last 2 lines look like an if-then-end if block?
infile : does the target of the awk, always go last?
I have always done
cat "filename" | awk.

thanks!
# 5  
Old 02-26-2012
$6 ~ /MYPROD/
~ is match with regular expression, this is similar to =~ in perl. So we have field 6 matches regular expression "MYPROD".

{print "Found"; exit}
The bit in {} will only execute on lines where the proceeding expression is true. A blank expression is always true, and this is why {print $1} prints the first field of every line. Two special expressions END/BEGIN cause the following code to execute only at the end or begining of processing respectively.

This code will output the string Found\n (Note print will automatically add a newline character when outputting) and then stop processing any more lines. The exit bit avoids "Found\nFound\n" being output when you have two lines that match.

Yes, like a number of unix commands awk accepts an optional list of files to process (if none supplied standard input is processed).

Here is a little demo awk program to play with. To try it out setup files test1.txt ... testn.txt, some containing MYPROD:
Code:
awk 'BEGIN { print "Start processing Files" }
/MYPROD/ { print "File " FILENAME " Line " NR " contains MYPRO" }
END { print "All done" }' test*.txt


Last edited by Chubler_XL; 02-26-2012 at 05:07 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search if file exists for a file pattern stored in array

Hi experts, I have two arrays one has the file paths to be searched in , and the other has the files to be serached.For eg searchfile.dat will have abc303 xyz123 i have to search for files that could be abc303*.dat or for that matter any extension . abc303*.dat.gz The following code... (2 Replies)
Discussion started by: 100bees
2 Replies

2. Shell Programming and Scripting

File exists, but cannot be opened.How to check- whether it could be opened to read when it exists

Hi #Testing for file existence if ; then echo 'SCHOOL data is available for processing' else echo 'SCHOOL DATA IS NOT AVAILABLE FOR PROCESSING' : i wrote a script, where it begins by checking if file exists or not. If it exists, it truncates the database... (2 Replies)
Discussion started by: rxg
2 Replies

3. Windows & DOS: Issues & Discussions

Script that, if file exists in Samba share, moves file to Unix server

I'm looking to do pretty much what the title says. I want a script that runs, it can run on Unix or Windows, doesn't matter, and searches a Samba shares for a .txt file. If the file exists, the script will move (or possibly copy) the file from the Samba share into a directory on our Unix... (3 Replies)
Discussion started by: twcostello
3 Replies

4. Shell Programming and Scripting

Script to check for the file existence, if file exists it should echo the no of modified days

Hi, I am looking for a shell script with the following. 1. It should check whether a particular file exists in a location #!/bin/sh if ; then echo "xxx.txt File Exists" else echo "File Not Found" fi 2. If file exists, it should check for the modified date and run a command... (2 Replies)
Discussion started by: karthikeyan_mac
2 Replies

5. Shell Programming and Scripting

Newbie.. Find if a file exists and open, if not create the desired file..

Hey all, I'm brand new to script writing, I'm wanting to make a script that will ask for a file and then retrieve that file if it exists, and if it doesn't exist, create the file with the desired name, and I'm completely stuck.. so far.. #! bin/bash echo "Enter desired file" read "$file" if ... (5 Replies)
Discussion started by: Byrang
5 Replies

6. UNIX for Dummies Questions & Answers

Need help finding a file where a pattern exists and the file has a timestamp

So, I know how to do some of this stuff on an individual level, but I'm drawing a blank as to how to put it all together. I have a pattern that I'm looking for in a log file. The log file I know came in yesterday, so I want to limit the search to that day's listing of files. How would I do... (5 Replies)
Discussion started by: kontrol
5 Replies

7. Shell Programming and Scripting

Grep pattern from different file and display if it exists in the required file

Hi, I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which contains rubbish information just to fill the page which is of no use. this is... (3 Replies)
Discussion started by: abinash
3 Replies

8. Shell Programming and Scripting

Finding the line after (if it exists)

Hi there, i have a text file like this category=production memory=32gb CPU=2.6ghz category=uat memory=16gb CPU=2.6ghz memory=32gb CPU=2.6ghz memory=8gb CPU=2.8ghz Is there... (6 Replies)
Discussion started by: rethink
6 Replies

9. Shell Programming and Scripting

Need to write a script in UNIX to find a file if another file exists

So I have a lot of Java applications on my servers all having their own folder from the applications subdirectory. Now, I need to do the following. Search all the applications subdirectories for message.jar. If the message.jar file exists, I need to search the application directory for... (1 Reply)
Discussion started by: mmdawg
1 Replies

10. Shell Programming and Scripting

Check File Exists and compare to previous day file script

We have data files that are ftp'd every morning to a SUN server. The file names are exactly the same except for that each has the date included in its name. I have to write script to do 2 things: STEP 1) Verify that the file arrived in morning. STEP 2) Compare the file size of the current... (3 Replies)
Discussion started by: rbknisely
3 Replies
Login or Register to Ask a Question