Find something inside a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find something inside a file
# 1  
Old 01-31-2010
Find something inside a file

Hi,
this is the problem i'm facing i need a check some file having some word like gzip or tar ,the way i normally do is
Code:
grep gzip filename

the thing is that i had some 1000 files to be checked is that the only way or can i customize it by some script ,i know there is some way ,but i need some lead into it
# 2  
Old 01-31-2010
If I understand you correctly, you need to search for the occurrence of a word like e.g. "gzip" in a bunch of uncompressed files. If those file are called filename<something>.ext then normally you can use:
Code:
grep gzip filename*.ext

It may be that there are too many files and the number of arguments to the grep command becomes too large. In that case you can use something like:
Code:
ls|while read f 
do
  case $f in
    filename*.ext) grep -H gzip $f
  esac
done

# 3  
Old 01-31-2010
Code:
grep -l gzip *

Will list the files that contain the work gzip, searching all the files in the current directory
# 4  
Old 02-01-2010
Quote:
Originally Posted by Scrutinizer
If I understand you correctly, you need to search for the occurrence of a word like e.g. "gzip" in a bunch of uncompressed files. If those file are called filename<something>.ext then normally you can use:
Code:
grep gzip filename*.ext

It may be that there are too many files and the number of arguments to the grep command becomes too large. In that case you can use something like:
Code:
ls|while read f 
do
  case $f in
    filename*.ext) grep -H gzip $f
  esac
done

hi
i put the code in a shell script and and ran it nothing happens
do i have do pass the file if so how can i and one more thing i want to make a search by user interactive like it has to ask what word you have to search something like that
# 5  
Old 02-01-2010
What happens if you enter:
Code:
ls filename*.ext

It should list all the files you'd like to grep. Of course these names are not to literal names and you should change them to your particular situation.

Quote:
Originally Posted by malickhat
[..]nd one more thing i want to make a search by user interactive like it has to ask what word you have to search something like that
You could pass the string as a command line parameter:
Code:
searchstring="$1"

or you can ask for it interactively
Code:
echo "Enter searchstring:"
read searchstring

Then you can grep like so:
Code:
grep "$searchstring" filename*.ext


Last edited by Scrutinizer; 02-01-2010 at 03:14 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find all .sh files in file system and need to replace the string inside .sh files

Hi All, I need to write a script to find all "*.sh" files in /home file system and if any string find "*.sh" files with the name vijay@gmail.com need to replace with vijay.bhaskar@gmail.com. I just understood about the find the command to search .sh files. Please help me on this. find / -name... (3 Replies)
Discussion started by: bhas85
3 Replies

2. Shell Programming and Scripting

How to find the Missing date inside the FILE?

Hi am using Unix AIX Ksh have a FILE CAT FILE 08/02/2013 16/02/2013 18/02/2013 I need the Outputs as Missing date are 09/02/2013 to 15/02/2013,17/02/2013 can anyone help me !!! (1 Reply)
Discussion started by: Venkatesh1
1 Replies

3. Shell Programming and Scripting

find biggest number inside file

Hi, I wanna find the biggest number inside of a file this is kind of example of file: 9 11 55 then i just wanna print out the biggest number i had try sed filenale | sort -k1,1n | paste -s -d',' - but i had no success ... (7 Replies)
Discussion started by: prpkrk
7 Replies

4. Shell Programming and Scripting

How can i find a word inside a file from a directory and it's subdirectory ?

Suppose i have a word "mail". I have to search this word in all files inside a directory and it's sub-directories. It will also search in all hidden directory and sub-directories. If it finds this word in any file it will list that file. How can i do this with perl/ruby/awk/sed/bash or... (9 Replies)
Discussion started by: cola
9 Replies

5. Shell Programming and Scripting

how to find the pattern inside the file and replace it

hello everybody, I have a group of file eg- sample1 sample2 sample3 sample4 each file contain this :- cat sample1 SEQ_NUM,1,UPESI1 My requirement is to change the value-UPESI1 to UPE10 in file which contain this pattern -UPESI1. any help is appreciated. (2 Replies)
Discussion started by: abhigrkist
2 Replies

6. UNIX for Dummies Questions & Answers

How to find a file with specific string inside it.

Hi , Is there any way i can find a file with specific word inside it. For example if i want to find a file which has some text written inside it. How would i form a command to search them? (3 Replies)
Discussion started by: pinga123
3 Replies

7. Shell Programming and Scripting

How to find files only inside the subdirectories only?

Hi I have a directory with two subdirectories and also have a code like below to search files modified in last 2 minutes. # ls hello080909.txt inbox outbox # find . -type f -mmin +2 ./inbox/hello2080909.txt ./outbox/hi0080909.txt ./hello080909.txt The above code just searches and... (3 Replies)
Discussion started by: Tuxidow
3 Replies

8. Shell Programming and Scripting

How to find a string inside files

Hi, I would like to know how to get a list of files that contain a specific string inside them. Thanks (12 Replies)
Discussion started by: yoavbe
12 Replies

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

10. Shell Programming and Scripting

how to find Script file location inside script

I have to find out the file system location of the script file inside script. for example a script "abc.sh" placed anywhere in the file system when executed shold tell by itself the location of it. example #pwd / #./abc this is / #cd /root #./abc this is /root #cd / #/root/abc this... (10 Replies)
Discussion started by: asami
10 Replies
Login or Register to Ask a Question