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
# 8  
Old 12-26-2012
Use /usr/xpg4/bin/awk instead for Solaris or SunOS
Code:
/usr/xpg4/bin/awk 'NR==FNR{ a[$0]=1;next } {n=0; for(i in a) { if($0~i) { print $0 >> i".txt" }}}' stringsfile.txt $file

# 9  
Old 12-26-2012
Quote:
Originally Posted by bipinajith
Use /usr/xpg4/bin/awk instead for Solaris or SunOS
Code:
/usr/xpg4/bin/awk 'NR==FNR{ a[$0]=1;next } {n=0; for(i in a) { if($0~i) { print $0 >> i".txt" }}}' stringsfile.txt $file


Sorry to play message tag with you... I'm on a MAC, obviously using OSX. I read that creating awk files would be best and using BEGIN and END seem to be pertinent. However, awk is very confusing to me and I'm not sure where to go with this one.

"/usr/xpg4/bin/awk" is a bad interpretor.


# 10  
Old 12-26-2012
I'm not sure about OSX.

I tested this code in following systems and it works fine
  • gawk in GNU/Linux
  • awk in HP-UX
  • /usr/xpg4/bin/awk in SunOS
This User Gave Thanks to Yoda For This Post:
# 11  
Old 12-26-2012
The trouble is with:
Code:
{ print $0 >> i".txt" }

Try:
Code:
{ f=i ".txt"; print>f }


Last edited by Scrutinizer; 12-26-2012 at 05:21 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 12  
Old 12-27-2012
Quote:
Originally Posted by Scrutinizer
The trouble is with:
Code:
{ print $0 >> i".txt" }

Try:
Code:
{ f=i ".txt"; print>f }

That's awesome. I had some things on my end to fix. But finally got it working, thanks to y'all. I appreciate the help.

While it finally works, I'd still like to know what I'm typing, which most of it makes sense. But why wouldn't this work:
Code:
{ print $0 >> i".txt" }

But this did:
Code:
{ f=i ".txt"; print>f }

A quick explanation would suffice, if anything. I'm just making sure the coding sticks, I would hate to ask the same question twice.

Thanks again!

-David
# 13  
Old 12-27-2012
I think for some awks it is ambiguous what is the target of the redirection. To remove this ambiguity, you could either assign the composition of the filename to a variable first, like I suggested, or use brackets:
Code:
{ print > ( i ".txt" ) }


Last edited by Scrutinizer; 12-27-2012 at 04:26 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 14  
Old 01-04-2013
Scrutinizer,

This code was given to me in this thread. I'm now going through some modifications to make this logic work with other files. Would you be able to take me through the code, piece by piece, so I can understand how to modify/create code for different things using awk.


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




I tried using code with a file that contains a list of things to search for. As I'm running it, it states the following for one file to be created:

awk: XYZ.txt makes too many open files
input record number 4845, file XXXXXX.files
source line number 1

xyz.txt is the file being created
XXXXX.files is the files being looked at

Source line 1 of the script looks like this
Code:

for file in /directory/*.files




This works with the original file I used for terms to search for, but when using a different one, with a longer list, I get those errors and almost empty created files.

Any help is appreciated. My real goal is to understand the code and be able to modify it myself if needed.

Thanks a bunch!

-David

Last edited by Scrutinizer; 01-04-2013 at 05:07 PM.. Reason: CODE ENCLOSING; mod: moved code tags
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