awk if conditional, multiple lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk if conditional, multiple lines
# 1  
Old 01-18-2012
awk if conditional, multiple lines

Hi experts,

I have a file with hundreds of lines that looks something like this:
Code:
Cake	1	3	4	2	3	1	3
Onion	3	2	4	1	3	4	2
Apple	2	3	4	4	1	2	1
Orange	4	3	4	1	2	4	1
Cake	3	1	4	2	4	2	1
Onion	2	4	2	1	4	2	1
Apple	4	3	3	3	3	2	1
Orange	2	1	2	2	2	4	1
Cake	4	3	4	3	1	3	3
...

I'm trying to list all the row numbers, except for "bad" lines with the following criteria:
a) row numbers in which the last column meets some threshold, for instance >1.5
AND
b) The next 3 rows after the row that is excluded in a)

For instance, in the sample above, the desired output would be:

Code:
6,7,8

So far, I'm able to get the "bad" lines with:

Code:
awk 'BEGIN {ORS=","}
{k=1.7}
{if($8>k)
print NR, NR+1, NR+2, NR+3
}' inputfile

but I'm a little lost on how to get the opposite, or "good" lines. This also can be redundant, and it prints extra row values if the last row meets the "bad" line criteria. It seems like the wrong direction to go.

Would I have to go through the file twice, once to flag "bad" lines and again to print "good" lines (by excluding "bad" lines)?

Hope this makes sense. Thanks for the help!

Last edited by david_seeds; 01-18-2012 at 07:09 PM.. Reason: typo in my awk example
# 2  
Old 01-18-2012
Code:
awk 'BEGIN{ok=0} 
       $(NR)>1.7 {ok=3; next} 
       {if(ok>0) {ok--; next}
         else { print $0}  }' inputfile

Assuming I got what you want, try that.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 01-18-2012
ah, brilliants, works great. didn't think of using "--"!

thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

awk multiple lines

When your data is consistent it is easy to use awk with multiple lines like this. Can we please make this portable so I can use this in both RHEL and AIX? awk '{RS="/directory1" } $7 ~ /drwxr-xr-x/ {print $1 " " $7}' file What do I do when the data is not consistent? When your data is not... (2 Replies)
Discussion started by: cokedude
2 Replies

2. Shell Programming and Scripting

Merging multiple lines to columns with awk, while inserting commas for missing lines

Hello all, I have a large csv file where there are four types of rows I need to merge into one row per person, where there is a column for each possible code / type of row, even if that code/row isn't there for that person. In the csv, a person may be listed from one to four times... (9 Replies)
Discussion started by: RalphNY
9 Replies

3. Shell Programming and Scripting

awk to filter multiple lines

Hi. I need to filter lines based upon matches in multiple tab-separated columns. For all matching occurrences in column 1, check the corresponding column 4. IF all column 4 entries are identical, discard all lines. If even one entry in column 4 is different, then keep all lines. How can I... (5 Replies)
Discussion started by: owwow14
5 Replies

4. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

5. Shell Programming and Scripting

inserting multiple lines with awk

awk '/<login-module code="com.nlayers.seneca.security.LdapLogin" flag="sufficient">/{p++} /<login-module code="com.nlayers.seneca.security.LdapLogin" flag="sufficient">/ && p==1 {$0="Mulitple lines here\n"$0}1' login-config.xml In the above awk code inside shell script, i am having problems when... (1 Reply)
Discussion started by: sunrexstar
1 Replies

6. Shell Programming and Scripting

displaying multiple lines using AWk

say I'm doing awk 'NR==534' Is there a way to display 534 535 536 537? without appending to a variable? per line? maybe an easier way with a different command? My first impression was NR==534-537 but that would be too easy :P (2 Replies)
Discussion started by: puttster
2 Replies

7. Shell Programming and Scripting

AWK record in multiple lines

Hi everyboby this is my problem I Have this input 1111;222 222 2;333 3333;4444 111; 22222;33 33; 444 and I need this output 1111;2222222;3333333;4444 (15 Replies)
Discussion started by: agritur
15 Replies

8. Shell Programming and Scripting

Awk to Break lines to multiple lines.

Input File: nawk -F "|" '{ for(i=1;i<=NF;i++) { if (i == 2) {gsub(",","#",$i);z=split($i,a,"")} else if (i == 3) {gsub(",","#",$i);z=split($i,b,"")} } if(z > 0) for(i=1;i<=z;i++) print $1,a,"Test"; if(w > 0) for(j=1;j<=w;j++) ... (1 Reply)
Discussion started by: pinnacle
1 Replies

9. UNIX for Dummies Questions & Answers

print multiple lines with awk

Hi everyone! I'm not new to Unix, but I've never used awk before. I tried to look up this information on several sites and forums, I also looked in the documentation but I haven't found a solution yet. I would like to print the previous 3 lines before and the following 4 lines after the... (6 Replies)
Discussion started by: djcsabus
6 Replies

10. Shell Programming and Scripting

Conditional concat lines awk

Hello, I have a text file like this: NONE FILE_Rename frompath: /log_audit/AIX/log/current/AIXAFTPP.log NONE FILE_Unlink filename /audit/tempfile.14041142 NONE FILE_Rename ... (8 Replies)
Discussion started by: carloskl
8 Replies
Login or Register to Ask a Question