search 3 file and write to 4th file (a bit complex)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search 3 file and write to 4th file (a bit complex)
# 1  
Old 12-29-2010
Data search 3 file and write to 4th file (a bit complex)

hi buddies;

rollbackip.txt:
Code:
10.14.3.65	2
10.14.3.65	3
...


lookup.txt:
Code:
...
10.14.3.65	2	10.14.5.55	1	55	10.14.6.66	1	66
10.14.3.65	3	10.14.7.77	3	77	10.14.8.88	2	88	10.14.9.99	4	99
...


ip-port.txt
Code:
...
port111	3	10.14.5.55	57
port111	2	10.14.5.55	51
port111	1	10.14.5.55	59 -> this line must be taken
...
port111	1	10.14.6.66	63 -> this line must be taken
port111	2	10.14.6.66	64
port111	3	10.14.6.66	65
port111	4	10.14.6.66	66
...
port113	3	10.14.7.77	72 -> this line must be taken
port113	1	10.14.7.77	74
...
port140	1	10.14.8.88	84
port140	2	10.14.8.88	84 -> this line must be taken
port140	5	10.14.8.88	84
...
port613	4	10.14.9.99	92 -> this line must be taken
port613	2	10.14.9.99	92
...


i need a code for;
- take ip & sector numbers, from rollbackip.txt
- search ip and sector numbers related to each, from lookup.txt
- get original values of each ip & sector, from ip-port.txt and write these original values to file result.txt.

to conclude, the new rollbackip.txt file should be:
result.txt:
Code:
10.14.3.65	2	10.14.5.55	1	59	10.14.6.66	1	63
10.14.3.65	3	10.14.7.77	3	72	10.14.8.88	2	84	10.14.9.99	4	92
...

hope to tell it clearly Smilie

thx
# 2  
Old 12-29-2010
Code:
 
awk '{if(FILENAME=="rollbackip.txt"){a[$1$2]++;next;}if(FILENAME=="ip-port.txt"){b[$3$2]=$4;next;}if(FILENAME=="lookup.txt"){if(a[$1$2]){for(i=3;i<=NF-2;i=i+3) if(b[$i$(i+1)]) $(i+2)=b[$i$(i+1)];print;}}}' rollbackip.txt ip-port.txt lookup.txt

# 3  
Old 12-29-2010
Smilie error:

