Shell Script using awk

File1

9000|9000|WW|1|1|SL|472|472|LC|2272|1072|MTY

9000|9000|WW|1|1|SL|470|470|MC|1270|1172|MPVT

9000|9000|WW|1|1|SL|472|472|LC|1072|1672|MBD

9000|9000|WW|1|1|SL|473|473|LF|1173|1173|MTY

File2

DPT.1072 CP~ Apr 1514283.914

DPT.1172 CP~ Apr 967882.506

DPT.1672 CP~ Apr 545199.000

DPT.1772 CP~ Apr 912656.817

Compare the column 11 of file 1 with the 1st column of file2. The 1st column of file2 contains a prefix dpt. also. We need to compare file2 with file1 and in the output we should get all the values from file2 that are present in file1. We should not remove dpt. from the output. It will always be 8 characters in 1st column of file2 (e.g DPT.1072).

The output should be:

DPT.1072 CP~ Apr 1514283.914

DPT.1172 CP~ Apr 967882.506

DPT.1672 CP~ Apr 545199.000

I am thankful for your help.

perhaps like this:

awk -F "|" '{print $11}' testing|xargs -i grep {} testing1|awk '{print$1}' testing1

testing is inputfile1 testing1 is comparation file

thanks

if you want all the output from file2
awk -F "|" '{print $11}' testing|xargs -i grep {} testing1|awk '{print$0}' testing1

Thanks for your reply. I will try this. Only thing I am not sure is the prefix DPT. in file2. We want to compare two files after removing prefix DPT. but we do NOT want to remove DPT. from the output. Means in the comparsion command only we need to compare without DPT.---------May be at the time of comparsion something like substr(col1 of file2, 4,8)...........

Thanks Much.

awk -F"|" 'FNR==NR {a[$11]++; next} a[substr($0, 5, 4)] {print}' file1 file2

awk -F"|" field separator

FNR==NR {a[$11]++; next} buid an array with 11th field of file1 and give value 1 (default value 0+1) to it.

a[substr($0, 5, 4)] {print} if a[nnnn] is TRUE (1)---> print line from file2

Thank-You. It worked fine.

hi can you say me what xargs is all about...

thanks
krips.

Please help me in fixing the following command:

awk -F "|" 'FNR==NR {a[$1]++; next} {print $0,a[$1]}' file1 file2 >>file3

File1
3308100100|C|20.83|21.87|22.91|04/APR/2009|2009|02|MAR
0102110500|C|5.01|5.26|5.51|04/APR/2009|2009|02|MAR
3308100100|C|18.74|19.67|20.61|04/APR/2009|2009|02|MAR
0102140200|C|5.07|5.32|5.57|04/APR/2009|2009|02|MAR
3308100100|C|741.50|778.58|815.65|04/APR/2009|2009|02|MAR
0104100400|C|41.26|43.33|45.39|04/APR/2009|2009|02|MAR
0530100300|C|38.26|40.17|42.08|04/APR/2009|2009|02|MAR

file2
3308100100|Store Inventory Turns Without Gas(FT LEONARD WOOD MS)
0530100300|Total Markdowns(FT BRAGG MS - SOUTH)
4792999900|Productivity(TRAV AFB)

the file3 should be like this:
3308100100|C|20.83|21.87|22.91|04/APR/2009|2009|02|MAR|Store Inventory Turns Without Gas(FT LEONARD WOOD MS)
3308100100|C|18.74|19.67|20.61|04/APR/2009|2009|02|MAR|Store Inventory Turns Without Gas(FT LEONARD WOOD MS)
3308100100|C|741.50|778.58|815.65|04/APR/2009|2009|02|MAR|Store Inventory Turns Without Gas(FT LEONARD WOOD MS)
0530100300|C|38.26|40.17|42.08|04/APR/2009|2009|02|MAR|Total Markdowns(FT BRAGG MS - SOUTH)

you can see comparision is being done on column 1 of both the files...if column1 of file 1 matches with column 1 of file2 then we need to append column 2 of file 2 in the end of matching rows from file1............please let me know if it is possible or not?

Next time, please start a new thread for a NEW issue.

awk -F '|'  'FNR==NR {a[$1]=$2; next} $1 in a {print $0,a[$1]}' OFS='|' file2 file1

perl

while(<DATA>)
{
	my @tmp=split("[|]",$_);
	$hash{$tmp[10]}=1;
}
open $fh,"<","a.txt";
while(<$fh>){
	my @tmp=split;
	my @t1=split("[.]",$tmp[0]);
	print if exists $hash{$t1[1]};
}
__DATA__
9000|9000|WW|1|1|SL|472|472|LC|2272|1072|MTY
9000|9000|WW|1|1|SL|470|470|MC|1270|1172|MPVT
9000|9000|WW|1|1|SL|472|472|LC|1072|1672|MBD
9000|9000|WW|1|1|SL|473|473|LF|1173|1173|MTY

Thanks vgersh99....I will start next thread as you suggested but one more help as it is related to this one only......if it had been a column 4 (instead of column 1)of file 1 and column 5 (instead of column 1)of file2 then how would we have written this command.

awk -F '|'  'FNR==NR {a[$5]=$2; next} $4 in a {print $0,a[$4]}' OFS='|' file2 file1

THANKS MUCH for your kind help.