Monday, February 18, 2013

Find files which are older than 15 minutes in Linux and Solaris..


The below example is for moving (mv) files, same can be changes easily for rm, ls, cp etc. One has to make change in xargs paramaeter.

In LINUX:
=========

find /path_to_directory/ -name "*.rpt" -type f -mmin +15 | xargs -I {} mv {} /target_directory_path/


In Solaris:
===========

#! /usr/bin/sh

#### Create a reference file minute_test
touch /path_to_directory/minute_test

#### Make minute_test 15 minutes old
sleep 900

#### Move all .rpt files to some other directory which are older than reference file minute_test
find /path_to_directory/ -name "*.rpt" -type f -newer minute_test | xargs -I {} mv {} /target_directory_path/

#### Remove reference file
rm -f /path_to_directory/minute_test


Sunday, December 9, 2012

xargs with cp and xargs with mv command


Find with xargs and mv and cp, On public demand :)
=======================================
find . -type f  | xargs -I {} mv {} /target_directory_path/
find . -type f  | xargs -I {} cp -p {} /target_directory_path/

Print lines of file 1 which are not present in file 2. Also second field of file 2 may be different from file 1.


#!/usr/bin/perl -w
use strict;
use English qw(-no_match_vars);
die "Manadatory Parameter missing!Exiting..." if @ARGV < 2;
my ($file1, $file2) = @ARGV;
open my $file_handle1, "<", $file1 || die "Error opening $file1 - $OS_ERROR";
open my $file_handle2, "<", $file2 || die "Error opening $file2 - $OS_ERROR";
while (<$file_handle1>) {
    my $file1_line = $_;
    my $file1_id   = ($file1_line =~ /^(.+?)\;/) [0];
    my $match_found;
    while (<$file_handle2>) {
        my $file2_line = $_;
        my $file2_id   = ($file2_line =~ /^(.+?)\;/) [0];
        if ($file1_id == $file2_id) {
            $match_found = 1;
            last;
        }
    }
    seek $file_handle2, 0, 0;
    print $file1_line if ! $match_found;

}

Tuesday, April 10, 2012

Count the number of occurances of a pattern in a file using awk

awk '{ for (i=1;i<=NF;i++) if ( $i == "word" ) count++ } END{print count}' filename

This will print the count of the occurance of  a pattern "word" in a file.

Thursday, April 5, 2012

How to compare the values of a column in awk in a same file and consecutive lines..


If one would like to compare the values of 2nd column of consecutive lines of same file in such a way so that if the difference between first value and second value is more than 100 it should print complete line else ignore line.

Input File:
=======

ABC 2500
ABCD 123
XYZ 122
WXYZ 2565


Desired Output:
==========

ABC 2500 (i.e. difference between 2500 and 123 is greater than 100 here)
XYZ 122 (i.e. difference between 122 and 2565 is greater than 100 here)

Command:
=======

awk 'NR % 2 != 0 {a=$1; b=$2} NR % 2 == 0 {if (b - $2 > 100 || $2 - b > 100){print a,b}}' inputfile

Monday, February 13, 2012

Use awk in following cases


Joining two files parallely using awk:
========================
awk 'NR==FNR{a[NR]=$0; next} {print a[FNR], $0}' file1.txt file2.txt

Changing extension of all .txt to .sh:
========================
for i in *.txt;do mv "$i" "${i%.txt}".sh;done