LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Best way to parse 1000 scripts for a string? (https://www.linuxquestions.org/questions/linux-newbie-8/best-way-to-parse-1000-scripts-for-a-string-4175504871/)

kristo5747 05-13-2014 02:41 PM

Best way to parse 1000 scripts for a string?
 
I have inherited code from a former employee and I need to identify the scripts he disabled with exit 0 at the top.

After *many* trials and errors, I finally got this to work

Code:

alan@p33 => find . -name "*.ksh" -exec sh -c "head -v -n2 '{}' | tail -v -n 1 | grep -H '^exit 0'" \;
(standard input):exit 0
(standard input):exit 0
(standard input):exit 0
(standard input):exit 0
(standard input):exit 0
(standard input):exit 0
(standard input):exit 0
(standard input):exit 0

BUT it does not display the file name even though I used `grep -H`, `head -v` and `tail -v`. How can I display the file name that matches my search pattern??

szboardstretcher 05-13-2014 02:46 PM

Just use the printf parameter of find:

Code:

find . -name "*.ksh" -printf "%f " -exec sh -c "head -v -n2 '{}' | tail -v -n 1 | grep -H '^exit 0'" \;

kristo5747 05-13-2014 02:57 PM

Quote:

Originally Posted by szboardstretcher (Post 5170160)
Just use the printf parameter of find:

Code:

find . -name "*.ksh" -printf "%f " -exec sh -c "head -v -n2 '{}' | tail -v -n 1 | grep -H '^exit 0'" \;

It does not work. It prints all the files in the directory. I have eyeballed approximately 179 files with exit 0 at the top and adding `printf` lists the whole directory instead (2000_ scripts).

szboardstretcher 05-13-2014 03:06 PM

Well, i created 2000 test files, randomly with exit 0 in the top of them, and i was able to work with the output. ie, yes they were all listed, but only those with exit 0 had the text 'standard input' next to them.

How bout this: type out the exact output you would like to see, as an example. Then we can tell you what commands to use to format your output in that way.

e.g

Code:

FILENAME THAT HAS 'exit 0' IS: test0001.ksh !!!! (standard input):exit 0
FILENAME THAT HAS 'exit 0' IS: test0221.ksh !!!! (standard input):exit 0
FILENAME THAT HAS 'exit 0' IS: test1221.ksh !!!! (standard input):exit 0


kristo5747 05-13-2014 03:15 PM

Quote:

Originally Posted by szboardstretcher (Post 5170180)
Well, i created 2000 test files, randomly with exit 0 in the top of them, and i was able to work with the output. ie, yes they were all listed, but only those with exit 0 had the text 'standard input' next to them.

How bout this: type out the exact output you would like to see, as an example. Then we can tell you what commands to use to format your output in that way.

e.g

Code:

FILENAME THAT HAS 'exit 0' IS: test0001.ksh !!!! (standard input):exit 0
FILENAME THAT HAS 'exit 0' IS: test0221.ksh !!!! (standard input):exit 0
FILENAME THAT HAS 'exit 0' IS: test1221.ksh !!!! (standard input):exit 0


I would to display something like this

load_db.ksh :exit 0
run_report.ksh :exit 0
backup_report.ksh :exit 0

That's it.

szboardstretcher 05-13-2014 03:25 PM

Ok. You can port this to ksh or sh or whatever,. but in bash(which is what i have to test with, sorry) this will work:

Code:

grep -Rn '^exit 0' *.ksh | grep ':1:\|:2:\|:3:'
Output:
Code:

test001.ksh:1:exit 0
test331.ksh:2:exit 0
test999.ksh:2:exit 0

What it does is:
  • grep -R (recursive)
  • grep -n (print line number)
  • grep '^exit 0' (exit 0 at beginning of line only)
  • grep ':1:\|:2:\|:3:' (only return IF lines 1,2, or 3.)

kristo5747 05-13-2014 03:52 PM

Quote:

Originally Posted by szboardstretcher (Post 5170197)
Ok. You can port this to ksh or sh or whatever,. but in bash(which is what i have to test with, sorry) this will work:

Code:

grep -Rn '^exit 0' *.ksh | grep ':1:\|:2:\|:3:'
Output:
Code:

test001.ksh:1:exit 0
test331.ksh:2:exit 0
test999.ksh:2:exit 0

What it does is:
  • grep -R (recursive)
  • grep -n (print line number)
  • grep '^exit 0' (exit 0 at beginning of line only)
  • grep ':1:\|:2:\|:3:' (only return IF lines 1,2, or 3.)

Brilliant!!! This did the trick!! Thank you.

grail 05-14-2014 02:29 AM

From the current examples of output it would appear all the scripts are in the current directory, so as an alternative:
Code:

awk 'FNR==2 && /exit 0/{print FILENAME,":exit 0"}' *.ksh


All times are GMT -5. The time now is 09:25 PM.