Bash Scipting (New); Run multiple greps > multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Scipting (New); Run multiple greps > multiple files
# 1  
Old 12-21-2012
Bash Scipting (New); Run multiple greps > multiple files

Hi everyone,

I'm new to the forums, as you can probably tell... I'm also pretty new to scripting and writing any type of code.

I needed to know exactly how I can grep for multiple strings, in files located in one directory, but I need each string to output to a separate file.

So I'd imagine I'd make a separate text file with each string that I want to grep. ex:
Code:
food1
food2
food3

.

Then i would use a loop function to grep all files in directory /Desktop/Blah/*, and output to a file named "the string i grepped for".

I would greatly appreciate guidance on this, as loops confuse the hell outa me and i'm not sure what "x" means in the following:

Code:
for x in 'cat /Desktop/Blah/*'; grep 'point to strings file' ..........

Where would $x go? Any guidance is helpful. Thanks everyone....


-David

Last edited by Scrutinizer; 12-26-2012 at 09:52 AM.. Reason: code tags
# 2  
Old 12-21-2012
Just to give you an idea, here is a sample script:-
Code:
#!/bin/bash

for file in /Desktop/Blah/*              # For each file in directory: /Desktop/Blah/
do
        grep food1 $file >> food1.txt    # Grep for food1, redirect & append o/p to file: food1.txt
        grep food2 $file >> food2.txt    # Grep for food2, redirect & append o/p to file: food2.txt
        grep food3 $file >> food3.txt    # Grep for food3, redirect & append o/p to file: food3.txt
done

# 3  
Old 12-22-2012
You could also use egrep if loops are confusing.

Code:
cat file | egrep '(food1|food2|food3)'

However, this may not be the format you want. Either way, knowing both methods will be good tools for later.
# 4  
Old 12-26-2012
bipinajith, Azrael:

Thank you both for the replies. I understand very well the examples you have both posted. If we can make things a little more complex, but at the same time simple in regards to automating weekly tasks, that's the goal of mine...

Using the example code:
Code:
#!/bin/bash  for file in /Desktop/Blah/*              # For each file in directory: /Desktop/Blah/ do         grep food1 $file >> food1.txt    # Grep for food1, redirect & append o/p to file: food1.txt         grep food2 $file >> food2.txt    # Grep for food2, redirect & append o/p to file: food2.txt         grep food3 $file >> food3.txt    # Grep for food3, redirect & append o/p to file: food3.txt done

I'll have MULTIPLE (80+) strings to grep and will increase over time.

Adding/Editing the following code, how can I have files created, that are named after the string "grepped".

Code:
#!/bin/bash

for file in /Directory/*txt

do 

     grep -f mystringsfile.txt >> "the string.txt" #This would be an automatically assigned name. I'm guessing a variable would be needed here. How would it be written?

done


Last edited by Scrutinizer; 12-26-2012 at 09:53 AM.. Reason: code tags
# 5  
Old 12-26-2012
Use awk instead:
Code:
for file in /Directory/*txt
do
  awk 'NR==FNR{ a[$0]=1;next } { n=0; for(i in a) { if($0~i) { print $0 >> i".txt" }}}' mystringsfile.txt $file
done

# 6  
Old 12-26-2012
It'd take a lot of time to run 80 greps on every file. If you read in your word file into an array you can do it at once, depending on how many files and if you need to do subdirectories.

If not many words/files, you can try:

Code:
#!/bin/bash
wordfile=words.lst

# read in fixed strings from word file
while IFS= read -r line; do
        words+=('-e')
        words+=("$line")
done < "$wordfile"

echo grep -F "${words[@]}" -- dir1/*
mute@clt:~/temp/LDHB2012$ ./script
grep -F -e one -e string with spaces -e foo -e bar -- dir1/file1 dir1/file2 dir1/file3

remove the 'echo' and adjust as needed.

edit: oops. sorry. this doesn't meet the requirement of outputting to separate files. while I wasn't looking an awk solution was posted. That'd probably be best.
# 7  
Old 12-26-2012
Quote:
Originally Posted by bipinajith
Use awk instead:
Code:
for file in /Directory/*txt
do
  awk 'NR==FNR{ a[$0]=1;next } { n=0; for(i in a) { if($0~i) { print $0 >> i".txt" }}}' mystringsfile.txt $file
done

Alright. I tried what you put here. I changed the .txt file to the actual file with the strings and changed the actual directory with the files to be searched.

Everything else I kept the same. It looks like this:

Code:
#!/bin/bash
for file in /ActualDirectory/*.txt

do

     awk 'NR==FNR{ a[$0]=1;next } {n=0; for(i in a) { if($0~i) { print $0 >> i".txt" }}}' stringsfile.txt $file

done

Here's my error:
Code:
awk: syntax error at source line 1
   context is 
               NR==FNR{ a[$0]=1;next } {n=0; for(i in a) { if($0~i) { print $0 >> >>> i".txt" <<<
awk: illegal statements at source line 1
   context is 
               NR==FNR{ a[$0]=1;next } {n=0; for(i in a) { if($0~i) { print $0 >> >>> i".txt" <<<
awk: syntax error at source line 1

-David
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Combining multiple greps

I'm trying to learn about regular expressions. Let's say I want to list all the files in /usr/bin beginning with "p", ending with "x", and containing an "a". I know this works:ls | grep ^p | grep x$ | grep abut I'm thinking there must be a way to do it without typing grep three times. Some of my... (9 Replies)
Discussion started by: Xubuntu56
9 Replies

2. Shell Programming and Scripting

Ssh to multiple hosts and then run multiple for loops under remote session

Hello, I am trying to login to multiple servers and i have to run multiple loops to gather some details..Could you please help me out. I am specifically facing issues while running for loops. I have to run multiple for loops in else condition. but the below code is giving errors in for... (2 Replies)
Discussion started by: mohit_vardhani
2 Replies

3. UNIX for Dummies Questions & Answers

Run script on multiple files

Hi Guys, I've been having a look around to try and understand how i can do the below however havent come across anything that will work. Basically I have a parser script that I need to run across all files in a certain directory, I can do this one my by one on comand line however I... (1 Reply)
Discussion started by: mutley2202
1 Replies

4. Shell Programming and Scripting

Run script on multiple files

I have a script that I need to run on one file at a time. Unfortunately using for i in F* or cat F* is not possible. When I run the script using that, it jumbles the files and they are out of order. Here is the script: gawk '{count++; keyword = $1} END { for (k in count) {if (count == 2)... (18 Replies)
Discussion started by: newbie2010
18 Replies

5. Shell Programming and Scripting

Run one script on multiple files and print out multiple files.

How can I Run one script on multiple files and print out multiple files. FOR EXAMPLE i want to run script.pl on 100 files named 1.txt ....100.txt under same directory and print out corresponding file 1.gff ....100.gff.THANKS (4 Replies)
Discussion started by: grace_shen
4 Replies

6. UNIX for Dummies Questions & Answers

Run one script on multiple files and print out multiple files.

How can I run the following command on multiple files and print out the corresponding multiple files. perl script.pl genome.gff 1.txt > 1.gff However, there are multiples files of 1.txt, from 1----100.txt Thank you so much. No duplicate posting! Continue here. (0 Replies)
Discussion started by: grace_shen
0 Replies

7. Shell Programming and Scripting

Run perl script on files in multiple directories

Hi, I want to run a Perl script on multiple files, with same name ("Data.txt") but in different directories (eg : 2010_06_09_A/Data.txt, 2010_06_09_B/Data.txt). I know how to run this perl script on files in the same directory like: for $i in *.txt do perl myscript.pl $i > $i.new... (8 Replies)
Discussion started by: ad23
8 Replies

8. Shell Programming and Scripting

How to run multiple awk files

I'm trying some thing like this. But not working It worked for bash files Now I want some thing like that along with multiple input files by redirecting their outputs as inputs of next command like below Could you guyz p0lz help me on this #!/usr/bin/awk -f BEGIN { } script1a.awk... (2 Replies)
Discussion started by: repinementer
2 Replies

9. Shell Programming and Scripting

Run Multiple Functions over SSH (BASH)

I am trying to write a script that will ssh into a remote machine and recurse through a specified directory, find mp3 files which may be two or three directories deep (think iTunes: music/artist/album/song.mp3), and scp them back to the machine running the script. The script should also maintain... (3 Replies)
Discussion started by: johnnybg00de
3 Replies

10. UNIX for Dummies Questions & Answers

when I try to run rm on multiple files I have problem to delete files with space

Hello when I try to run rm on multiple files I have problem to delete files with space. I have this command : find . -name "*.cmd" | xargs \rm -f it doing the work fine but when it comes across files with spaces like : "my foo file.cmd" it refuse to delete it why? (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question