LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 05-13-2014, 02:41 PM   #1
kristo5747
Member
 
Registered: Jul 2010
Location: Earth
Distribution: Ubuntu 11.04 (Natty Narwhal)
Posts: 31

Rep: Reputation: 0
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??
 
Old 05-13-2014, 02:46 PM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,208

Rep: Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611
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'" \;
 
Old 05-13-2014, 02:57 PM   #3
kristo5747
Member
 
Registered: Jul 2010
Location: Earth
Distribution: Ubuntu 11.04 (Natty Narwhal)
Posts: 31

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by szboardstretcher View Post
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).
 
Old 05-13-2014, 03:06 PM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,208

Rep: Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611
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

Last edited by szboardstretcher; 05-13-2014 at 03:08 PM.
 
Old 05-13-2014, 03:15 PM   #5
kristo5747
Member
 
Registered: Jul 2010
Location: Earth
Distribution: Ubuntu 11.04 (Natty Narwhal)
Posts: 31

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by szboardstretcher View Post
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.
 
Old 05-13-2014, 03:25 PM   #6
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,208

Rep: Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611
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.)

Last edited by szboardstretcher; 05-13-2014 at 03:27 PM.
 
Old 05-13-2014, 03:52 PM   #7
kristo5747
Member
 
Registered: Jul 2010
Location: Earth
Distribution: Ubuntu 11.04 (Natty Narwhal)
Posts: 31

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by szboardstretcher View Post
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.
 
Old 05-14-2014, 02:29 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,513

Rep: Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891
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
 
  


Reply

Tags
find, grep, ksh


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
Parse string using bash StrongIce Linux - Newbie 4 08-07-2012 03:46 AM
how to parse the xml string Dyuri Linux - Software 2 09-03-2008 08:49 AM
parse a string in perl 2007fld Programming 13 08-07-2007 02:41 PM
c++ parse date string?? blizunt7 Programming 25 05-18-2006 03:12 PM
Parse a perl string djgerbavore Programming 3 10-31-2004 07:23 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:30 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