Code:
nawk: syntax error at source line 1
context is
   >>> {if(FILENAME=="rollbackip.txt"){a[$1$2]++;next;}if(FILENAME=="ip-port.txt"){b[$3$2]=$4;next;}if(FILENAME=="lookup.txt"){if( <<<
nawk: illegal statement at source line 1
nawk: syntax error at source line 1

do i have to change {if(FILENAME==/home/gcsw/.../rollbackip.txt like this? i have tried both, but no result..
# 4  
Old 12-29-2010
PLease be careful with [$1$2] and [$3$2] constructs, for data like
Code:
192.168.1.1    21
192.168.1.12    1

will munge together. Awk "supports" multidimensional arrays -- it interpolates the value of SUBSEP.

Below, here is my proposal:
Code:
BEGIN {
    rollbackfile = ARGV[1];
    lookupfile   = ARGV[2];
    ipportfile   = ARGV[3];
}

/^#/ { next; }

FILENAME == rollbackfile {
    rollback[$1, $2]++;
    next;
}

FILENAME == lookupfile  {
    if (rollback[$1, $2]) {
        for (i = 3; i < NF; i += 3) { lookup[$(i+1), $(i)]++; }
    }
    next;
}

FILENAME == ipportfile {
    if (lookup[$2, $3]) { print; }
    next;
}

{
    print FILENAME "(" FNR "): unparsed line -", $0 > "/dev/stderr";
}

The files are in order on the command line.
This User Gave Thanks to m.d.ludwig For This Post:
# 5  
Old 12-29-2010
Try this,
Code:
 awk 'NR==FNR{for (i=3;i<=NF;i++){y=i;i++;b[$y$i]=$1" "$2;++i};next} {if(FILENAME=="rollbackip.txt"){f[$1" "$2]++;next}}b[$3$2]{w[$3" "$2" "$4]=b[$3$2]} END {for (k in f){printf RS k FS;for (l in w) {if(k==w[l]){printf FS l FS}}}}' lookup.txt ip-port.txt rollbackip.txt

# 6  
Old 12-29-2010
Code:
awk 'NR==FNR{A[$3,$2]=$4;next} NF>2{for(i=3;i<=NF;i+=3)$(i+2)=A[$i,$(i+1)];B[$1,$2]=$0;next} B[$1,$2]{print B[$1,$2]}' OFS='\t' ip-port.txt lookup.txt rollbackip.txt

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 7  
Old 12-29-2010
Quote:
Originally Posted by Scrutinizer
Code:
awk 'NR==FNR{A[$3,$2]=$4;next} NF>2{for(i=3;i<=NF;i+=3)$(i+2)=A[$i,$(i+1)];B[$1,$2]=$0;next} B[$1,$2]{print B[$1,$2]}' OFS='\t' ip-port.txt lookup.txt rollbackip.txt

That's the best.Smilie
This User Gave Thanks to rdcwayx For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search the specific content from the complex file

Hi, I have a file with complex data without delimiter, have requirement to fetch the specific record based on some charcters. here is my file data ... (12 Replies)
Discussion started by: Riverstone
12 Replies

2. Shell Programming and Scripting

Search text beween tags and write to file using awk

Hi Friends, I have a very big text file, that has code for multiple functions. I have scan through the file and write each function in seperate file. All functions starts with BEGIN DSFNC Identifier "ABCDDataValidationfnc" and ends with END DSFNC I need create a file(using identifier)... (2 Replies)
Discussion started by: anandapani
2 Replies

3. Shell Programming and Scripting

Search pattern and write line into another file

Hi, I have a file which contains the below details.. My requirement is to fetch all the lines which are starting with "ABC_XY_" into 1 file and rest of the lines (not starting with "ABC_XY_") into another file. Could you please help with what command needs to be used? file1.txt ----------... (12 Replies)
Discussion started by: satyaatcgi
12 Replies

4. Shell Programming and Scripting

Need to search a particular String form a file a write to another file using perl script

I have file which contains a huge amount of data. I need to search the pattern Message id. When that pattern is matched I need to get abcdeff0-1g6g-91g3-1z2z-2mm605m90000 to another file. Kindly provide your input. File is like below Jan 11 04:05:10 linux100 |NOTICE... (2 Replies)
Discussion started by: Raysf
2 Replies

5. Shell Programming and Scripting

Search and replace from file in awk using a 16 bit text file

Hello, Some time ago a helpful awk file was provided on the forum which I give below: NR==FNR{A=$0;next}{for(j in A){split(A,P,"=");for(i=1;i<=NF;i++){if($i==P){$i=P}}}}1 While it works beautifully on English and Latin characters i.e. within the ASCII range of 127, the moment a character beyond... (6 Replies)
Discussion started by: gimley
6 Replies

6. UNIX for Dummies Questions & Answers

Trying to understand a complex bit of code

Hi, To re-introduce myself, I'm a router guy trying to learn some scripting from the examples in my work place... In a ksh script, one of the script guys wrote the following and I am trying to understand it. I'm hoping someone can explain it to me. The script flow enters a case structure.... (5 Replies)
Discussion started by: Marc G
5 Replies

7. UNIX for Dummies Questions & Answers

Write 2nd and 3rd fields to a 4th file name?

I have a flatfile A.txt date|products|notes|location 121117|a108|this is a test|florida 121118|b111|just test it|tampa How do i write an awk to create a file name as location.txt and have products:notes awk -F'|' '{ print $2 ":" $3 }' A.txt > $4.txt I am sure it cannot write to... (5 Replies)
Discussion started by: sabercats
5 Replies

8. Shell Programming and Scripting

"Search for files in a file and write these files to another file which sould be in some other direc

Hi , Need to shell script to extracts files names and write those to another file in different directory. input file is inputfile.txt abc|1|bcd.dat 123 david123 123 rudy2345 124 tinku5634 abc|1|def.dat 123 jevid123 123 qwer2345 124 ghjlk5634 abc|1|pqr.txt 123 vbjnnjh435 123 jggdy876... (1 Reply)
Discussion started by: dssyadav
1 Replies

9. Shell Programming and Scripting

write shell script to search file in folder

hi .. i have a problem how to search file with date and version number(ms_2.0_dd/mm/yy_24)in folder.Here 24 is version number and compare the this file to other file which is in another folder and if does not match then copy this file to respective folder.Also copy different files in different... (1 Reply)
Discussion started by: shubhig15
1 Replies

10. Programming

How to write a code in C to search a file

Dear All, Plz give me some code that how can I search a file through a C program file may be in Present Directory or in its Sub-Directories and it should support Linux Platform. :mad: (6 Replies)
Discussion started by: krishna_sicsr
6 Replies
Login or Register to Ask a Question