Help! SHELL or AWK script - only the masters of the forum will solve


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Help! SHELL or AWK script - only the masters of the forum will solve
# 1  
Old 11-29-2009
Help! SHELL or AWK script - only the masters of the forum will solve

Hello everybody!

I have no experience with shell Programmer, but I need to compare 02 files. Txt and generate an output or a new file, after the comparisons. see:

If the column 1 of file1 is equal to column 1 of file2, and column 3 of file2 contains the column 4 of file1, output: column1 (file1); column2 (file1); col3 (file2); col3 (file1)

file1:

ARC-RJ-E320-01, 2009-11-26, GigabitEthernet, 0 / 0
ARC-RJ-E320-01, 2009-11-26, GigabitEthernet, 1 / 0
ARC-RJ-E320-01, 2009-11-26, GigabitEthernet, 3 / 0
ARC-RJ-E320-01, 2009-11-26, GigabitEthernet, 5 / 0
ARC-RJ-E320-01, 2009-11-26, ATM, 13 / 0
ARC-RJ-E320-01, 2009-11-26, ATM, 14 / 0
ARC-RJ-E320-01, 2009-11-26, ATM, 15 / 0
BOT-RJ-E320-03, 2009-11-26, GigabitEthernet, 0 / 0
BOT-RJ-E320-03, 2009-11-26, GigabitEthernet, 1 / 0
BOT-RJ-E320-03, 2009-11-26, GigabitEthernet, 3 / 0
BOT-RJ-E320-03, 2009-11-26, GigabitEthernet, 5 / 0
BOT-RJ-E320-03, 2009-11-26, GigabitEthernet, 14 / 0
BOT-RJ-E320-03, 2009-11-26, GigabitEthernet, 15 / 0
BRB-RJ-E320-02, 2009-11-26, GigabitEthernet, 0 / 0
BRB-RJ-E320-02, 2009-11-26, GigabitEthernet, 1 / 0
BRB-RJ-E320-02, 2009-11-26, GigabitEthernet, 3 / 0
BRB-RJ-E320-02, 2009-11-26, GigabitEthernet, 5 / 0
BRB-RJ-E320-02, 2009-11-26, GigabitEthernet, 14 / 0
MAR-RJ-E320-01, 2009-11-26, GigabitEthernet, 0 / 0
MAR-RJ-E320-01, 2009-11-26, GigabitEthernet, 1 / 0
MAR-RJ-E320-01, 2009-11-26, GigabitEthernet, 3 / 0
MAR-RJ-E320-01, 2009-11-26, GigabitEthernet, 5 / 0
MAR-RJ-E320-01, 2009-11-26, GigabitEthernet, 14 / 0
MAR-RJ-E320-01, 2009-11-26, GigabitEthernet, 15 / 0

file2:

