Bash script to fgrep a large file. With list as source for searching.
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Bash script to fgrep a large file. With list as source for searching.
Hi,
I need to fgrep a list of things which are in a file. The file in which I will do the SEACHING is a large text file and I need fgrep to output each item from the list as a file with the item from the list as the file name.
Its kinda like this:
./script list.txt largefile.txt
output would be
jack.txt
screen.txt
blah.txt
I don't know bash all to well since I am learning it. Can anybody write this kind of thing?.
There was a guy called jack. He liked to watch tv.
But only if the tv had a large screen.
He tried to convince friends that this was the best
way but they found boring and hadly listened. To
them it sounded like blah.
Then you probably want:
Code:
fgrep -of list.txt largefile.txt | sed "s/$/.txt/"
and your script would therefore look something like:
I need to have each item from the list be a file each containing restults from the large file, essentially I want to the grab the whole line that containts something from the list. And the search items do have white spaces =/
I honestly cannot image what you are asking. Do you want to actually create files? Could you provide me an example of what list.txt and largefile.txt might look like. And if you are trying to output files, what you expect the contents of say "jack.txt" would look like after your script successfully completed.
Ok, I thought about this a little and I think I understand what you want.
Assuming list.txt contains:
Code:
jack
screen
blah
and largefile.txt contained:
Code:
There was a guy called jack. He liked to watch tv
but only if the tv had a large screen.
He tried to convince his friends to join him
but thought this sounded like a load of blah.
Hmm ... Ok I assume you wanted to make a script because you thought this would be hard but if it was me I'd probably not bother with making a script and do it as one line with the wonderful GNU Parallel.
Code:
parallel -a list.txt 'fgrep "{}" largefile.txt > "{}.txt"'
P.S. If you don't have parallel search for it in your distro's repository and install it. It is great for stuff like this and a whole lot more!
Re-reading your original request it was actually quite clear. For some reason I had presumed this was just part of some script that you were working on. I hadn't realised that you had summed up your entire requirements. Sorry for the confusion before!
Well not as clean as parallel (which I don't have either )
Consider getting it. What I did was just a really simple demo of what is possible. Check out these two introductory videos by the parallel author himself if you really want to get a glimpse of what is possible:
@tange: Wow a reply from the Parallel author himself! Thanks for the quoting tip. Yeah that might take some getting used to but I can see it would make things so much more readable when applied to more complex examples.
I'm glad you read my blog post. I had been meant to write it for a while and it was actually this thread that reminded me to do it. I only touch on the basic stuff there because the few readers I have tend to be those interested in Opera development, so I wanted to use an example that would mean something to them. Also you have covered the more powerful stuff in detail in the documentation you provide already.
P.S. Thanks for Parallel. I couldn't live without it now. I just hope a few more distros start to include it by default.
@tange: I decided to write another post. Once again my example is recursive unpacking of an archive but this time I pull apart a deb for the purpose editing and then put it back together (both times using Parallel). Hopefully this will be interesting to a wider range of people and hence encourage more people to take a look at your software.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.