Best way to parse 1000 scripts for a string?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Best way to parse 1000 scripts for a string?
# 1  
Old 05-13-2014
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??
# 2  
Old 05-13-2014
You're not giving grep any filenames, so it can't tell you any filenames. All it has is a list of undifferentiated lines coming from standard input (which it tells you, 'standard input'.)

How about this? awk can combine the head and tail into one program which still knows the filename:

Code:
find . -name "*.ksh" -type f -exec awk '(FNR<3) && /^exit 0/{ print FILENAME }' '+'

If your version of find doesn't have '+' you can use ';' instead but '+' would be much more efficient.
# 3  
Old 05-13-2014
Quote:
Originally Posted by Corona688
You're not giving grep any filenames, so it can't tell you any filenames. All it has is a list of undifferentiated lines coming from standard input (which it tells you, 'standard input'.)

How about this? awk can combine the head and tail into one program which still knows the filename:

Code:
find . -name "*.ksh" -type f -exec awk '(FNR<3) && /^exit 0/{ print FILENAME }' '+'

If your version of find doesn't have '+' you can use ';' instead but '+' would be much more efficient.
I must be doing something wrong

Code:
$ find . -name "*.ksh" -type f -exec awk '(FNR<3) && /^exit 0/{ print FILENAME }' '+'

This gives me
Code:
find: missing argument to `-exec'

I tried with the semi colon

Code:
$ find . -name "*.ksh" -type f -exec awk '(FNR<3) && /^exit 0/{ print FILENAME }' \;

Nothing happens. Prompt seems to hang...
# 4  
Old 05-13-2014
My mistake, if you don't have '+' you should use '{}' ';'
# 5  
Old 05-13-2014
Code:
grep -Rn '^exit 0' *.ksh | grep ':1:\|:2:'

did the trick. 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:' (only return IF lines 1 or 2.)

Last edited by Franklin52; 05-14-2014 at 04:59 AM.. Reason: Please use code tags
These 2 Users Gave Thanks to alan For This Post:
# 6  
Old 05-13-2014
Oh, they're all in the same folder? That's easy then:

Code:
awk '/^exit 0/ && (FNR<3) { print FILENAME }' *.ksh

If they're not all in the same folder, I don't think that grep is going to give you the result you want, since you're not giving it any folders to recurse into.
# 7  
Old 05-13-2014
I'd assume you want to work with those disabled scripts...

Code:
disabled=""
for F in $(find -type f -name *ksh);do
    grep -n ^exit 0|grep -q :1:\:2: && disabled+=" $F"
done
printf "${disabled/\ /\n}"

hth

EDIT:
Or is this (once again) just a too slow method?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How do you check for a particular string in 1000's of files?

Please forgive my ignorance on scripting. I am trying to determine (via a script) if a certain string of characters is present . The string that I am looking for is a constant length. Here is the string I am searching for: Model_Type={t}, ModelName={m} I need to determine if the above... (2 Replies)
Discussion started by: dlundwall
2 Replies

2. Shell Programming and Scripting

parse a mixed alphanumeric string from within a string

Hi, I would like to be able to parse out a substring matching a basic pattern, which is a character followed by 3 or 4 digits (for example S1234 out of a larger string). The main string would just be a filename, like Thisis__the FileName_S1234_ToParse.txt. The filename isn't fixed, but the... (2 Replies)
Discussion started by: keaneMB
2 Replies

3. Shell Programming and Scripting

Script to find the string contain in which file 1000 files.

Hi Greetings i have 1000 files (xmlfiles) and i need to find which file contain the string over 1000 file all file end with txt i tried with grep -c "string" *.txt i am getting an error -bash: /bin/egrep: Argument list too long i have put an script like below #!/bin/bash for line... (9 Replies)
Discussion started by: venikathir
9 Replies

4. Shell Programming and Scripting

Parse string

Hi, I need to parse a string, check if there are periods and strip the string. For example i have the following domains and subdomains: mydomain.com, dev.mydomain.com I need to strip all periods so i have a string without periods or domain extensions: mydomain, devmydomain. I use this for... (12 Replies)
Discussion started by: ktm
12 Replies

5. Shell Programming and Scripting

How to parse a string into variables

I'm working in korn shell and have a variable which contains a string like: aa_yyyymmdd_bbb_ccc_ddd.abc. I want to treat the _ and . as delimiters and parse the string so I end up with 6 values in variables that I can manipulate. My original plan was to use var1=`echo $sting1 | cut -c1-c2` but... (9 Replies)
Discussion started by: aquimby
9 Replies

6. Shell Programming and Scripting

How to parse a string efficiently

I am new to the boards and to shell programming and have a requirement to name new files received with a unique sequence number. I need to look at a particular file pattern that exists and then to increment a sequence by 1 and write the new file. Example of file names and sequence # ... (4 Replies)
Discussion started by: sandiego_coder
4 Replies

7. Shell Programming and Scripting

Parse String Using Sed

Hi, I am wondering if there's a simpler way to extract the second occurrence of a word enclosed in that matches my search criteria. Sample Input is as follows: Error installing feature - com.er.nms.cif.ist.NoMatchingUpgra Error installing feature -... (4 Replies)
Discussion started by: racbern
4 Replies

8. Shell Programming and Scripting

how to parse this string

I want to get filenames from the following input. How can I parse this in bash. input data ------------------------------------------------------------------- path=/aaa/bbb/filename1;/aaa/filename2;/aaa/bbb/ccc/ddd/filename3 -------------------------------------------------------------------... (13 Replies)
Discussion started by: hcliff
13 Replies

9. Shell Programming and Scripting

shell scripts that parse log files

hi all ,i would like a shell script that parses log files and checks the contents for any anonalities,please help,thanks (4 Replies)
Discussion started by: trueman82
4 Replies

10. Shell Programming and Scripting

How to parse large numbers of shell scripts

I am trying to parse hundreds of shell scripts to determine how they related to each other. Ideally for every script, I would get an output of: What other scripts it calls What files it reads Environment variables it accesses Any ideas on how to do this? TIA! (2 Replies)
Discussion started by: bliss
2 Replies
Login or Register to Ask a Question