He2
November 25, 2009, 9:04pm
1
Friends;
I have little experience with shell and I am not able to resolve this
problem:
file1:
CRA-JJ-W980-01; 2009-11-24; GigabitEthernet; 0/0
CRA-JJ-W980-01; 2009-11-24; GigabitEthernet;1/0
CRA-JJ-W980-01;2009-11-24;GigabitEthernet;3/0
CRA-JJ-W980-01;2009-11-24;GigabitEthernet;5/0
CRA-JJ-W980-01;2009-11-24;ATM;13/0
CRA-JJ-W980-01;2009-11-24;ATM;14/0
CRA-JJ-W980-01;2009-11-24;ATM;15/0
File2:
CRA-JJ-W980-01;2009-11-24;0/0/0;6967
CRA-JJ-W980-01;2009-11-24;1/0/0;5035
CRA-JJ-W980-01;2009-11-24;3/0/0;3937
CRA-JJ-W980-01;2009-11-24;5/0/1;5745
CRA-JJ-W980-01;2009-11-24;11/0/0;1786
CRA-JJ-W980-01;2009-11-24;11/0/1;2376
CRA-JJ-W980-01;2009-11-24;13/0/0;925
CRA-JJ-W980-01;2009-11-24;13/0/1;1254
CRA-JJ-W980-01;2009-11-24;13/0/2;772
CRA-JJ-W980-01;2009-11-24;13/0/3;690
CRA-JJ-W980-01;2009-11-24;14/0/0;515
CRA-JJ-W980-01;2009-11-24;14/0/1;179
CRA-JJ-W980-01;2009-11-24;14/0/2;860
CRA-JJ-W980-01;2009-11-24;14/0/3;677
CRA-JJ-W980-01;2009-11-24;15/0/0;806
CRA-JJ-W980-01;2009-11-24;15/0/1;98
CRA-JJ-W980-01;2009-11-24;15/0/2;894
CRA-JJ-W980-01;2009-11-24;15/0/3;353
compare column 3 of file 2 and column 4 file 1. as in column 4 of file 1 is contained in the 2nd column 3.
eg
> Line 1 of file 1:
CRA-JJ-W980-01;2009-11-24;GigabitEthernet;0/0
> Line 1 of file 2:
CRA-JJ-W980-01;2009-11-24;0/0/0;6967
0/0/0 contains the beginning 0/0
then I would file a 3 with the following output:
File3:
CRA-JJ-W980-01;2009-11-24;0/0/0;GigabitEthernet
CRA-JJ-W980-01;2009-11-24;1/0/0;GigabitEthernet
CRA-JJ-W980-01;2009-11-24;3/0/0;GigabitEthernet
CRA-JJ-W980-01;2009-11-24;5/0/1;GigabitEthernet
CRA-JJ-W980-01;2009-11-24;13/0/0;ATM
CRA-JJ-W980-01;2009-11-24;13/0/1;ATM
CRA-JJ-W980-01;2009-11-24;13/0/2;ATM
CRA-JJ-W980-01;2009-11-24;13/0/3;ATM
CRA-JJ-W980-01;2009-11-24;14/0/0;ATM
CRA-JJ-W980-01;2009-11-24;14/0/1;ATM
CRA-JJ-W980-01;2009-11-24;14/0/2;ATM
CRA-JJ-W980-01;2009-11-24;14/0/3;ATM
CRA-JJ-W980-01;2009-11-24;15/0/0;ATM
CRA-JJ-W980-01;2009-11-24;15/0/1;ATM
CRA-JJ-W980-01;2009-11-24;15/0/2;ATM
CRA-JJ-W980-01;2009-11-24;15/0/3;ATM
Thank you.
#!/bin/bash
for line in $(cat file2); do
echo $line | awk 'BEGIN { FS=";" ; OFS=";" } { print $1, $2, $3 }' >> file3
done
lin=1
for line in $(cat file1); do
pat="$(sed -n ${lin}p file1 | awk 'BEGIN { FS=";" } { print $3 }')"
sed -n ${lin}p file3 | sed s/$/\;"$pat"/ >> file4
lin=$[$lin+1]
done
output is file4, just rename it to whatever you want
output:
mo@mo-laptop:~/scripts$ cat file4
RA-JJ-W980-01;2009-11-24;0/0/0; GigabitEthernet
CRA-JJ-W980-01;2009-11-24;1/0/0; GigabitEthernet
CRA-JJ-W980-01;2009-11-24;3/0/0;GigabitEthernet
CRA-JJ-W980-01;2009-11-24;5/0/1;GigabitEthernet
CRA-JJ-W980-01;2009-11-24;11/0/0;ATM
CRA-JJ-W980-01;2009-11-24;11/0/1;ATM
CRA-JJ-W980-01;2009-11-24;13/0/0;ATM
$
$ cat file1
CRA-JJ-W980-01;2009-11-24;GigabitEthernet;0/0
CRA-JJ-W980-01;2009-11-24;GigabitEthernet;1/0
CRA-JJ-W980-01;2009-11-24;GigabitEthernet;3/0
CRA-JJ-W980-01;2009-11-24;GigabitEthernet;5/0
CRA-JJ-W980-01;2009-11-24;ATM;13/0
CRA-JJ-W980-01;2009-11-24;ATM;14/0
CRA-JJ-W980-01;2009-11-24;ATM;15/0
$
$ cat file2
CRA-JJ-W980-01;2009-11-24;0/0/0;6967
CRA-JJ-W980-01;2009-11-24;1/0/0;5035
CRA-JJ-W980-01;2009-11-24;3/0/0;3937
CRA-JJ-W980-01;2009-11-24;5/0/1;5745
CRA-JJ-W980-01;2009-11-24;11/0/0;1786
CRA-JJ-W980-01;2009-11-24;11/0/1;2376
CRA-JJ-W980-01;2009-11-24;13/0/0;925
CRA-JJ-W980-01;2009-11-24;13/0/1;1254
CRA-JJ-W980-01;2009-11-24;13/0/2;772
CRA-JJ-W980-01;2009-11-24;13/0/3;690
CRA-JJ-W980-01;2009-11-24;14/0/0;515
CRA-JJ-W980-01;2009-11-24;14/0/1;179
CRA-JJ-W980-01;2009-11-24;14/0/2;860
CRA-JJ-W980-01;2009-11-24;14/0/3;677
CRA-JJ-W980-01;2009-11-24;15/0/0;806
CRA-JJ-W980-01;2009-11-24;15/0/1;98
CRA-JJ-W980-01;2009-11-24;15/0/2;894
CRA-JJ-W980-01;2009-11-24;15/0/3;353
$
$
$ ##
$ awk -F";" '{if (NR==FNR){x[$4]=$3}
> else {split($3,s,"/");
> if (x[s[1]"/"s[2]]!=""){
> print $1";"$2";"$3";"x[s[1]"/"s[2]]
> }}}' file1 file2
CRA-JJ-W980-01;2009-11-24;0/0/0;GigabitEthernet
CRA-JJ-W980-01;2009-11-24;1/0/0;GigabitEthernet
CRA-JJ-W980-01;2009-11-24;3/0/0;GigabitEthernet
CRA-JJ-W980-01;2009-11-24;5/0/1;GigabitEthernet
CRA-JJ-W980-01;2009-11-24;13/0/0;ATM
CRA-JJ-W980-01;2009-11-24;13/0/1;ATM
CRA-JJ-W980-01;2009-11-24;13/0/2;ATM
CRA-JJ-W980-01;2009-11-24;13/0/3;ATM
CRA-JJ-W980-01;2009-11-24;14/0/0;ATM
CRA-JJ-W980-01;2009-11-24;14/0/1;ATM
CRA-JJ-W980-01;2009-11-24;14/0/2;ATM
CRA-JJ-W980-01;2009-11-24;14/0/3;ATM
CRA-JJ-W980-01;2009-11-24;15/0/0;ATM
CRA-JJ-W980-01;2009-11-24;15/0/1;ATM
CRA-JJ-W980-01;2009-11-24;15/0/2;ATM
CRA-JJ-W980-01;2009-11-24;15/0/3;ATM
$
$
tyler_durden
He2
November 26, 2009, 8:34am
4
Dear, momo.reina.
Thank you for your commitment and the response, however, I need to do a comparison to the $ 4 string file1 contains the beginning of the string $ 3 file2, then if true print $ 1, $ 2, $ 3 (file2) and $ 3 (file1).
The output should have the following format:
CRA-JJ-W980-01; 2009-11-24, 0/0/0; GigabitEthernet
CRA-JJ-W980-01; 2009-11-24, 1/0/0; GigabitEthernet
CRA-JJ-W980-01; 2009-11-24, 3/0/0; GigabitEthernet
CRA-JJ-W980-01; 2009-11-24, 5/0/1; GigabitEthernet
CRA-JJ-W980-01; 2009-11-24, 13/0/0; ATM
CRA-JJ-W980-01; 2009-11-24, 13/0/1; ATM
CRA-JJ-W980-01; 2009-11-24, 13/0/2; ATM
CRA-JJ-W980-01; 2009-11-24, 13/0/3; ATM
CRA-JJ-W980-01; 2009-11-24, 14/0/0; ATM
CRA-JJ-W980-01; 2009-11-24, 14/0/1; ATM
CRA-JJ-W980-01; 2009-11-24, 14/0/2; ATM
CRA-JJ-W980-01; 2009-11-24, 14/0/3; ATM
CRA-JJ-W980-01; 2009-11-24, 15/0/0; ATM
CRA-JJ-W980-01; 2009-11-24, 15/0/1; ATM
CRA-JJ-W980-01; 2009-11-24, 15/0/2; ATM
CRA-JJ-W980-01; 2009-11-24, 15/0/3; ATM
thanks
He2
---------- Post updated at 11:34 AM ---------- Previous update was at 11:28 AM ----------
Dear, durden_tyler
Thanks for the reply. Your script generated the output perfect, but in my shell (Solaris 7 with bash-2.05) can not work with vector.
Always gives error in the scripts array.
thanks
He2
Use nawk or /usr/xpg4/bin/awk on Solaris.
He2
November 26, 2009, 12:58pm
6
Caro, durden_tyler
The script by developed you worked, only I would also compare the column $ 1 of file1 and column $ 1 of file2, also from the comparison of the column initially tested in file1 with file2.
You could also explain to me the script.
Thank you.
He2