awk to print matching lines in files that meet critera


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to print matching lines in files that meet critera
# 8  
Old 09-14-2016
Hi,

Do you mind explaining use of
Code:
> 0

in
Code:
getline < "f1" > 0

# 9  
Old 09-14-2016
Quote:
Originally Posted by greet_sed
Hi,

Do you mind explaining use of
Code:
> 0

in
Code:
getline < "f1" > 0

The getline function returns 1 for successful input, zero for end-of-file, and −1 for an error. So:
Code:
while((getline < "f1") > 0) x[$2]=$0

reads successive lines from a file named f1 until EOF or a read error is detected. And, for each line successfully read from that file, an element in the array x with the array index specified by the 2nd field found in that line is created or updated to have the value specified by the entire contents of that line.

Last edited by Don Cragun; 09-14-2016 at 05:27 PM.. Reason: Fix typo: s/ready/read/
This User Gave Thanks to Don Cragun For This Post:
# 10  
Old 09-15-2016
Quote:
Originally Posted by Don Cragun
What you are saying is true if both "arrow" operators are comparison operators; but it is not true when one of the "arrow" operators is a redirection operator. Historic versions of awk based on AT&T UNIX System utility code do one thing. GNU awk does the other.
IMHO you are mixing operator overloading with operator precedence...the left arrow stands for "input" while the right arrow tests for "greater than" is evident from the program's context and the rules of precedence and associativity have nothing to do with the interpretation of these operators...that being said the implied parenthesization of (getline < "f1" > 0) is ((getline < "f1") > 0)

---------- Post updated at 11:49 PM ---------- Previous update was at 11:42 PM ----------

Quote:
Originally Posted by greet_sed
Hi,

Do you mind explaining use of
Code:
> 0

in
Code:
getline < "f1" > 0

You need "> 0" because if file f1 doesnt exist getline returns -1 which in awk terms is true thereby producing an infinite loop...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print lines that meet conditions and have value in another file

I am trying to use awk to print lines that satisfy either of the two conditions below: condition 1: $2 equals CNV and the split of $3, the value in red, is greater than or equal to 4. ---- this is a or so I think condition 2: $2 equals CNV and the split of $3, the value in red --- this is a... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Print header and lines that meet both conditions in awk

In the awk below I am trying to print only the header lines starting with # or ## and the lines that $7 is PASS and AF= is less than 5%. The awk does execute but returns an empty file and I am not sure what I am doing wrong. Thank you. file ... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Shell Programming and Scripting

awk to capture lines that meet either condition

I am trying to modify and understand an awk written by @Scrutinizer The below awk will filter a list of 30,000 lines in the tab-delimited file. What I am having trouble with is adding a condition to SVTYPE=CNV that will only print that line if CI=,0.95: portion in blue in file is <1.9. The... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

awk to print fields that match using conditions and a default value for non-matching in two files

Trying to use awk to match the contents of each line in file1 with $5 in file2. Both files are tab-delimited and there may be a space or special character in the name being matched in file2, for example in file1 the name is BRCA1 but in file2 the name is BRCA 1 or in file1 name is BCR but in file2... (6 Replies)
Discussion started by: cmccabe
6 Replies

5. UNIX for Dummies Questions & Answers

awk - Print lines if only matching key is found

I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. Thanks a lot. Any help is appreciated. Script I am using: awk 'FNR == NR && ! /^]*$/ {... (9 Replies)
Discussion started by: High-T
9 Replies

6. UNIX for Dummies Questions & Answers

Print lines meet requirement

Dear Masters, I have 2 files input below file1 8269229289|CROATIA|LUX 8269229412|ASIA|LUX 8269229371|EUROPE|LUX 8269229355|LANE|LUX 8269229469|SWISS|LUX 8269229477|HAMBURG|LUX 8269229484|EGYPT|LUX 8269229485|GERMANY|LUX 8269229498|CROATIA|LUX File2 8269229289|1100100020... (6 Replies)
Discussion started by: radius
6 Replies

7. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

8. Shell Programming and Scripting

Look up between 2 files and print matching lines

Hi, I have 2 large log files in .gz format file 1 contains abcde 12345 23456 . . . . . . . . 09123 (8 Replies)
Discussion started by: aravindj80
8 Replies

9. Shell Programming and Scripting

awk print non matching lines based on column

My item was not answered on previous thread as code given did not work I wanted to print records from file2 where comparing column 1 and 16 for both files find rows where column 16 in file 1 does not match column 16 in file 2 Here was CODE give to issue ~/unix.com$ cat f1... (0 Replies)
Discussion started by: sigh2010
0 Replies

10. Shell Programming and Scripting

Print lines matching value(s) in other file using awk

Hi, I have two comma separated files. I would like to see field 1 value of File1 exact match in field 2 of File2. If the value matches, then it should print matched lines from File2. I have achieved the results using cut, paste and egrep -f but I would like to use awk as it is efficient way and... (7 Replies)
Discussion started by: SBC
7 Replies
Login or Register to Ask a Question