grep string and output filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep string and output filename
# 1  
Old 11-19-2007
grep string and output filename

Hello,

I have over 200 files and some of them have the string like "John price $200". I would like to grep the string. Then output the filename which found the string.

I have the following script, but it ONLY output the string
echo Please input list file name:
read listn
for file in `cat $listn.txt`
do
cat $file |grep "John price $200"
done

expect output:
filename: XXXXXX
found string: John price $200


thx,
happyv
# 2  
Old 11-20-2007
Just change the commandline to grep to be:
Code:
grep "John price $200" $file /dev/null

Can use cut or awk to pick apart the output if you have to have it formatted differently
# 3  
Old 11-20-2007
not sure what OS you are using, but grep -l (that's a lowercase L) will show just the filename, not the line that matches. Do a man on grep and it should tell you how to get just the name, or tell us the os and we might be able to help some more.
# 4  
Old 11-20-2007
thx..but how can I check my OS in unix?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to grep for a string on a FILENAME?

I call my bash shell script "test.sh" and pass "admin_usr.txt" as an argument like below. ./test.sh admin_usr.txt Inside the "test.sh" i wish to check if the filename passed "admin_usr.txt" i.e "$1" contains the string "admin" or not ... which in this case it does. Note: I do not wish to... (5 Replies)
Discussion started by: mohtashims
5 Replies

2. UNIX for Dummies Questions & Answers

Find all files containing string not following symlinks CAT (modified) output content to /filename

This should recursively walk through all dirictories and search for a specified string in all present files, if found output manicured content (eg some regex) with CAT into a specified directory (eg /tmp/) one by one, keeping the original names This is what I have so far, which seems to... (1 Reply)
Discussion started by: lowmaster
1 Replies

3. Shell Programming and Scripting

Grep for string and then output...

Hi all, I have a file: /var/log/lct/buildinformation I am trying to grep for string: MANUFACTURER : VMware, Inc. If it contains the string I want to output the results of:df -h |grep '/usr|/var' |awk '{print $6 " "$5}' If it does not have the above string to send a no vm found... (6 Replies)
Discussion started by: gartie
6 Replies

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

5. Shell Programming and Scripting

grep only a string on command output

How can I grep exactly a string that has .,/ characters using grep? Example: I want to grep ONLY string1 and not string1.more or string1.more.evenmore #lsauth ALL|grep 'string1' All output: string1 <--- This is the only I want. string1.more string1.evenmore. more.string1... (4 Replies)
Discussion started by: iga3725
4 Replies

6. Shell Programming and Scripting

How to search (grep?) filename for a string and if it contains this then...

Hi i want to write a script that will search a filename e.g. test06abc.txt for a string and if it contains this string then set a variable equal to something: something like: var1=0 search <filename> for 06 if it contains 06 then var1=1 else var1=0 end if but in unix script :) (4 Replies)
Discussion started by: tuathan
4 Replies

7. Shell Programming and Scripting

Redirect grep output to dynamique fileName and subdirectory

Hi all i'm new in KSH, i want to write a script to grep a logs files and redirecting the result into a relative subdirectory/file.txt that must be created near to each log file my begin script is : find ./logs -type f -name "*.log" -exec grep error {} \; how i can perform that modest... (10 Replies)
Discussion started by: rambler
10 Replies

8. UNIX for Dummies Questions & Answers

How to grep / zgrep to output ONLY the matching filename and line number?

Hi all, I am trying to zgrep / grep list of files so that it displays only the matching filename:line number and does not display the whole line, like: (echo "1.txt";echo "2.txt") | xargs zgrep -no STRING If I use -o option, it displays the matching STRING and if not used, displays the... (3 Replies)
Discussion started by: vvaidyan
3 Replies

9. Shell Programming and Scripting

Appending to filename a string of text grep finds

I am wanting to automate a process that includes the step of appending to a filename a string of text that's contained inside the file. I.e. if filename A.fileA contains a string of text that reads 1234 after the phrase ABC, I want the shell script file to rename the file 1234_FileChecked_A.fileA.... (3 Replies)
Discussion started by: HLee1981
3 Replies

10. Shell Programming and Scripting

Losing filename in grep output

I have the following line in a script that searches files in several directories and shows the search results on the screen. ls "$path" | xargs cat | tr -s " " | fgrep -i "$search_arg" But, because I'm also using CAT and TR, the output from the search does not display the name of the file... (6 Replies)
Discussion started by: netguy
6 Replies
Login or Register to Ask a Question