How to search then remove based on condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to search then remove based on condition
# 1  
Old 06-25-2009
Power How to search then remove based on condition

Folks;
I'm trying to write a script to scan through a directory tree then for each file it finds, it run a command line tool, then if the results include the word "DONE", it removes the file.
In more details;
i have a Linux directory tree such as "/opt/grid/1022/store"
I'm trying to write a script going through this tree and every file it finds, it should run this command on it:

/opt/dev/chktool -getstat file_name |grep -v "DONE"

If the output include the word "DONE" then we need to remove it, if not, skip to the next one.

Any help is greatly appreciated
# 2  
Old 06-26-2009
You may never get "DONE" because you are using grep -v.
If I understood your query properly, you probably need this...
Code:
for file_name in `find . -name 'pattern' -print`
do
   if [[ `/opt/dev/chktool -getstat $file_name |grep -c "DONE"` -gt 0 ]]; then
      #rm $file_name
   fi
done

# 3  
Old 06-26-2009
But i don't know the file names, I need to run the tool on any file that can be in the directory tree
# 4  
Old 06-26-2009
Try this:

Code:
for file_name in `ls /opt/grid/1022/store`
do
    if [[ ! -z `/opt/dev/chktool -getstat $file_name |grep "DONE"` ]]; then   
          rm -f ${file_name}
    fi
done

regards,
Arun.
# 5  
Old 06-26-2009
I still see "file_name" in the if statement.
I think i didn't explain it better,
What i'm looking for is to go through the directory tree, find any file under any subdirectory then run the tool on it, if the output include the word "DONE" then remove the file, if not, leave the file alone and go to the next file and do the same thing.

I hope that makes it more clear
# 6  
Old 06-26-2009
Did you even try the code that rakesh and I posted here?

regards,
Arun.
# 7  
Old 07-01-2009
I did try it but i'm getting "failed to open the next directory"

How can i make it go search every directory and subdirectory for files then perform the tool on every file it find?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove line based on condition in awk

In the following tab-delimited input, I am checking $7 for the keyword intronic. If that keyword is found then $2 is split by the . in each line and if the string after the digits or the +/- is >10, then that line is deleted. This will always be the case for intronic. If $7 is exonic then nothing... (10 Replies)
Discussion started by: cmccabe
10 Replies

2. Shell Programming and Scripting

Copy down based on condition

Hello: I need to copy down some data from the previous record in to the next record based on the below conditions If position 41- 59 of the current record is same as the previous record and the value of position 62 is not equal to 1 then copy the previous records value for positions... (1 Reply)
Discussion started by: techedipro
1 Replies

3. UNIX for Dummies Questions & Answers

Condition based on Timestamp (Date/Time based) from logfile (Epoch seconds)

Below is the sample logfile: Userids Date Time acb Checkout time: 2013-11-20 17:00 axy Checkout time: 2013-11-22 12:00 der Checkout time: 2013-11-17 17:00 xyz Checkout time: 2013-11-19 16:00 ddd Checkout time: 2013-11-21 16:00 aaa Checkout... (9 Replies)
Discussion started by: asjaiswal
9 Replies

4. Shell Programming and Scripting

Comment based on a condition

I want to comment 2 lines based on a condition. If THEN occurs immediately after WHEN then i have to comment both the lunes For example : $cat file1.txt CASE WHEN THEN 1 WHEN c1= 'I' AND c2= '0' THEN 2 So in this example i want to... (2 Replies)
Discussion started by: ashwin3086
2 Replies

5. Shell Programming and Scripting

Search based on 1,2,4,5 columns and remove duplicates in the same file.

Hi, I am unable to search the duplicates in a file based on the 1st,2nd,4th,5th columns in a file and also remove the duplicates in the same file. Source filename: Filename.csv "1","ccc","information","5000","temp","concept","new" "1","ddd","information","6000","temp","concept","new"... (2 Replies)
Discussion started by: onesuri
2 Replies

6. Shell Programming and Scripting

Condition based concatenation.

Hello, I am looking for concatenating the lines based on conditions. Below are the contents of the file: Infile: ----- Test1.PO_Itm COLUMN GAC_DT. Test1.PO_Itm COLUMN (PRODTCD ,PLNTCD). Test1.PO_Itm COLUMN PLNTCD. Test1.PO_Itm COLUMN ACTVIND. Test2.RgnToTerrtryGPI COLUMN... (3 Replies)
Discussion started by: indrajit_u
3 Replies

7. Shell Programming and Scripting

Remove lines from XML based on condition

Hi, I need to remove some lines from an XML file is the value within a tag is empty. Imagine this scenario, <acd><acdID>2</acdID><logon></logon></acd> <acd><acdID></acdID><logon></logon></acd> <acd><acdID></acdID><logon></logon></acd> <acd><acdID></acdID><logon></logon></acd> I... (3 Replies)
Discussion started by: giles.cardew
3 Replies

8. Shell Programming and Scripting

transpose based on condition

Hi, I have the oracle table coulns in an order like date, state1, state2....state9 and i need to prepare data from the script output for loading in to this table The script is #!/bin/ksh /usr/xpg4/bin/awk -F"-" '{print $2,$4}' /aemu/ErrorLogs/data/MissingCGIcount.txt |... (5 Replies)
Discussion started by: aemunathan
5 Replies

9. Shell Programming and Scripting

Read file based on condition

Hi Friends, Can any one help with this: I have a huge file with the format as A SAM 4637 B DEPT1 4758 MILAN A SMITH 46585 B DEPT2 5385 HARRYIS B SAMUL 63547 GEORGE B DANIEL 899 BOISE A FRES 736 74638 I have to read this file and write only the records that starts with "B" only ... (5 Replies)
Discussion started by: sbasetty
5 Replies

10. Shell Programming and Scripting

remove some files on a condition..

Hi.. when I do a ls -lt, I get a listing of about 200 files.. These are trace files and some of it I might not need.. To be clear, say in a given week , I might not need files that have been traced between 11 and 11:30 am on a particular day. How can I delete based on this condition ? Thanks,... (4 Replies)
Discussion started by: ST2000
4 Replies
Login or Register to Ask a Question