Monday, December 10, 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.

Friday, April 6, 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

Saturday, February 11, 2012

Print 3 lines before and 5 lines after pattern match abcd in a file a.txt

In Linux:

awk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=3 a=5 s="abcd" a.txt

In SOLARIS:

nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=3 a=5 s="abcd" a.txt

ftp in UNIX

ftp -n 10.200.120.12 << EOF
quote user username
quote pass password
cd /path/to/directory/
prompt off
put dump.txt
exit
EOF
 

Blogger news

Blogroll