How to find a string that is present in the file or not?

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to find a string that is present in the file or not?
# 1  
Old 12-04-2013
How to find a string that is present in the file or not?

Im pretty new to shell scripting, but i have got the jist of it over the past 10 weeks or so i have been using it. I just have one thing im stuck on thats bugging the hell out of me. Here it goes...

I need a script that -
You enter a filename and then a string which reports whether that string is present or not?
So for example, i need to enter a filename such as "testfile1" then i need to enter a string such as "hey" and it will report back to me whether the string is in the file.

I've searched high and low for a script that actually works but to no avail Smilie. Any help will be greatly appreciated.

# 2  
Old 12-04-2013
This question means joining a few things together.

First off, the 'grep' utility does exactly what you want. It doesn't read from the user, but it does find strings in files.

grep -q string filename && echo "found in file"

Second, you can read a string from the user with the read shell built-in.

echo "Type a string and hit ENTER"
echo "You typed $VARIABLE"

To make your script you can join these things together.
# 3  
Old 12-04-2013
Have a read of the manual page for grep as a starting point. if you don't have them on your PI, there are on this site too, although there may be some variations. Look under the "Man Pages" link at the top of this page.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk code to find difference in second file which is not present in first file .

Hi All, I want to find difference between two files and output only lines which are not present in second file .I am using awk and I am getting only the first difference but I want to get all the lines which are not present in file2 .Below is the code I am using . Please help to get the desired... (7 Replies)
Discussion started by: srinivasrao
7 Replies

2. Shell Programming and Scripting

Search a string in a file which is also present in another file in UNIX

Hi there, I am new to Unix and had below requirement to finish my task. I have file1.dat which has data as shown below. case1.txt case2.txt case3.txt case4.txt file1.dat has only file names I have folder which has above files mentioned in file1.dat ./all_files case1.txt... (6 Replies)
Discussion started by: raj028
6 Replies

3. Shell Programming and Scripting

Find string in file and find the all records by string

Hello I would like to get know how to do this: I got a big file (about 1GB) and I need to find a string (for instance by grep ) and then find all records in this file based on a string. Thanks for advice. Martin (12 Replies)
Discussion started by: mape
12 Replies

4. Shell Programming and Scripting

Find string in file and append new string after

Hi All, I'm trying to insert a string into a file at a specific location. I'd like to add a string after the parent::__construct(); in my file. <?php if (! defined('BASEPATH')) exit('No direct script access allowed'); class MY_Controller extends CI_Controller { function... (6 Replies)
Discussion started by: jjkilpatrick
6 Replies

5. Shell Programming and Scripting

grep exact string from files and write to filename when string present in file

I am attempting to grep an exact string from a series of files within a directory and append that output to the filename when it is present in the file. I've been after this all day with no luck. Thanks for your help in advance :wall:. (4 Replies)
Discussion started by: JC_1
4 Replies

6. Shell Programming and Scripting

find string and replace with string in other file

Dear all, I need your help, I have file like this: file1:23456 01910964830098775635 34567 01942809546554654323 67589 26546854368698023653 09778 58716868568576876878 08675 86178546154065406546 08573 54165843543054354305 . .file2: 23456 25 34567 26 67589 27 (2 Replies)
Discussion started by: attila
2 Replies

7. Shell Programming and Scripting

Find multiple string in one file using find command

Hi, I want find multiple string in one file using find coomand. And keeping it in one variable.grep is not working. (5 Replies)
Discussion started by: vivek1489
5 Replies

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

9. Shell Programming and Scripting

Script to find all the files that contain any of the words present in another file

Hi All, I am new to UNIX and shell scripts and also new to this forum. I need a script to find all the files in a directory that contain any of the strings present in another file. Please provide me the script or if you could provide pointers to any link in this forum it would be helpful.... (4 Replies)
Discussion started by: tsanthosh
4 Replies

10. Shell Programming and Scripting

To find the count of records from tables present inside a file.

hi gurus, I am having a file containing a list of tables.i want to find the count of records inside thes tables. for this i have to connect into database and i have to put the count for all the tables inside another file i used the following loop once all the tablenames are inside the file. ... (1 Reply)
Discussion started by: navojit dutta
1 Replies
Login or Register to Ask a Question