shell programming

Hi iam new to shell programming. I would like to ask one dought abt the file
handling in unix.

Iam having a file1 as follows:

ASDERFCX1234567890123456
POIUYTRE0098765432123456
BVCXCVBN0955644411111111

File2

ASDERFCX1234567890123456  kill@abc.com                                                                                          2008-02-020005009.55 0000000 00100.00 2008-03-01 JILL,POWER J                              000000000000006.16 000000000000000.00 CBB00010000000911

The above is a single line in file2. there are many lines like this.

So i need to check compare the file1 with file2(1st column). If that info matches in file 2 then i need to paste file 1 in file2 1st column and othe other coulumn in file2 should remain same without any space modifications.

Most important is records present in file 1 should be there in file2 and other lines in file 2 should be deleted in file2.

Please give me the soulution to resolve this problem.

Thanks in advance.

if the line in file1 matches the first column in file2, why would you need to paste it to file2? It's already in file2.

Sorry ....I am over confused about this issue.

If the file 1 is matching with the file2 column 1, then i need to select that row alone and write it in to new file.

That;s it.

Thanks for your valuble time.

I only know perl well enough to do this:

#!/usr/bin/perl
use warnings;
use strict;
my %file2;
open (FILE2, '<' , 'path/to/file2') or die "$!";
while(<FILE2>){
    $file2{(split(/\s+/))[0]}=1;
}
close FILE2;
open (FILE1, '<' , 'path/to/file1') or die "$!";
open (OUT, '>' , 'path/to/outputfile') or die "$!";
while(<FILE1>){
    chomp;
    if (exists $file2{$_}) {
        print OUT "$_\n";
    }
}
close FILE1;
close OUT;

You want to read the smaller of the two files into a hash, so if file1 is considerbaly smaller than file2, file1 should be read into a hash to compare to file2 instead of how I did it. If both files are the same or nearly the same size it does not matter.

Thanks for your time, and solution.

But ineed solution in shell program, ...i dion't know perl.sorry for that

Hi Kavin,

I just executed your code in perl. and it is creating an empty output file.

Try below shell script..

#! /bin/ksh
#set -vx
INFILE1=$1
INFILE2=$2
OUTFILE=/tmp/outfil.dat
cat $INFILE1 | while read line
do
cat $INFILE2 | while read line1
do
echo $line
wrd=`echo $line1 | cut -c1-24`
if [ $line = $wrd ]; then
echo $line1 >> $OUTFILE
fi
done
done

======================================================
$cat f1.dat
QWERTYUI098765432112345
QWIUYTUI098765432112345
CDXRTYUI098765432112345

$cat f2.dat
QWERTYUI098765432112345 abc@soft.com 2007-09-260012275.80 0000924 00245.00 2007-
10-25ASDFRESS,POIU S 000000000000015.52 000000000000000.00 CBB00010000000906
QWERTYUI098765432112345 abc@soft.com 2007-09-260012275.80 0000924 00245.00 2007-
10-25ASDFRESS,POIU S 000000000000015.52 000000000000000.00 CBB00010000000906
QWERTYUI098765432112345 abc@soft.com 2007-09-260012275.80 0000924 00245.00 2007-
10-25ASDFRESS,POIU S 000000000000015.52 000000000000000.00 CBB00010000000906
AAQAAAAAAAAAAAAAAAAAAAA abc@soft.com 2007-09-260012275.80 0000924 00245.00 2007-
10-25ASDFRESS,POIU S 000000000000015.52 000000000000000.00 CBB00010000000906
$

$comp.ksh f1.dat f2.dat

$view /tmp/outfil.dat
QWERTYUI098765432112345 abc@soft.com 2007-09-260012275.80 0000924 00245.00 2007-
10-25ASDFRESS,POIU S 000000000000015.52 000000000000000.00 CBB00010000000906
QWERTYUI098765432112345 abc@soft.com 2007-09-260012275.80 0000924 00245.00 2007-
10-25ASDFRESS,POIU S 000000000000015.52 000000000000000.00 CBB00010000000906
QWERTYUI098765432112345 abc@soft.com 2007-09-260012275.80 0000924 00245.00 2007-
10-25ASDFRESS,POIU S 000000000000015.52 000000000000000.00 CBB00010000000906

==============================================

Hope this helps :slight_smile:

nivas
(1) sort the first field of file1
(2) same way sort the second file on the first field
(Assuming the first field's record length is same if digit is there use sort -n
if strings is there use sort -d and if both are there use sort -k , and you want to join on the first field.)
use the comd ->
join -1 1 -2 1 -t(the delimiter used ) -o 1.1 1.2 2.1 2.1 file1 file2 > file 3
Here 1.1 is the first filed of file1 and likewise .I hope it I have made it
clear .

Enjoy :slight_smile: