Unix File operations

Hi,

Iam having the two files as follows:

file1:

ASQWEDFR09876543121234512
POIUYTREW09876512345676788
ZXCVBNMKS1209888888888888

file2:
ASQWEDFR09876543121234516 asdcvfgbtg@abc.com 0000000-90-1239--2008 8990----
CXADFGTU09876543121234789 asdcvfgbtg@abc.com 0000000-90-1239--2008 8990----
KLOPIUYRE09876541234234516 asdcvfgbtg@abc.com 0000000-90-1239--2008 8990----
ZXSDCVFG09876512345634516 asdcvfgbtg@abc.com 0000000-90-1239--2008 8990----

i WANT TO update the contents in file1 to file2 and remove the other lines.

example.:

ASQWEDFR09876543121234516 asdcvfgbtg@abc.com 0000000-90-1239--2008 8990----
POIUYTREW0987651234567678 asdcvfgbtg@abc.com 0000000-90-1239--2008 8990----
ZXCVBNMKS1209888888888888 asdcvfgbtg@abc.com 0000000-90-1239--2008 8990----

paste file_update file_to_be_updated | awk ' { print $1" " $3" "$4}' > final
cat final > file_to_be_updated

grep -f file1 file2 > out.txt

Hi,

Thanks for your reply. It is working fine, but iam having the file 2 as follows:

file 2

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

file 1

QWERTYUI098765432112345
QWIUYTUI098765432112345
CDXRTYUI098765432112345

While iam pasting from unix box to here, the contents are not in one single line.

The above info in file1 is all in a single line.

Now i want to take the lines one by one in file1 and find if there is matching record in file 2 and if it finds it should replace and most important is it should not disturb the other info in the particular line.( Previous paste command is working but it is disturbing the spaces and it is removing those spaces in file 2.

Please give me the solution.

Thanks in advance.

Here also in file 2 , there is 40 spaces in between the email id and the next number. while copying it is not correctly pasted.

Thanks

use vB Codes when posting code or data samples.

how to use this vb codes??

The quoted link explains the details.
For example to post your file2 content:

```text
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

Please suggest some commands for my problem mensioned above.

I think below steps should help you to get resolution -

$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

$cut -c25- f2.dat > out1.txt

$paste f1.dat out1.txt
QWERTYUI098765432112345 abc@soft.com 2007-09-260012275.80 0000924 00245.00 2007-
10-25ASDFRESS,POIU S 000000000000015.52 000000000000000.00 CBB00010000000906
QWIUYTUI098765432112345 abc@soft.com 2007-09-260012275.80 0000924 00245.00 2007-
10-25ASDFRESS,POIU S 000000000000015.52 000000000000000.00 CBB00010000000906
CDXRTYUI098765432112345 abc@soft.com 2007-09-260012275.80 0000924 00245.00 2007-
10-25ASDFRESS,POIU S 000000000000015.52 000000000000000.00 CBB00010000000906

Why have you posted the same question in two threads? I just finished posting a reply in the other thread and here you already are getting replies in this thread. But here your data is different? Whatever....

This solution will cut the 1st column in file 2 and append the file1 with file2 1st culumn.

But i need to take the file1 and search the lines in file2 1st column, if it matches, then it should take that line alone and write in to a seperate file.

Thanks for your precious time.

Actually KavinADC,

For long time that thread was not replied by any users,so i cretaed a new one stating a clear description o f the problem.

Ok.. Now I got your requirement.

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

:b: Hope this solves your problem