LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-29-2019, 01:11 PM   #1
bruinshockey
Member
 
Registered: Dec 2010
Location: Chilliwack, BC
Distribution: Slackware 14.1 64 Bit
Posts: 34

Rep: Reputation: 10
Trying to figure out a grep command


I'm trying to find a grep command or something similar that will do the following

1. find a string amongst files in separate folders
string is <errorcode>86
each location has it's own folder 057, 068, etc
file names end with the extension of .dg

2. create a new grep output file per folder.

is this doable? I've been trying to wrap my head around it. I can do it folder by folder, but with 390-400 folders that would be fairly time consuming. If there is a way to automate it that would be awesome.

any suggestions are appreciated!
 
Old 03-29-2019, 01:20 PM   #2
1nuxg33k
Member
 
Registered: Feb 2019
Location: PNW
Distribution: Debian, CentOS, OpenSuse, Mint, LMDE
Posts: 74

Rep: Reputation: 30
It's doable. Search for your string in the folders with * then pipe that to a new folder to view the contents.
What have you tried so far?
 
Old 03-29-2019, 01:45 PM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,316
Blog Entries: 13

Rep: Reputation: 3703Reputation: 3703Reputation: 3703Reputation: 3703Reputation: 3703Reputation: 3703Reputation: 3703Reputation: 3703Reputation: 3703Reputation: 3703Reputation: 3703
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.

I agree with 1nuxg33k in that it is very doable, and that we'd like to see what you have considered or tried.

Where is your understanding breaking down about this problem?

You say you can perform grep, have you tried to use grep to search recursively down a tree of files? Have you reviewed the grep manual page for information as to how to do this?

What is your experience with regular expressions as well as script writing?

There may or may not be a way to re-direct the grep output, representing many sub-directories, to various file names, all using one complex command. I personally would have to work to see how re-directing output, conditionally, to different files could be done via bash and regex.

There absolutely are ways to write a bash, or other type of script which will take a bunch of output and re-direct it according to how you wish to have that come out.

Please update your fellow forum members with some added information about how far along you are with this problem and your expertise with things like scripting and regular expressions.
 
Old 03-29-2019, 04:43 PM   #4
/dev/random
Member
 
Registered: Aug 2012
Location: Ontario, Canada
Distribution: Slackware 14.2, LFS-current, NetBSD 6.1.3, OpenIndiana
Posts: 319

Rep: Reputation: 108Reputation: 108
Quote:
Originally Posted by bruinshockey View Post
I'm trying to find a grep command or something similar that will do the following

1. find a string amongst files in separate folders
string is <errorcode>86
each location has it's own folder 057, 068, etc
file names end with the extension of .dg

2. create a new grep output file per folder.

is this doable? I've been trying to wrap my head around it. I can do it folder by folder, but with 390-400 folders that would be fairly time consuming. If there is a way to automate it that would be awesome.

any suggestions are appreciated!
Are these directories a subdirectory of something? You could do this...

Code:
grep -r "<errorcode>86" /path/to/directories
 
Old 03-29-2019, 06:05 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,396

Rep: Reputation: 3048Reputation: 3048Reputation: 3048Reputation: 3048Reputation: 3048Reputation: 3048Reputation: 3048Reputation: 3048Reputation: 3048Reputation: 3048Reputation: 3048
Quote:
Originally Posted by bruinshockey View Post
2. create a new grep output file per folder.

... I can do it folder by folder, but with 390-400 folders that would be fairly time consuming.
You'll need to wrap a script around it to handle that condition. How awkward will depend on the dir structure.
 
Old 03-29-2019, 06:59 PM   #6
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: A few
Posts: 3,964

Rep: Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110
To iterate over a list of folders:
Code:
for f in LIST-OF-FOLDERS
do grep '<errorcode>86' $f/*.dg > $f.out
done
As a first approach, you can use find to generate the list. If it’s too long, you can put the grep into the find command, using its exec option.

This requires more work if folder names contain characters that are interpreted by the shell, such as blanks.

Last edited by berndbausch; 03-30-2019 at 03:58 AM. Reason: corrected "smart" quotes
 
Old 03-29-2019, 08:21 PM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ Win10
Posts: 9,510

Rep: Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036
Code:
#!/bin/bash

while read f
do
echo "$f"
    grep -rwn "$f" -e  "<errorcode>86"
done < dirlist
dirlist consist of absolute paths to parent dir.
Code:
 find  /media/data1 -mindepth 1 -maxdepth 1 -type d > dirlist
test out put
Code:

$ ./grepped
/media/data1/offSDCard
/media/data1/feh
/media/data1/feh/man/Insuddir:15: here I am <errorcode>86 yo
/media/data1/torrents
/media/data1/HoldingImages
/media/data1/EtermBGs
/media/data1/musicscript
/media/data1/musicscript/oneCopysortbymetatag:15: here I am <errorcode>86 yo
/media/data1/aeskulap-1.0
2. create a new grep output file per folder.

Last edited by BW-userx; 03-29-2019 at 08:54 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 03:07 PM
[SOLVED] Trying to figure out a date command thebeav Linux - Newbie 5 01-14-2011 12:56 AM
omg i cant figure out grep or sed ... dave247 Debian 21 10-24-2008 08:07 AM
trying to figure out some output from grep naijaguy Linux - Newbie 8 08-17-2004 01:44 PM
I am trying to figure out dependecies newmandrakeuser Linux - Newbie 13 12-06-2002 01:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration