Statement to find if an entry exists in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Statement to find if an entry exists in a file
# 1  
Old 05-09-2012
Statement to find if an entry exists in a file

I need to check if an entry input by the user is in a file. If so, I need to run a command, and if it does not exist then it should output entry does not exist.

So I have so far...

echo "Enter record:"
read record
//command || //command

Can I use an if statement to do this?
# 2  
Old 05-09-2012
Code:
enter "record:"
read rec
grep -qF "$rec" somefile
rc=$?
if [ $rc -eq 0 ]; then
    # whatever the user entered exists in somefile
    # take appropriate action/ no action
else
    # whatever user enter DOES NOT exist in the file somefile
    # do stuff here
fi

Since you question was really vague, this is about a good an answer as I have.
# 3  
Old 05-09-2012
It says -qF is not an option for the grep command
# 4  
Old 05-09-2012
What OS version and which shell? You can also try awk to replace grep.

Code:
record_file=/path/to/some/file.txt

record_exists() {
    awk -v "record=$1" 'index($0,record) {found=1;exit} END {exit !found}' "$record_file"
}

read record
if record_exists "$record"; then
  #do stuff
else
  #do other stuff
fi

edit: to be more specific about matching, if your record file is for instance delimited you can check a specific field. for instance finding a username in /etc/passwd. /etc/passwd is delimited with : and the username is the first field

Code:
user_exists() {
    awk -F: -v "user=$1" '$1 == user {found=1;exit} END {exit !found}' /etc/passwd
}


Last edited by neutronscott; 05-09-2012 at 10:15 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find a file and if exists , execute a different file

Good Morning All, I'm a novice and please excuse me if i did miss some of the forum rules. What my intention is, i have a file (services) residing @ /u01/Oracle/services. I know i can use the find command to find the "service" file. I get this file from a windows box and there is no certain... (8 Replies)
Discussion started by: thinkingeye
8 Replies

2. Shell Programming and Scripting

sed if statement to see if file exists

Is there an easy way of checking for the existence of a file that ends with the extension .order and if it exists do something? if not do nothing (7 Replies)
Discussion started by: firefox2k2
7 Replies

3. Shell Programming and Scripting

How to find out whether a file exists with the help of regular expression?

Hi all I have a list of file names in array. But this file names are not exact. so i want to search whether a file exists or not using regular expression. code snippet: if ; then echo "File exists" else echo "File does not exits" fi over here "*EQST*" should be treated as a regular... (4 Replies)
Discussion started by: Ganesh_more
4 Replies

4. Shell Programming and Scripting

Perl program for find one entry and put in different file

Hi I have a file name1 xxxxx name1 xxxxx name1 yyyyy name 1 zzzzz name1 Uniprot Id 1234 name2 sssss name2 eeeee name2 bengamine name2 Uniprot Id 3456 ......................and so on I have to capture Uniprot IDs only in a separate file so that output contain only ... (20 Replies)
Discussion started by: manigrover
20 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. Shell Programming and Scripting

IF statement to check file exists

Hi All, If i run below copy command, it works absolutely fine, /opt/csw/bin/scp axetlxyz01:/opt/data/test/QURIES* ./input I want to make the above line better, by adding an IF statement, want to check if there is any file exists with name QURIES*.* then i need to copy that. if ... (7 Replies)
Discussion started by: rkrgarlapati
7 Replies

7. Shell Programming and Scripting

find, if exists then append for file with same name

I will have to process multiple files with same name everyday. My requirement is: If on a certain day I see that filename.txt exists then the contents of the filename.txt would be added/append to the former file contents.Each time it sees the file the content would be added.But the header ... (8 Replies)
Discussion started by: RubinPat
8 Replies

8. Shell Programming and Scripting

How to check if a file exists using the if statement

Hi, I'm trying to write a bit of code that will check if a file exists and then archives the file Im trying to use the following if statement without success.. if then mv filename archive/filename else echo "no filename exists" fi Should the file name be... (3 Replies)
Discussion started by: Jazmania
3 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

unix script to check whether particular file exists and to find its size

I want to find the size of particular file exists in a particular directory and i wnt to zip it. In the below mentioned code it should check the MQ.log in the particular directory.Please correct my code so that it will check for particular MQ.log but i could not able to check whether the... (9 Replies)
Discussion started by: Balachandar
9 Replies
Login or Register to Ask a Question