awk print only select records from file2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk print only select records from file2
# 8  
Old 08-02-2011
Code:
awk -F'~' '
  NR==FNR {a[$1]=$6;}
  NR!=FNR && a[$1]!=$6' file1 file2

# 9  
Old 08-02-2011
(I do not testing, but used code is not good (besides bad written) and your understanding is not exactly correct.)

Code:
 'NR==FNR{p=$1;$1=x;A[p]=$0;next}

- what the 'p' and '$1=x' do here?!
Apsolutely useless! That is enough:

Code:
 'NR==FNR{A[$1]=$0;next}

... then append FS (a comma) ...
- No! FS is set to '[ \t]*' - spaces

... $2=A[$1] FS $2;print then append FS (a comma) followed by A[$1] ...
- ??? did you try to say ' a comma (in 'print') appends a FS? That would be correct!
- that statement do not print anything, but reassign the $2 (the seconf field) by the A[$1], followed by FS (whatever it is; in your code it a space), folloved by the original value of the $2.

Also, I think the 'FS=~' should be with the '\': 'FS=\~' - the '~' by shell going to be interpreted and substituted as a home-directory.

And, the used 'awk' (should it be 'nawk' to use the FNR and '?:'-operator?) code is really weird!
It reads first file with delimiter '~' into array A, indexing by first '~' field;
check the second file,having delimitered on spaces;
having the same first field in the second file as any line in the first file, it applys to the second space-delimitered field the line from the first file (with ~-delimiters) after the '~': so, the first field is just repeated.

I could hardly guess how after all the output could be used!

Last edited by radoulov; 08-02-2011 at 03:03 PM.. Reason: Code tags!
# 10  
Old 08-03-2011
alex

you did not give alternative solution to the code.
Do you have one?

The awk solution did not work wither
# 11  
Old 08-09-2011
Quote:
Originally Posted by sigh2010
alex

you did not give alternative solution to the code.
Do you have one?
What's wrong with yazu solution?! -(to your second part of your replay!)

(To your first statement: )
Actualy, you did not sound like you are looking for final line of code to do your job: that I most likely would not even start to responde!
I (seems wrongly) guess that you are looking for understanding how to go your stuff and for that bring up your work on (seems to you) simular piece of code.
I was trying to help you in that (in understanding).

To do your stuff after all explanation you should not have any problem, IMHO.

Speaking about the task: The answer is reflection of the question. In other words: 50% of an answer is a good question and in the question!
You did not ask enough clear to have a reasonable answer.
By discution I see that the delimiter in your file is the '~'.
Do you have any other restictionons?
Maybe files are big to be a problem to have the 1-st+6-th column be kept in memory (that I could see as a possible problem in yazu's solution).. maybe something else?

Alright, assume both file are delimitered by the same '~' delimiter.
Assume by 'comparing column 1 and column 6' means (as used later) file-2's '~'-delimitered columns 1 and 6 are not the same as any record's the same columns in the file-1.
Assume the 'do not match ' means (as, again, understand later) never been found in the file-1 in the same positions in any record simultaneosly.

On all these requirements the first 'yazu' solution completely fine: build a hash of (1-st + 6-th) columns of the first file and print the second file's record when the same combination (not by value, but the combination: 1-st + 6-th columns of the second file's record) hash member is not exist!

What the problem do you have with that solution?
Not clear? (Probably, already not, but) here are few words:
- Only on first (by the NR==FNR) file the hash 'a' is built (the '++' in not nessesary, just shortest to be sure that the such index hash member is any how referensed.)
- On the second file the check of the 'a' hash is in the condition of selection for an action that is not specified; so, default is used: print a full (current) record.
File's delimiter is defined as command line parameter by the -F option. That is done in single quotes; so the special meaning of the '~' is not processed by the shell.

What else?!
Perfect solution to the described by my assumptions' task!
Something is not good in assumptions? - Explain!
Something does not work? - show that; but, better think and try out by yourself. When you stuck - bring it here.
That way help would be helpfull!
Just to solve a task helps just to cheaters or liers, who pretends to know and has no any interest to the subject.

