Load find pattern from a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Load find pattern from a file?
# 1  
Old 06-04-2015
Load find pattern from a file?

Right now I have a bunch case statement wrapped around multiple groups of patterns. I want to make each pattern end-user definable in files instead. Can the bold parts be imported from a file simply? Code injection attack is not a big concern since there is access control on the share drive but if I have a choice in doing it in a relatively safe way, I'd prefer that.
Code:
find "$dataDir" \(    -iname 'redacted_1' \
                      -o -iname 'redacted_2' \
                      -o -iname 'redacted_3' \
                      -o -iname 'redacted_4' \) |
                           > "$scratchDir"file_list.txt

Mike
# 2  
Old 06-05-2015
Quote:
Originally Posted by Michael Stora
Can the bold parts be imported from a file simply?
Depends, if there are always 4 values, and i assume its those redacted_{1..4} that would change, then yes its simple.
Handling an unknown is a little more complex.

pattern_file.txt
Code:
redacted_1 redacted_2 redacted_3 redacted_4
redacted_21 redacted_22 redacted_23 redacted_24
redacted_31 redacted_32 redacted_33 redacted_34

(untested)
Code:
while read r1 r2 r3 r4
do 	find "$dataDir" \
		( -iname '$r1' \
                  -o -iname '$r2' \
                  -o -iname '$r3' \
                  -o -iname '$r4' ) |
                  >> "$scratchDir"file_list.txt
done<pattern_file.txt

hth
# 3  
Old 06-05-2015
Quote:
Originally Posted by sea
Depends, if there are always 4 values, and i assume its those redacted_{1..4} that would change, then yes its simple.
Handling an unknown is a little more complex.

pattern_file.txt
Code:
redacted_1 redacted_2 redacted_3 redacted_4
redacted_21 redacted_22 redacted_23 redacted_24
redacted_31 redacted_32 redacted_33 redacted_34

(untested)
Code:
while read r1 r2 r3 r4
do     find "$dataDir" \
        ( -iname '$r1' \
                  -o -iname '$r2' \
                  -o -iname '$r3' \
                  -o -iname '$r4' ) |
                  >> "$scratchDir"file_list.txt
done<pattern_file.txt

hth
No, it can be an arbitrary list of filters for find (not necessarily 4 and not necessarily -iname).

Mike
# 4  
Old 06-05-2015
Wouldnt it then be easier to sum up all filters and then run either run one by one or all at once?
# 5  
Old 06-05-2015
I'm not currently doing any iteration, only one find command. I don't particularly want iteration because that might cause duplicate entries. The multiline syntax \) ... )\ is just for human readability and does not change how find parses the filter.

Mike

Last edited by Michael Stora; 06-05-2015 at 03:14 AM..
# 6  
Old 06-05-2015
Try (untested):
Code:
PATTERN=""
isFirst=true
while read pattern;do 
	$isFirst && \
		PATTERN+=" -iname \"$pattern\"" && \
		isFirst=false || \
		PATTERN+=" -o -iname \"$pattern\""
done<pattern_file.txt

find "$dataDir" $PATTERN >> "$scratchDir"file_list.txt

hth
# 7  
Old 06-05-2015
The OP wants a program which will generate find statement(s).

For instance, an awk or shell program (or any other) could generate find statements.
But you need to define proper conditions for it.

Exact conditions like input file format, error handling desired (like possible timeouts,file locking, other processes on files in question etc.), exact find switches you wish you use in what scenario.

Just reading find switches from files does not bode well Smilie

Can you post your entire code with a short explanation what you wish to change ?
These 2 Users Gave Thanks to Peasant For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. Shell Programming and Scripting

How to find a file with a specific pattern for current sysdate & upon find email the details?

I need assistance with following requirement, I am new to Unix. I want to do the following task but stuck with file creation date(sysdate) Following is the requirement I need to create a script that will read the abc/xyz/klm folder and look for *.err files for that day’s date and then send an... (4 Replies)
Discussion started by: PreetArul
4 Replies

3. Shell Programming and Scripting

Find file of particular pattern

Hi All, I have a file PSU_ 20130805_201308041234522 i want to search this file where variable day=20130805 and curday=20130804 after currday date some numbers will be added.how to search this file by using day and curday. Thanks in advance. (3 Replies)
Discussion started by: pracheth
3 Replies

4. Shell Programming and Scripting

how to find a pattern from an external file in a directory containing multiple file recursively

Hi, Need your help in this. I have an input file that has multiple enrollment_number, somewhat like 1234567 8901234 9856321 6732187 7623465 Now i have to search and delete these enrollment_number recursively from all the files that are within multiple sub-directories of a... (10 Replies)
Discussion started by: mukulverma2408
10 Replies

5. Shell Programming and Scripting

Shell script to find specific file name and load data

I need help as to how to write a script in Unix for the following: We have 3 servers; The mainframe will FTP them to a folder. In that folder we will need the script to look and see if the specific file name is there and load it to the correct table. Can anyone pls help me out with... (2 Replies)
Discussion started by: msrahman
2 Replies

6. Shell Programming and Scripting

To find a pattern in file

Hi, I would like to find a pattern in a file as follows: I would like to find "or" "OR" "and" "AND" between two numeric values. I have tried this: grep '**or*' But does not work. Appreciate help on this. (4 Replies)
Discussion started by: pinnacle
4 Replies

7. Shell Programming and Scripting

How to find pattern in file names?

I have some files, those are abbreviated (ed,ea, and bi) company_ed_20100719.txt company_ea_20100719.txt company_bi_20100719.txt I would like to rename these files by replacing ed with EmployeeDetails ea with EmployeeAddress bi with BankInfomration as company_... (3 Replies)
Discussion started by: LinuxLearner
3 Replies

8. Shell Programming and Scripting

Find a pattern in a file at a particular location

Hi all, I have a question on how to search for a pattern in a file and return a value if it is present at that particular location. How to read each line and each character for the pattern in the file of any format. Eg for the file format: attached the file (1 Reply)
Discussion started by: sparks
1 Replies

9. Shell Programming and Scripting

How to find this pattern in a file

hi all i have a file in my box, which is so huge and full file is in a single line. In this file i have to look for a pattern "ABC01234567" In this above mentioned pattern ABC is fixed and number might change . it will a eight digit random number Thanks so much for all you help ... (1 Reply)
Discussion started by: Prateek007
1 Replies
Login or Register to Ask a Question