![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Compare multiple fields in file1 to file2 and print line and next line | gillesc_mac | Shell Programming and Scripting | 7 | 03-16-2009 06:26 AM |
| awk/sed search lines in file1 matching columns in file2 | floripoint | Shell Programming and Scripting | 1 | 12-17-2008 11:36 PM |
| extracting lines from a file1 which maches a pattern in file2 | smriti_shridhar | Shell Programming and Scripting | 11 | 07-31-2008 01:56 AM |
| match value from file1 in file2 | myguess21 | Shell Programming and Scripting | 2 | 02-21-2008 11:39 AM |
| Awk Compare f1,f2,f3 of File1 with f1 of File2 | RacerX | Shell Programming and Scripting | 6 | 11-09-2007 01:34 AM |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
print contents of file2 for matching pattern in file1 - AWK
File1 row is same as column 2 in file 2. Also file 2 will either start with A, B or C. And 3rd column in file 2 is always F2. When column 2 of file 2 matches file1 column, print all those rows into a separate file. Here is an example. file 1: Code:
100 103 104 108 file 2: Code:
A|100|F2|hello B|100|F2|djhbsdhjf B|100|F2|dksadbkdfd C|100|F2|djsbdjinldf A|101|F2|hellodfd B|101|F2|djhbsdhjdff B|101|F2|dksadbkdfgd C|101|F2|djsbdjinlgfg A|102|F2|hellodfgfd B|102|F2|djhbsdhjfgf C|102|F2|djsbdjinlhgf A|103|F2|hellohggg B|103|F2|djhbsdhjhjhj B|103|F2|dksadbkdfdr C|103|F2|djsbdjinlfgf A|104|F2|hellofg B|104|F2|djhbsdhjfgf B|104|F2|dksadbkhfgg C|104|F2|djsbdjinlhgh A|105|F2|hellohgh B|105|F2|djhbsdhjdsgh B|105|F2|dksadbkds C|105|F2|djsbdjinlds A|108|F2|hello B|108|F2|djhbsdhj B|108|F2|dksadbk C|108|F2|djsbdjinl OUTPUT: Code:
A|100|F2|hello B|100|F2|djhbsdhjf B|100|F2|dksadbkdfd C|100|F2|djsbdjinldf A|103|F2|hellohggg B|103|F2|djhbsdhjhjhj B|103|F2|dksadbkdfdr C|103|F2|djsbdjinlfgf A|104|F2|hellofg B|104|F2|djhbsdhjfgf B|104|F2|dksadbkhfgg C|104|F2|djsbdjinlhgh A|108|F2|hello B|108|F2|djhbsdhj B|108|F2|dksadbk C|108|F2|djsbdjinl I am trying awk...but no luck...here is what i am trying Code:
awk -v i="1" 'BEGIN { FS="|" }
FR==NR
{
a[i]=$2
if (a[i]==a[i-1]) { h[$2,i]=$0; i++ }
else { if (i==1) { h[$2,i]=$0; i++; }
if (i!=1) { h[$2,i]=$0; i=1; ; }
}
next
}
{
for (j=1;j<1000;j++)
{
if (h[$0,j]!="") { print h[$0,j]
}
}
next
}' file2 file1 > ouputfile
************************************************************ i do not want to use for/while unix loops as it is nt efiicient .......... Last edited by vgersh99; 09-07-2009 at 08:11 PM.. Reason: code tags, PLEASE! |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|