ARC-RJ-E320-01, 2009-11-26, 0/0/0, 9278
ARC-RJ-E320-01, 2009-11-26, 1/0/0, 6949
ARC-RJ-E320-01, 2009-11-26, 3/0/0, 5345
ARC-RJ-E320-01, 2009-11-26, 5/0/1, 8842
ARC-RJ-E320-01, 2009-11-26, 11/0/0; 2316
ARC-RJ-E320-01, 2009-11-26, 11/0/1; 3392
ARC-RJ-E320-01, 2009-11-26, 13/0/0; 1300
ARC-RJ-E320-01, 2009-11-26, 13/0/1; 1752
ARC-RJ-E320-01, 2009-11-26, 13/0/2; 1188
ARC-RJ-E320-01, 2009-11-26, 13/0/3, 900
ARC-RJ-E320-01, 2009-11-26, 14/0/0; 728
ARC-RJ-E320-01, 2009-11-26, 14/0/1; 230
ARC-RJ-E320-01, 2009-11-26, 14/0/2; 1147
ARC-RJ-E320-01, 2009-11-26, 14/0/3; 1053
ARC-RJ-E320-01, 2009-11-26, 15/0/0; 1110
ARC-RJ-E320-01, 2009-11-26, 15/0/1, 129
ARC-RJ-E320-01, 2009-11-26, 15/0/2; 1204
ARC-RJ-E320-01, 2009-11-26, 15/0/3; 456
BOT-RJ-E320-03, 2009-11-26, 0/0/0, 2745
BOT-RJ-E320-03, 2009-11-26, 1/0/0, 220
BOT-RJ-E320-03, 2009-11-26, 3/0/0; 10,021
BOT-RJ-E320-03, 2009-11-26, 5/0/0, 5815
BRB-RJ-E320-02, 2009-11-26, 0/0/0, 7118
BRB-RJ-E320-02, 2009-11-26, 1/0/0, 1891
BRB-RJ-E320-02, 2009-11-26, 3/0/0, 5401
BRB-RJ-E320-02, 2009-11-26, 5/0/0, 6606
MAR-RJ-E320-01, 2009-11-26, 0/0/0, 2777
MAR-RJ-E320-01, 2009-11-26, 3/0/0, 3697
MAR-RJ-E320-01, 2009-11-26, 5/0/0, 2952

or has to be generated output:
column1 (file1); column2 (file1); col3 (file2); col3 (file1)

ARC-RJ-E320-01, 2009-11-26, 0/0/0; GigabitEthernet
ARC-RJ-E320-01, 2009-11-26, 1/0/0; GigabitEthernet
ARC-RJ-E320-01, 2009-11-26, 3/0/0; GigabitEthernet
ARC-RJ-E320-01, 2009-11-26, 5/0/1; GigabitEthernet
ARC-RJ-E320-01, 2009-11-26, 13/0/0; ATM
ARC-RJ-E320-01, 2009-11-26, 13/0/1; ATM
ARC-RJ-E320-01, 2009-11-26, 13/0/2; ATM
ARC-RJ-E320-01, 2009-11-26, 13/0/3; ATM
ARC-RJ-E320-01, 2009-11-26, 14/0/0; ATM
ARC-RJ-E320-01, 2009-11-26, 14/0/1; ATM
ARC-RJ-E320-01, 2009-11-26, 14/0/2; ATM
ARC-RJ-E320-01, 2009-11-26, 14/0/3; ATM
ARC-RJ-E320-01, 2009-11-26, 15/0/0; ATM
ARC-RJ-E320-01, 2009-11-26, 15/0/1; ATM
ARC-RJ-E320-01, 2009-11-26, 15/0/2; ATM
ARC-RJ-E320-01, 2009-11-26, 15/0/3; ATM
BOT-RJ-E320-03, 2009-11-26, 0/0/0; GigabitEthernet
BOT-RJ-E320-03, 2009-11-26, 1/0/0; GigabitEthernet
BOT-RJ-E320-03, 2009-11-26, 3/0/0; GigabitEthernet
BOT-RJ-E320-03, 2009-11-26, 5/0/0; GigabitEthernet
BRB-RJ-E320-02, 2009-11-26, 0/0/0; GigabitEthernet
BRB-RJ-E320-02, 2009-11-26, 1/0/0; GigabitEthernet
BRB-RJ-E320-02, 2009-11-26, 3/0/0; GigabitEthernet
BRB-RJ-E320-02, 2009-11-26, 5/0/0; GigabitEthernet
MAR-RJ-E320-01, 2009-11-26, 0/0/0; GigabitEthernet
MAR-RJ-E320-01, 2009-11-26, 3/0/0; GigabitEthernet
MAR-RJ-E320-01, 2009-11-26, 5/0/0; GigabitEthernet


thanks.

He2
# 2  
Old 11-29-2009
Try:
Code:
awk -F'[,;] ' 'NR==FNR {A[$1,$4]=$3;next} {split($3,s,"/")}
               $4=A[$1,s[1]" / "s[2]] {print $1", "$2", "$3"; "$4}' file1 file2


