Grep multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep multiple files
# 1  
Old 04-01-2015
Grep multiple files

Hello,

I am trying to grep, in bash, multiple specific files at once. I understand you can grep parameters file1 file2... fileN.

My question is how to do this when the specific files are not constant in name or quantity? (min 2 files, max 24 files)

Example 1: grep parameters file1 file2
Example 2: grep parameters file1 file2 file3 file4

I've tried using an array with [@] and a string with the files names separated by a space. Both don't work.

I do need to keep this to a single grep if possible.

Thanks!

Last edited by Don Cragun; 04-01-2015 at 10:01 PM.. Reason: Add ICODE tags.
# 2  
Old 04-01-2015
Try

Code:
for file in file1 file2
do
grep parameters $file
done

# 3  
Old 04-01-2015
Why should either of your examples not work? They do for me. Please post error messages.
# 4  
Old 04-01-2015
Quote:
Originally Posted by speedy1354
Hello,

I am trying to grep, in bash, multiple specific files at once. I understand you can grep parameters file1 file2... fileN.

My question is how to do this when the specific files are not constant in name or quantity? (min 2 files, max 24 files)

Example 1: grep parameters file1 file2
Example 2: grep parameters file1 file2 file3 file4

I've tried using an array with [@] and a string with the files names separated by a space. Both don't work.

I do need to keep this to a single grep if possible.

Thanks!
Please show us EXACTLY what you have tried, and show us the output you got (including any diagnostic messages) in CODE tags. We can imagine ways to do what you have said that you are trying that should work perfectly.

We can also imagine lots of things you can do incorrectly. Rather than having us play guessing games, show us what you did and what results you got.

Also tell us what operating system and shell you're using!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep strings on multiple files and output to multiple files

Hi All, I want to use egrep on multiple files and the results should be output to multiple files. I am using the below code in my shell script(working in Ksh shell). However with this code I am not attaining the desired results. #!/bin/ksh ( a="/path/file1" b="path/file2" for file in... (4 Replies)
Discussion started by: am24
4 Replies

2. UNIX for Dummies Questions & Answers

Grep from multiple files by column name

I have 40 files with varying number of columns They however do have identical column names for the overlapping columns I would like to grep some info from these specific colums e.g file1 id beta se direction N 2 .5 .01 + 1000 5 -.6 .02 - 2000 file2 id ... (6 Replies)
Discussion started by: MFAB
6 Replies

3. Shell Programming and Scripting

Grep multiple strings in multiple files

Hi, every one! I have a file with multiple strings. file1 ATQRGNE ASQGVKFTE ASSQYRDRGGLET SPEQGARSDE ASSRDFTDT ASSYSGGYE ASSYTRLWNTGE ASQGHNTD PSLGGGNQPQH SLDRDSYNEQF I want to grep each string in hundreds of files in the same directory, further, I want to find out the string... (7 Replies)
Discussion started by: xshang
7 Replies

4. UNIX Desktop Questions & Answers

How do you [e]grep for multiple values within multiple files?

Hi I'm sure there's a way to do this, but I ran out of caffeine/talent before getting the answer in a long winded alternate way (don't ask ;) ) The task I was trying to do was scan a directory of files and show only files that contained 3 values: I940 5433309 2181 I tried many variations... (4 Replies)
Discussion started by: callumw
4 Replies

5. Shell Programming and Scripting

grep from multiple lines in several gz files

Hello all, I have been struggling to get grep work to my requirements. Basically I have to filter out patterns spread across multiple lines over hundreds of .gz files in a folder. And the output needs to be piped to a file. Here is the example: folder name: logs files in this folder:... (4 Replies)
Discussion started by: mandhan
4 Replies

6. UNIX for Dummies Questions & Answers

Grep multiple strings in multiple files using single command

Hi, I will use below command for grep single string ("osuser" is search string) ex: find . -type f | xarg grep -il osuser but i have one more string "v$session" here i want to grep in which file these two strings are present. any help is appreciated, Thanks in advance. Gagan (2 Replies)
Discussion started by: gagan4599
2 Replies

7. Shell Programming and Scripting

Grep in subdir, multiple files

Hi, I m trying to have script to grep a pattern in all files under a directory. I use Sun Solaris, the below want doesnt do exactly what I want. find /home/xxx/ tagHeu | while read FILE; do grep text $FILE && echo 1 grep text $FILE || echo 0 done I also tried running: find... (2 Replies)
Discussion started by: s3rro
2 Replies

8. UNIX for Dummies Questions & Answers

Grep from multiple files

Hello, I need to grep from multiple files that begin with the same text. For example, I have files named alert_audit324, alert_audit325, alert_audit326, etc. Is there a way I can use a wildcard and grep from any file that begins with alert_audit? Thanks. (1 Reply)
Discussion started by: robertson1995
1 Replies

9. UNIX for Dummies Questions & Answers

Grep from multiple files

Hello, How do I grep from multiple, specific file names. For example I have a directory that has numerous files named bsm0a.alarm_output.xxxxxx (the x's will be the date). I need to be able to grep any string that contains the acronym ACN or the word packets, but I will only need them... (4 Replies)
Discussion started by: robertson1995
4 Replies

10. UNIX for Advanced & Expert Users

grep count across multiple files

I have a number of simulation log files and I want to get a total count of the "PASSED" expression in them. If I use grep -c <files>, grep would give a tally for each file. I just want one number, the total count. How do I do that? (4 Replies)
Discussion started by: CrunchMunch
4 Replies
Login or Register to Ask a Question