Showing posts with label perl. Show all posts
Showing posts with label perl. Show all posts

Sunday, December 9, 2012

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;

}

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

Friday, February 10, 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

Saturday, September 13, 2008

Comparing Partial strings and use of config file inside script

This shell script compares the partial strings and also says how do you use another file as config file inside the script.
========================================================

#! /bin/ksh

. `pwd`/config.txt

export RPT_DB_NAME="$1"
string='_rpt'
echo $RPT_DB_NAME | grep $string >> /dev/null
if [ $? = 0 ]
then
if [[ -e check.txt ]]
then rm check.txt
echo "Manu"
echo "Swami"
echo "File Check.txt is removed"
fi
echo "first loop"
echo "first loop"
echo "first loop"
else
echo "No Match Found"
echo "second loop"
echo "second loop"
echo "second loop"
fi
#print manu from the file config.txt
echo "$MANUSWAMI"

Invisible Password

#!/bin/ksh

/bin/stty -echo echonl
read up?password:
/bin/stty echo

# you should get user input password in this up varialbe now
# print it out to verify
echo password is "$up"