Last edited by Scrutinizer; 11-29-2009 at 07:58 PM..
# 3  
Old 11-30-2009
Quote:
Originally Posted by Scrutinizer
Try:
Code:
awk -F'[,;] ' 'NR==FNR {A[$1,$4]=$3;next} {split($3,s,"/")}
               $4=A[$1,s[1]" / "s[2]] {print $1", "$2", "$3"; "$4}' file1 file2

Dear, Scrutinizer

Thanks for the reply. Your script did not generate output?
what can be wrong?

Thank you.

He2


bash-2.05$ /usr/xpg4/bin/awk -F"[,;] " 'NR==FNR {A[$1,$4]=$3;next} {split($3,s,"/")} $4=A[$1,s[1]" / "s[2]] {print $1", "$2", "$3"; "$4}' file1.txt file2.txt
bash-2.05$
# 4  
Old 11-30-2009
Do file1.txt and file2.txt exist? In your example they do not have the txt extension. Do your input files have exactly the same format as your examples? Do the scripts also not work with input files that contain identical data as was used in your examples.

Last edited by Scrutinizer; 11-30-2009 at 12:19 PM..
# 5  
Old 12-01-2009
Quote:
Originally Posted by Scrutinizer
Do file1.txt and file2.txt exist? In your example they do not have the txt extension. Do your input files have exactly the same format as your examples? Do the scripts also not work with input files that contain identical data as was used in your examples.

excuse-me,
I saved the files with name file1.txt and file2.txt, but, when pasted the contents of the files ocurred problem, follow the correct files:


thank you

file1.txt:
Code:
ARC-RJ-E320-01;2009-11-26;GigabitEthernet;0/0
ARC-RJ-E320-01;2009-11-26;GigabitEthernet;1/0
ARC-RJ-E320-01;2009-11-26;GigabitEthernet;3/0
ARC-RJ-E320-01;2009-11-26;GigabitEthernet;5/0
ARC-RJ-E320-01;2009-11-26;ATM;13/0
ARC-RJ-E320-01;2009-11-26;ATM;14/0
ARC-RJ-E320-01;2009-11-26;ATM;15/0
BOT-RJ-E320-03;2009-11-26;GigabitEthernet;0/0
BOT-RJ-E320-03;2009-11-26;GigabitEthernet;1/0
BOT-RJ-E320-03;2009-11-26;GigabitEthernet;3/0
BOT-RJ-E320-03;2009-11-26;GigabitEthernet;5/0
BOT-RJ-E320-03;2009-11-26;GigabitEthernet;14/0
BOT-RJ-E320-03;2009-11-26;GigabitEthernet;15/0
BRB-RJ-E320-02;2009-11-26;GigabitEthernet;0/0
BRB-RJ-E320-02;2009-11-26;GigabitEthernet;1/0
BRB-RJ-E320-02;2009-11-26;GigabitEthernet;3/0
BRB-RJ-E320-02;2009-11-26;GigabitEthernet;5/0
BRB-RJ-E320-02;2009-11-26;GigabitEthernet;14/0
MAR-RJ-E320-01;2009-11-26;GigabitEthernet;0/0
MAR-RJ-E320-01;2009-11-26;GigabitEthernet;1/0
MAR-RJ-E320-01;2009-11-26;GigabitEthernet;3/0
MAR-RJ-E320-01;2009-11-26;GigabitEthernet;5/0
MAR-RJ-E320-01;2009-11-26;GigabitEthernet;14/0
MAR-RJ-E320-01;2009-11-26;GigabitEthernet;15/0

