awk remove file with filenumber less then

I’m restoring a large amount of oracle archived redo logs into one directory.

The archived redolog filenames have the following names ‘1_116133_820730017.dbf’ – ‘1_{log_sequence_number}_820730017.dbf’. The log sequence number is reflected in the filename, it is the second number pair – between the first and the second underscore.

So it is like:

1_116133_820730017.dbf
is
1_<log_sequence_number>_820730017.dbf 

# log sequence number is 116133

Newest restored archived logfiles ( log seq number like 119,…) ;

$ ls -lat *.dbf  | head -n2
-rw-r-----    1 oracle   oinstall   15843840 Dec 06 13:34 1_119570_820730017.dbf 
-rw-r-----    1 oracle   oinstall   10368512 Dec 06 13:34 1_119571_820730017.dbf
$ 

Oldest restored archived logfiles ( log seq number like 116,…) ;

$ ls -lart *.dbf  | head -n2
-rw-r-----    1 oracle   oinstall   30214656 Dec 06 12:06 1_116134_820730017.dbf
-rw-r-----    1 oracle   oinstall   30930944 Dec 06 12:06 1_116133_820730017.dbf
$ 

Now I want to remove all restored archived logs that have a log sequence number less than 118133. I do not need them anymore.

The awk snippet below does that;
It extracts the log sequence number into an awk variable ‘seq_no’, does a simple if then comparison and echos the rm command if seq_no is less then 118133.

awk 'BEGIN { FS = "_"} { seq_no = $2; if ( seq_no < 118133 ) print "rm ./" $0; }'

Output of the ‘ls awk ‘:

# oldest files that can me removed 
$ ls 1_*.dbf | awk 'BEGIN { FS = "_"} { seq_no = $2; if ( seq_no < 118133 ) print "rm ./" $0; }' | head -n 3
rm ./1_116132_820730017.dbf
rm ./1_116133_820730017.dbf
rm ./1_116134_820730017.dbf
$

# newest files that can me removed 
$ ls 1_*.dbf | awk 'BEGIN { FS = "_"} { seq_no = $2; if ( seq_no < 118133 ) print "rm ./" $0; }' | tail -n 3
rm ./1_118130_820730017.dbf
rm ./1_118131_820730017.dbf
rm ./1_118132_820730017.dbf
$ 

To execute these generated rm commands pipe the output to the sh ( echo "echo 'Hellow World!'" | sh ). Before piping anything to the ‘sh’ better dubblecheck if the commands you pipe are ok.

$ ls 1_*.dbf | awk 'BEGIN { FS = "_"} { seq_no = $2; if ( seq_no < 118133 ) print "rm ./" $0; }' | sh 

Break down of awk snippet;

awk 'BEGIN { FS = "_"} { seq_no = $2; if ( seq_no < 118133 ) print "rm ./" $0; }' 

# The awk BEGIN clause comes before processing any line - I need to define the Field Seperator to be and underscore ("_" )
# instead of the default fs character which is any whitespace sequences (spaces, TABs, and newlines) 
# //www.gnu.org/software/gawk/manual/html_node/Field-Separators.html

BEGIN { 
  FS = "_"
}

# ze lines
# define seq_no variable. The archive sequence number is the 'second' field; $2
{ 
  seq_no = $2; 
  
# a simple if then; if the seq_no is less then 118133 echo the "rm command" . $0 is always the entire current line 
# //www.gnu.org/software/gawk/manual/html_node/If-Statement.html

  if ( seq_no < 118133 ) 
# please notice the "./" - to make sure ...
    print "rm ./" $0; 
} 

Leave a Reply

Helpful? - leave your note below so I can brag

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>