Print lines that match certain criteria


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print lines that match certain criteria
# 1  
Old 03-14-2012
[Solved] Print lines that match certain criteria

Hi all

I have a text file with the following format:
id col1 col2 col3 col4 col5 col6 col7 ...
Code:
row1 0 0 0 0 0 0 0
row2 0 0 0 0 0 0 0
row3 0 0 0 0 0 0.2 0
row4 0 0 0 0 0 0 0
row5 0 0 0 0 0 0 0
row6 0 0 0 0.1 0 0 0
row7 0 0 0 0 0 0 0
row8 0 0 0 0 0 0 0
row9 0 0 0 0 0 0 0
...

The file is though much larger than this around 3000 rows and 5000 columns. Most lines have only zeros but values are in range of [0,1]. I am interested to print out all lines were there are some values > 0.

I created a short awk script like this:
Code:
awk 'BEGIN { OFS="" ; ORS="" } ; { for ( i=2; i<NF; i++ ) if($i > 0) print $i " "; print $NF "\n" }'

But that one printed only the values that were greater than zero (and also a little bit more) and I need to print the whole line.

I would really appreciate any help and I hope that my query is clear enoughSmilie.

-Gauti

Moderator's Comments:
Mod Comment Please use next time code tags for your code and data
# 2  
Old 03-14-2012
Try:
Code:
awk '{for (i=2;i<NF;i++) if($i>0){print;next}}' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 03-14-2012
Thank you bartus11 for a very quick answer, your code worked perfectly.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match the value & print lines from the match

Hello, I have a file contains two columns. I need to print the lines after “xxx” so i'm trying to match "xxx" & cut the lines after that. I'm trying with the grep & cut command, if there any simple way to extract this please help me. Sample file : name id AAA 123 AAB 124 AAC 125... (4 Replies)
Discussion started by: Shenbaga.d
4 Replies

2. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! 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. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

3. Shell Programming and Scripting

Match 2 different patterns and print the lines

Hi, i have been trying to extract multiple lines based on two different patterns as below:- file1 @jkm|kdo|aas012|192.2.3.1 blablbalablablkabblablabla sjfdsakfjladfjefhaghfagfkafagkjsghfalhfk fhajkhfadjkhfalhflaffajkgfajkghfajkhgfkf jahfjkhflkhalfdhfwearhahfl @jkm|sdf|wud08q|168.2.1.3... (8 Replies)
Discussion started by: redse171
8 Replies

4. Shell Programming and Scripting

Print lines that do not match the pattern

I need to print the lines that do not match a pattern. I tried using grep -v and sed -n '/pattern/!p', but both of them are not working as I am passing the pattern as variable and it can be null some times. Example ........ abcd...... .........abcd...... .........abcd......... (4 Replies)
Discussion started by: sunny1234
4 Replies

5. Shell Programming and Scripting

Print all lines before first match

Hi, I have this file. close block3c block3b block3a open close block2b block2a open close block1a open and I need : open block3a block3b block3c close (1 Reply)
Discussion started by: lasserfox
1 Replies

6. Shell Programming and Scripting

Print lines before and after pattern match

I am using Solaris, I want to print 3 lines before pattern match pattern 5 lines after pattern match Pattern is abcd to be searched in a.txt. Looking for the solution in sed/awk/perl. Thanks .. Input File a.txt: ================= 1 2 3 abcd 4 5 6 7 8 (7 Replies)
Discussion started by: manuswami
7 Replies

7. Shell Programming and Scripting

Need help in adding the column to set of lines that match the certain criteria

Hi, I need help to add a certain values to end of each line in the same file. Here is my input file name:aaa id :123 salary job qulification name:bbb id :124 salary job qulification name:ccc id :345 salary job qulification (2 Replies)
Discussion started by: jpkumar10
2 Replies

8. Shell Programming and Scripting

Replacing lines which match certain criteria

Hi, I have code which is like this <TABLE name="UsageDetail_24> <ROW> <Date24><!]></Date24> <Time24><!]></Time24> <Destination24><!]></Destination24> <Rate24><!]></Rate24> <Duration24><!]></Duration24> <Cost24><!]></Cost24> <Allowance24><!]></Allowance24> </ROW> <ROW>... (3 Replies)
Discussion started by: legolad
3 Replies

9. UNIX for Dummies Questions & Answers

Awk print all lines on match?

Ok so I can use awk to match a pattern and print the whole line with print $0. Is there any way to just tell awk to print every line of output when the pattern matches? I'm having it wait for the word error and then print that entire line. But what I actually need to see is all the following... (9 Replies)
Discussion started by: MrEddy
9 Replies

10. Shell Programming and Scripting

how to print all lines from a second match

I am trying to parse iostat output for io issues.. I want to print all lines including second occurance of 'extended' till EOF(end of file). Can we do that using awk or sed one liners or do we need a script for it? (1 Reply)
Discussion started by: kchinnam
1 Replies
Login or Register to Ask a Question