file2.txt:
Code:
ARC-RJ-E320-01;2009-11-26;0/0/0;9278
ARC-RJ-E320-01;2009-11-26;1/0/0;6949
ARC-RJ-E320-01;2009-11-26;3/0/0;5345
ARC-RJ-E320-01;2009-11-26;5/0/1;8842
ARC-RJ-E320-01;2009-11-26;11/0/0;2316
ARC-RJ-E320-01;2009-11-26;11/0/1;3392
ARC-RJ-E320-01;2009-11-26;13/0/0;1300
ARC-RJ-E320-01;2009-11-26;13/0/1;1752
ARC-RJ-E320-01;2009-11-26;13/0/2;1188
ARC-RJ-E320-01;2009-11-26;13/0/3;900
ARC-RJ-E320-01;2009-11-26;14/0/0;728
ARC-RJ-E320-01;2009-11-26;14/0/1;230
ARC-RJ-E320-01;2009-11-26;14/0/2;1147
ARC-RJ-E320-01;2009-11-26;14/0/3;1053
ARC-RJ-E320-01;2009-11-26;15/0/0;1110
ARC-RJ-E320-01;2009-11-26;15/0/1;129
ARC-RJ-E320-01;2009-11-26;15/0/2;1204
ARC-RJ-E320-01;2009-11-26;15/0/3;456
BOT-RJ-E320-03;2009-11-26;0/0/0;2745
BOT-RJ-E320-03;2009-11-26;1/0/0;220
BOT-RJ-E320-03;2009-11-26;3/0/0;10021
BOT-RJ-E320-03;2009-11-26;5/0/0;5815
BRB-RJ-E320-02;2009-11-26;0/0/0;7118
BRB-RJ-E320-02;2009-11-26;1/0/0;1891
BRB-RJ-E320-02;2009-11-26;3/0/0;5401
BRB-RJ-E320-02;2009-11-26;5/0/0;6606
MAR-RJ-E320-01;2009-11-26;0/0/0;2777
MAR-RJ-E320-01;2009-11-26;3/0/0;3697
MAR-RJ-E320-01;2009-11-26;5/0/0;2952

or has to be generated output:
column1 (file1); column2 (file1); col3 (file2); col3 (file1)


Code:
ARC-RJ-E320-01;2009-11-26;0/0/0;GigabitEthernet
ARC-RJ-E320-01;2009-11-26;1/0/0;GigabitEthernet
ARC-RJ-E320-01;2009-11-26;3/0/0;GigabitEthernet
ARC-RJ-E320-01;2009-11-26;5/0/1;GigabitEthernet
ARC-RJ-E320-01;2009-11-26;13/0/0;ATM
ARC-RJ-E320-01;2009-11-26;13/0/1;ATM
ARC-RJ-E320-01;2009-11-26;13/0/2;ATM
ARC-RJ-E320-01;2009-11-26;13/0/3;ATM
ARC-RJ-E320-01;2009-11-26;14/0/0;ATM
ARC-RJ-E320-01;2009-11-26;14/0/1;ATM
ARC-RJ-E320-01;2009-11-26;14/0/2;ATM
ARC-RJ-E320-01;2009-11-26;14/0/3;ATM
ARC-RJ-E320-01;2009-11-26;15/0/0;ATM
ARC-RJ-E320-01;2009-11-26;15/0/1;ATM
ARC-RJ-E320-01;2009-11-26;15/0/2;ATM
ARC-RJ-E320-01;2009-11-26;15/0/3;ATM
BOT-RJ-E320-03;2009-11-26;0/0/0;GigabitEthernet
BOT-RJ-E320-03;2009-11-26;1/0/0;GigabitEthernet
BOT-RJ-E320-03;2009-11-26;3/0/0;GigabitEthernet
BOT-RJ-E320-03;2009-11-26;5/0/0;GigabitEthernet
BRB-RJ-E320-02;2009-11-26;0/0/0;GigabitEthernet
BRB-RJ-E320-02;2009-11-26;1/0/0;GigabitEthernet
BRB-RJ-E320-02;2009-11-26;3/0/0;GigabitEthernet
BRB-RJ-E320-02;2009-11-26;5/0/0;GigabitEthernet
MAR-RJ-E320-01;2009-11-26;0/0/0;GigabitEthernet
MAR-RJ-E320-01;2009-11-26;3/0/0;GigabitEthernet
MAR-RJ-E320-01;2009-11-26;5/0/0;GigabitEthernet


