Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Identifying the first line that has zeros Post 302536501 by ctsgnb on Tuesday 5th of July 2011 03:21:58 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 04:16 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy