Visit Our UNIX and Linux User Community


Identifying the first line that has zeros


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Identifying the first line that has zeros
# 1  
Old 07-05-2011
Identifying the first line that has zeros

If I have a file like:

Code:
9350.  0.288426
9370.  0.320469
9390.  0.394475
9410.  0.353157
9430.  0.336001
9450.  0.336692
9470.  0.356827
9490.  0.359891
9510.  0.346305
9530.  0.356506
9550.  0.348306
9570.  0.36832
9590.  0.379067
9610.  0.0246704
9630.  0
9650.  0
9670.  0
9690.  0
9710.  0
9730.  0
9750.  0
9770.  0
9790.  0
9810.  0
9830.  0
9850.  0
9870.  0
9890.  0
9910.  0
9930.  0
9950.  0

And I want to automatically find the line where column 2 is a starts to be zero (in this case = 15) how can I do that?
# 2  
Old 07-05-2011
This one...?
Code:
sed '/ 0$/!d;q' inputfile

This User Gave Thanks to michaelrozar17 For This Post:
# 3  
Old 07-05-2011
Code:
$ awk '!$2&&!c++' tst
9630.  0
$ awk '!$2&&!c++{print NR}' tst
15

This User Gave Thanks to ctsgnb For This Post:
# 4  
Old 07-05-2011
Code:
# sed -n '/[^ ]*[^\t ]*0$/{p;q}' file ## it works the lines that has 2 columns

Code:
# awk '$2~/^0$/{print;exit}' file  ## it works the lines that has more than 2 columns when the line found firstly otherwise it works like sed

regards
ygemici
# 5  
Old 07-05-2011
Quote:
Originally Posted by ctsgnb
Code:
$ awk '!$2&&!c++{print NR}' tst


How can I include a sentence like:

The first zero is in line ...

I tried

Code:
awk '!$2&&!c++{print "The first zero for file1 is" NR}'

But didn't work Smilie
# 6  
Old 07-05-2011
Quote:
Originally Posted by cosmologist
How can I include a sentence like:

The first zero is in line ...

I tried

Code:
awk '!$2&&!c++{print "The first zero for file1 is" NR}'

But didn't work Smilie
You forgot the very last argument : the filename on which you run you awk command !!!

Code:
$ awk '!$2&&!c++{print "The first zero for file1 is " NR}' tst
The first zero for file1 is 15

You can enhance the code so it pick up the name of the file that is currently being processed by awk :

Code:
awk '!$2&&!c++{print "The first zero for " FILENAME " is " NR}' yourfile

Example:

Code:
 
[ctsgnb@shell ~]$ cat tst
9350.  0.288426
9370.  0.320469
9390.  0.394475
9630.  0
9650.  0
9890.  0
9910.  0
9930.  0
[ctsgnb@shell ~]$ cat tst2
9590.  0.379067
9610.  0.0246704
9620.  0
9630.  0
9640.  0
9650.  0
9660.  0
[ctsgnb@shell ~]$ awk '!$2&&!c++{print "The first zero for " FILENAME " is " NR}' tst
The first zero for tst is 4
[ctsgnb@shell ~]$ awk '!$2&&!c++{print "The first zero for " FILENAME " is " NR}' tst2
The first zero for tst2 is 3

---------- Post updated at 09:21 PM ---------- Previous update was at 09:07 PM ----------

but my code may behave erroneously : if no value exists in $2 it will report the line number, so maybe you would prefer ygemici code which also have the advantage not to scan further line as soon as the first "2cnd-column-equal-zero" line has been found

Code:
awk '$2~/^0$/{print "The first zero for " FILENAME " is " NR;exit}' input


Last edited by ctsgnb; 07-05-2011 at 04:15 PM..
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #844
Difficulty: Medium
Bittorent, Gnutella and other distributed file transfers systems use do not user peer-to-peer, by default.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Identifying a sentence and putting it on a new line

I am revisiting the problem of sentence splitting. I have a Perl Script which splits a para into sentences, but acronyms and short forms create an issue #!/usr/bin/perl use feature qw/say/; use strict; use warnings; my $s; my @arr; while(<>) { chomp $_; $s .= $_ . " "; } @arr... (2 Replies)
Discussion started by: gimley
2 Replies

2. Shell Programming and Scripting

Script for identifying and deleting dupes in a line

I am compiling a synonym dictionary which has the following structure Headword=Synonym1,Synonym2 and so on, with each synonym separated by a comma. As is usual in such cases manual preparation of synonyms results in repeating the synonym which results in dupes as in the example below:... (3 Replies)
Discussion started by: gimley
3 Replies

3. Shell Programming and Scripting

tr command to delete zeros

Hi, I have a input string 0000106 I need to extract the number after leading zeros ie the number 106. I used the command tr -d "0" and got the output as 16. Could any one of you please help me in using the tr command to get the output 106. Thanks in advance.... (2 Replies)
Discussion started by: dean_amrita
2 Replies

4. Shell Programming and Scripting

Padding with zeros.

Hi Friends, I would like to left pad with "0's" on first column say (width six) I have a large file with the format: FILE: 1: ALFRED 84378 NY 8385: JAMES 88385 FL 323: SMITH 00850 TX My output needs to be like: 000001: ALFRED 84378 NY 008385: JAMES 88385 FL 000323: SMITH... (10 Replies)
Discussion started by: sbasetty
10 Replies

5. UNIX for Advanced & Expert Users

trimming zeros

Hi, I want to trim +with leading zero's with amount fields.I know using awk for trimming leading zeros with +,but I want get the entire row itself. cat file_name |awk -F " " '{printf "%14.4f%f\n",$4}' ex: 10 xyz bc +00000234.4500 20 yzx foxic +002456.000 Expexted 10 xyz bc... (3 Replies)
Discussion started by: mohan705
3 Replies

6. UNIX for Dummies Questions & Answers

Left filled with zeros

Hi A question about numeric display there... Is there any option in awk that would prints a number with "left filled with zero" format, and how does it work with variable length definition? For instance IŽd like my variable to be left filled with 0s for a length of 3 in total: 4 would be... (3 Replies)
Discussion started by: Indalecio
3 Replies

7. Shell Programming and Scripting

Pad zeros to a number

Pad zeros to a number and assign it to a variable like i get 1 in $i ,i want it to be $i as 01 (6 Replies)
Discussion started by: anumkoshy
6 Replies

8. UNIX for Dummies Questions & Answers

pad Zeros

Hi can I know command to pad Zeros to a value I get 16 and I need to send 0000000016 (5 Replies)
Discussion started by: mgirinath
5 Replies

9. Shell Programming and Scripting

Leading zeros

How to insert leading zeros into a left-justisfied zip code? e.g. Zip code is written as 60320 which is left-justified to make it be read as 0060320. We have to move it to right-justifiable then insert 2 leading zeros into it... ;) (1 Reply)
Discussion started by: wtofu
1 Replies

10. UNIX for Dummies Questions & Answers

identifying duplicates line & reporting their line number

I need to find to find duplicate lines in a document and then print the line numbers of the duplicates The files contain multiple lines with about 100 numbers on each line I need something that will output the line numbers where duplicates were found ie 1=5=7, 2=34=76 Any suggestions would be... (5 Replies)
Discussion started by: stresslog
5 Replies

Featured Tech Videos