Last edited by vgersh99; 12-01-2009 at 07:28 PM.. Reason: code tags, please!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to solve query

Hi I have data in the below format in two columns in excel which i will copy to notepad. test as rec1, string test as rec2, byteint test as rec3, string update date as test, datetime name as tes2 string I need to add trim function on all the string columns and keep the remaining... (10 Replies)
Discussion started by: pisikar
10 Replies

2. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

3. Shell Programming and Scripting

How to solve awk: line 1: runaway string constant error?

Hi All ! I am just trying to print bash variable in awk statement as string here is my script n=1 for file in `ls *.tk |sort -t"-" -k2n,2`; do ak=`(awk 'FNR=='$n'{print $0}' res.dat)` awk '{print "'$ak'",$0}' OFS="\t" $file n=$((n+1)) unset ak doneI am getting following error awk:... (7 Replies)
Discussion started by: Akshay Hegde
7 Replies

4. Shell Programming and Scripting

Shell Scripting needed for the below problem please solve ...

please solve my problem..... :( i have a sample input file as A A A A B B B B B C C C C C i want the output such that if the count of A or B or C is greater than 3 A (1 Reply)
Discussion started by: hemanthsaikumar
1 Replies

5. Shell Programming and Scripting

korn shell how to solve rm file and display files

Write a KSH script called cleanse which displays the name of each file in a given directory and allows the user to interactively decide whether or not to keep or delete the specific file. Notes: Again, please check for errors. can any one help on this problem ?? (1 Reply)
Discussion started by: babuda0059
1 Replies

6. Shell Programming and Scripting

Bash or awk script to solve this problem

Hi everybody! I have written some awk scripts that return me some results I need to process. At the moment I use openOffice to process them, but I am trying to find a more efficient solution using possibly a bash or awk script. I have two files, file1 is in the format: time position ... (3 Replies)
Discussion started by: Alice236
3 Replies

7. Shell Programming and Scripting

Help me to solve some question about shell Script

Factorial calculation Example output: Please enter a non-negative number: 3 3! = 3 X 2 X 1 = 6 Please enter a non-negative number: 10 10! = 10 X 9 X 8 X 7 X 6 X 5 X 4 X 3 X 2 X 1 = 3628800 Please enter a non-negative number: -1 ... (1 Reply)
Discussion started by: cenco
1 Replies

8. Shell Programming and Scripting

Help me to solve some question about shell Script

Factorial calculation Example output: Please enter a non-negative number: 3 3! = 3 X 2 X 1 = 6 Please enter a non-negative number: 10 10! = 10 X 9 X 8 X 7 X 6 X 5 X 4 X 3 X 2 X 1 = 3628800 Please enter a non-negative number:... (1 Reply)
Discussion started by: cenco
1 Replies

9. UNIX for Dummies Questions & Answers

Can anyone solve this awk issue ?

awk -F^ '{ if (($1 != "X") && ($5 != "3")) || if (($1 != "S") && ($5 != "2")) print $0}' abc> xyz The above command returns this error awk: syntax error near line 1 awk: illegal statement near line 1 I am newbie to awk, Any awk intelligence involved in correcting this issue is highly... (2 Replies)
Discussion started by: karthickrn
2 Replies

10. Shell Programming and Scripting

Can AWK solve it ???

Hi I have some 200 tables and I have macro which actually returns the tables columns descriptions . Say for exapmle describe('table1') will list all the columns of table one with the description. Now what I want to do is I want to write a shell script which will take tables name from a file... (1 Reply)
Discussion started by: emon
1 Replies
Login or Register to Ask a Question