Conditional delete


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditional delete
# 22  
Old 08-30-2018
OK, my fault, I may have taken too much for granted. Pls. modify as
Code:
                 if (T[1] == KnownTime)         {print "echo rm " LastFile
                                                 print SUM += $2 * $3
                                                 if (SUM >= Needed) exit
                                                }
                 KnownTime = T[1]
                 LastFile = $1
                }

# 23  
Old 08-30-2018
Quote:
Originally Posted by RudiC
OK, my fault, I may have taken too much for granted. Pls. modify as
Code:
                 if (T[1] == KnownTime)         {print "echo rm " LastFile
                                                 print SUM += $2 * $3
                                                 if (SUM >= Needed) exit
                                                }
                 KnownTime = T[1]
                 LastFile = $1
                }

First of thank you very much for all you patience Smilie with me
this thread become hottest thread.. Smilie


Here the modified the code

Code:
[asp@abcsd34 ~]$ cat abc7.sh
{ cd /home/asp; stat -fc"%b %a %S" .; stat -c"%n %b %B" 2018*.tar.gz | sort; } | awk '
NR == 1         {print Needed = ($1 * PCT - $2) * $3
                 next
                }

                {split ($1, T, "_")
                 if (T[1] == KnownTime)
                                   {print "echo rm " LastFile
                    print SUM += $2 * $3
                    if (SUM >= Needed) exit
                    }
                 KnownTime = T[1]
                 LastFile = $1
                }
' PCT=0.4
[asp@abcsd34 ~]$ sh abc7.sh
5.63364e+09
echo rm 20180701_0012.tar.gz
251428864
echo rm 20180701_0712.tar.gz
502857728
echo rm 20180701_1412.tar.gz
754286592
echo rm 20180702_0012.tar.gz
1005715456
echo rm 20180702_0712.tar.gz
1257144320
echo rm 20180702_1412.tar.gz
1508573184
echo rm 20180703_0012.tar.gz
1760002048
echo rm 20180703_0712.tar.gz
2011430912
echo rm 20180703_1412.tar.gz
2262859776
echo rm 20180704_0012.tar.gz
2514288640
echo rm 20180704_1412.tar.gz
2765717504
echo rm 20180705_0012.tar.gz
3017146368
echo rm 20180705_1412.tar.gz
3268575232
echo rm 20180706_0012.tar.gz
3520004096
echo rm 20180706_1412.tar.gz
3771432960
echo rm 20180707_0012.tar.gz
4022861824
echo rm 20180707_1412.tar.gz
4274290688
echo rm 20180708_0012.tar.gz
4525719552
echo rm 20180708_1412.tar.gz
4777148416
echo rm 20180709_0012.tar.gz
5028577280
echo rm 20180709_1412.tar.gz
5280006144
echo rm 20180710_0012.tar.gz
5531435008
echo rm 20180710_1412.tar.gz
5782863872
[asp@abcsd34 ~]$

Now its showing correctly first 3 files of the day in echo and skipping one which needs to be retained.
# 24  
Old 08-30-2018
Looks like your problem is solved? If you REALLY trust it, remove echo and pipe through sh to make it fully functional.


As Peasant in post#16 proposed, you may want to increase the compression factor of your backup gzip to save some space?
# 25  
Old 08-30-2018
Quote:
Originally Posted by RudiC
Looks like your problem is solved? If you REALLY trust it, remove echo and pipe through sh to make it fully functional.


As Peasant in post#16 proposed, you may want to increase the compression factor of your backup gzip to save some space?
Sorry at my poor knowledge...did not get you mean by pipe through sh...
can you give small example please...

" If you Really trust" --haha no other go, i need to test it for sure whether it will come correct or not.. A Big thank you for your help
# 26  
Old 08-30-2018
I'm confused about a couple of points...
1st: In the pipeline feeding into awk:
Code:
stat -c"%n %b %B" 2018*.tar.gz | sort;

why is the sort needed? The shell will expand 2018*.tar.gz in increasing sorted order by the date and time stamps included in the filenames matched by that pattern and sort should not change that order.

2nd: In post #8, it was pointed out that the filename matching pattern I suggested in post #4 has a Y3K problem (i.e., would stop working in January, 3000). Using the filename matching pattern shown above (i.e., 2018*.tar.gz, the code will stop working in January, 2019. Do you really want to use a pattern that only matches files created this year?
# 27  
Old 08-30-2018
Quote:
Originally Posted by Don Cragun
I'm confused about a couple of points...
1st: In the pipeline feeding into awk:
Code:
stat -c"%n %b %B" 2018*.tar.gz | sort;

why is the sort needed? The shell will expand 2018*.tar.gz in increasing sorted order by the date and time stamps included in the filenames matched by that pattern and sort should not change that order.

2nd: In post #8, it was pointed out that the filename matching pattern I suggested in post #4 has a Y3K problem (i.e., would stop working in January, 3000). Using the filename matching pattern shown above (i.e., 2018*.tar.gz, the code will stop working in January, 2019. Do you really want to use a pattern that only matches files created this year?
regarding 2 nd point, obviously I need this to work after 2018 as well Smilie .
# 28  
Old 08-30-2018
Quote:
Originally Posted by onenessboy
Sorry at my poor knowledge...did not get you mean by pipe through sh...
can you give small example please...

