AWK Help ME! this easy for who know


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK Help ME! this easy for who know
# 1  
Old 11-25-2009
Power AWK Help ME! this easy for who know

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.
# 2  
Old 11-26-2009
Code:
#!/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:

Code:
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

# 3  
Old 11-26-2009
Code:
$ 
$ 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
# 4  
Old 11-26-2009
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
# 5  
Old 11-26-2009
Use nawk or /usr/xpg4/bin/awk on Solaris.
# 6  
Old 11-26-2009
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
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk easy question

So, I have the following code: cat testfile.txt | awk -F, '{ print $1" "$2" "$3" "$4" "$5 }' | read DOC ORG NAME echo "$DOC" echo "$ORG" echo "$NAME" My testfile.txt looks something like the following: Document Type,Project Number,Org ID,Invoice Number It will eventually be more... (14 Replies)
Discussion started by: Parrakarry
14 Replies

2. Shell Programming and Scripting

Couple of easy questions for experts on awk/sed

Hello Experts.. I have 3-4 C codes with Oracle SQL statements embedded. All the SQL statements starts with EXEC SQL keyword and ends with ;. I want to extract all the SQL statements out of these codes. I did awk '/^EXEC SQL/,/\;/' inputFile (I use this on all of the codes individually). That... (2 Replies)
Discussion started by: juzz4fun
2 Replies

3. Shell Programming and Scripting

awk Help: quick and easy question may be: How to use &&

Hi Guru's. I am trying to use to check if $5 is greater than 80 & if not 100, then to print $0 : awk '{ if ($5>80) && if ($5 != 100) print $0} But getting error: >bdf1|sed 's/%//g'|awk '{ if ($5>80) && if ($5 != 100) print $0}' syntax error The source line is 1. The error... (6 Replies)
Discussion started by: rveri
6 Replies

4. UNIX for Dummies Questions & Answers

fun and easy awk question

I have a file called mytitles.txt containing a list of book titles I have a second file called abfile.txt containing a list of book titles (in the 3rd field) and it has author info and copyright year info as well.. I want to search mytitles.txt for a match in the 3rd field of abfiles.txt, and... (2 Replies)
Discussion started by: glev2005
2 Replies

5. UNIX for Dummies Questions & Answers

Newbie Help with Grep or Awk .. Easy one ...

I have this output: uniquemember=uid=315kthatch,ou=people,ou=client315,dc=paisleyhosting,dc=com and i want the output to be just this: 315kthatch I need it to be generic tho, because I have hundreds of lines of output, and the preceding numbers are not always 315. So I would need... (3 Replies)
Discussion started by: kthatch
3 Replies

6. UNIX for Dummies Questions & Answers

Need help on installing an EASY to use and easy to install command line text editor

Hi again. Sorry if it seems like I'm spamming the boards a bit, but I figured I might as well ask all the questions I need answers to at once, and hopefully at least get some. I have installed Solaris 10 on a server. The default text editors are there (vi, ex, ed, maybe others, I know emacs is... (4 Replies)
Discussion started by: EugeneG
4 Replies

7. Shell Programming and Scripting

Easy AWK question

Ive got some output in a file that looks exactly like this: 1 ----------- 1542 1 record(s) selected. How do I just extract that 1542 and drop it into another file or (preferrably) a variable (using a ksh script) (9 Replies)
Discussion started by: rdudejr
9 Replies

8. UNIX for Dummies Questions & Answers

Easy question about awk gsub

How does one escape the left bracket in awk? For example: | awk 'gsub (" I'm sure it's pretty simple, I'm just a noob. (1 Reply)
Discussion started by: TheCrunge
1 Replies
Login or Register to Ask a Question