Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then 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.
I'm familiar with a handful of linux/unix command line tools, but don't know enough about scripting to do this. I'm trying to put together a shell script that will do the following:
get a list of all files in a particular directory
for each file, run grep -c for two defined patterns (e.g., grep -c foo, grep -c bar)
append the results of each grep to a file with this format for each line (without brackets)
{filename} {grep pattern} {count results}
I suspect i need a 'for each' loop ... this seems like it shoudl be pretty easy to do.
This shouldn't be difficult via shell/bash scripting, but since my background is in perl, I came up with a little ditty to help point you in the right direction (this is untested but the theory should be sound):
The above could be done with perl's built-in grep function, but as I said this was a quick-and-dirty script. The above would output the # of occurrences of each word to STDOUT, where you could redirect it anywhere you'd like.
The above could be done with perl's built-in grep function, but as I said this was a quick-and-dirty script. The above would output the # of occurrences of each word to STDOUT, where you could redirect it anywhere you'd like.
this is great, thanks a ton! how would i put it to a file, though, instead of stdout?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.