Did you read and understand Don Cragun's post#4?



Quote:
Originally Posted by Don Cragun
I'm confused about a couple of points...
1st: In the pipeline feeding into awk:
Code:
stat -c"%n %b %B" 2018*.tar.gz | sort;

why is the sort needed? The shell will expand 2018*.tar.gz in increasing sorted order by the date and time stamps included in the filenames matched by that pattern and sort should not change that order.

Understood - I wasn't sure stat's output would be sorted although the shell expands / presents the file names sorted.


Quote:
2nd: In post #8, it was pointed out that the filename matching pattern I suggested in post #4 has a Y3K problem (i.e., would stop working in January, 3000). Using the filename matching pattern shown above (i.e., 2018*.tar.gz, the code will stop working in January, 2019. Do you really want to use a pattern that only matches files created this year?
Quote:
Originally Posted by onenessboy
regarding 2 nd point, obviously I need this to work after 2018 as well Smilie .

The proposal (as mainly all of my proposals) is to be read as a "proof of concept" and not as a "turnkey system". It does NOT relieve the user of developing a deep understanding and thoroughly checking / testing / adapting it before applying to real world - even less to production - systems. Please adapt to your needs, e.g. by reading, understanding, and applying other posts in this thread.

Last edited by RudiC; 08-30-2018 at 05:41 AM..
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conditional delete -- New glitch

Hi Please dont consider this as duplicated post.. I am using below pattern to find delete files to bringdown disc size.. however how i can make sure ist going to correct folder and searching for files... while print "echo rm " LastFile correctly print files names for deletion, but when i... (7 Replies)
Discussion started by: onenessboy
7 Replies

2. UNIX for Beginners Questions & Answers

(g)awk conditional substitution issues when attempting to delete character

A portion of my input is as follows: 1087 IKON01,49 A WA- -1 . -1 . 0 W WA- -1 . -1 . 0 . -1 . -1 -1 -1 -1 -1 -1 W 1088 IKON01,49 A J.@QU80MW. 2... (2 Replies)
Discussion started by: jvoot
2 Replies

3. Shell Programming and Scripting

Conditional execution

Here's an interesting (to me, anyway) little puzzle. Background: I have a process that restores a number of large(ish) files from tape backup. As an individual file is being written, it is owned by root, then the ownership is changed when that file is complete. Since this process can take... (3 Replies)
Discussion started by: edstevens
3 Replies

4. Shell Programming and Scripting

Conditional search and delete using SED / Shell script

Hi, I want to perform a conditional search and remove my search string. Input string: "abcdaabcadgfaarstab" Character to search: "a" Condition: Remove all "a" in the input string except if it is "aa" Output string: "bcdaabcdgfaarstb" Can you please help me in this? (5 Replies)
Discussion started by: dominiclajs
5 Replies

5. Shell Programming and Scripting

if conditional statement

Hi, I have a script like this: sample.sh mapping=$1 if then echo "program passed" fi I'm running the above script as ./sample.sh pass The script is not getting executed and says "integer expression expected" Could anyone kindly help me? (2 Replies)
Discussion started by: badrimohanty
2 Replies

6. Shell Programming and Scripting

conditional statement

Hi all, The following code is to find if a list of numbers from one file are within the range in another file. awk -F, '\ BEGIN { while ((getline < "file2") > 0) file2=$3 } {for (col1 in file2) if ($0>=30 && $1<=45) print $0} ' FILE1 But where I have the number 30 and 45, I... (3 Replies)
Discussion started by: dr_sabz
3 Replies

7. Shell Programming and Scripting

If conditional

Hi, I am new to unix and shell scripting.In my script,there is a line using the "if" conditional - if && ; then do something Here "x" is a variable holding string value.If it is not equal to a comma or a string,only then I want to enter the "if" loop. But I am getting error while... (12 Replies)
Discussion started by: abhinavsinha
12 Replies

8. UNIX for Dummies Questions & Answers

If conditional

Hi, I am new to unix and shell scripting.In my script,there is a line using the "if" conditional - if && ; then do something Here "x" is a variable holding string value.If it is not equal to a comma or a string,only then I want to enter the "if" loop. But I am getting error while... (1 Reply)
Discussion started by: abhinavsinha
1 Replies

9. UNIX for Dummies Questions & Answers

conditional

conditional is not wworking can any one figure out what goes wrong xx1=`$ORACLE_HOME/bin/sqlplus -s apps/ostgapps1 2>/dev/null << EOF WHENEVER SQLERROR EXIT 1 set head off feedback off ; WHENEVER SQLERROR EXIT SQL.SQLCODE; select count(*) from CMS_INVOICE_ALL... (2 Replies)
Discussion started by: u263066
2 Replies

10. Shell Programming and Scripting

Conditional Statements

How can I compare two decimal values within a function using Bash? Function fun2 isn't comparing the decimal values. Is there a way to do this using Bash or Korn? #!/bin/bash set -x x=1 z=110 function fun1() { i=`bc << EOF 2>> /dev/null scale=3 ... (1 Reply)
Discussion started by: cstovall
1 Replies
Login or Register to Ask a Question