I do not believe anyone could be not agree with my statements!
That why I have no interest to help people who do not think on task, unable to present a question reasonably and do not care answers without complete and final solution of the task.

Good luck!


Quote:
Originally Posted by sigh2010
The awk solution did not work wither
How it is not work?
Should it be 'nawk'?
Show some result of test!
# 12  
Old 08-09-2011
the solution by yazu was correct.
my system could not use awk properly, it is setup to use nawk

this worked
Code:
nawk -F~ 'NR==FNR{A[NR]=$1$16;next}$1$16!=A[FNR]' f1 f2

thanks

Last edited by Franklin52; 08-10-2011 at 10:36 AM.. Reason: Please use code tags for data and code samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk- Indexing a list of numbers in file2 to print certain rows in file1

Hi Does anyone know of an efficient way to index a column of data in file2 to print the coresponding row in file1 which corresponds to the data in file2 AND 30 rows preceding and after the row in file1. For example suppose you have a list of numbers in file2 (single column) as follows:... (6 Replies)
Discussion started by: Geneanalyst
6 Replies

2. Shell Programming and Scripting

awk print matching records and occurences of each record

Hi all , I have two files : dblp.xml with dblp records and itu1.txt with faculty members records. I need to find out how many dblp records are related to the faculty members. More specific: I need to find out which names from itu1.txt are a match in dblp. xml file , print them and show how many... (4 Replies)
Discussion started by: iori
4 Replies

3. Shell Programming and Scripting

To select non-duplicate records using awk

Friends, I have data sorted on id like this id addressl 1 abc 2 abc 2 abc 2 abc 3 aabc 4 abc 4 abc I want to pick all ids with addressesses leaving out duplicate records. Desired output would be id address 1 abc 2 abc 3 abc 4 abc (5 Replies)
Discussion started by: paresh n doshi
5 Replies

4. Shell Programming and Scripting

awk read in file1, gsub in file2, print to file3

I'm trying to use awk to do the following. I have file1 with many lines, each containing 5 fields describing an individual set. I have file2 which is a template config file with variable space holders to be replaced by the values in file1. I would like to substitute each set of values in file1 with... (6 Replies)
Discussion started by: msmehaffey
6 Replies

5. Shell Programming and Scripting

Print unique records in 2 columns using awk

Is it possible to print the records that has only 1 value in 2nd column. Ex: input awex1 1 awex1 2 awex1 3 assww 1 ader34 1 ader34 2 output assww 1 (5 Replies)
Discussion started by: quincyjones
5 Replies

6. Shell Programming and Scripting

AWK, print no of records after pattern match.

Hi ALL :). i have a file, cat 3 + dog 5 + rat 6 - i want to print no of record having pattern "+". thanks in advance :confused:. (2 Replies)
Discussion started by: admax
2 Replies

7. UNIX for Advanced & Expert Users

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: 100 103 104 108 file 2: ... (6 Replies)
Discussion started by: i.scientist
6 Replies

8. Shell Programming and Scripting

Based on num of records in file1 need to check records in file2 to set some condns

Hi All, I have two files say file1 and file2. I want to check the number of records in file1 and if its atleast 2 (i.e., 2 or greater than 2 ) then I have to check records in file2 .If records in file2 is atleast 1 (i.e. if its not empty ) i have to set some conditions . Could you pls... (3 Replies)
Discussion started by: mavesum
3 Replies

9. UNIX for Dummies Questions & Answers

AWK ??-print for fields within records in a file

Hello all, Would appreciate if someone can help me out on the following requirement. INPUT FILE: -------------------------- TPS REPORT abc def ghi jkl mon pqr stu vrs lll END OF TPS REPORT TPS REPORT field1 field2 field3 field4 field5 field6 (8 Replies)
Discussion started by: hyennah
8 Replies

10. Shell Programming and Scripting

Using a variable to select records with awk

As part of a bigger task, I had to read thru a file and separate records into various batches based on a field. Specifically, separate records based on the value in the batch field as defined below. The batch field left-justified numbers. The datafile is here > cat infile 12345 1 John Smith ... (5 Replies)
Discussion started by: joeyg
5 Replies
Login or Register to Ask a Question