Printing the line number of first column found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing the line number of first column found
# 1  
Old 03-13-2012
Bug Printing the line number of first column found

Hello,

I have a question on how to find the line number of the first column that contains specific data. I know how to print all the line numbers of those columns, but haven't been able to figure out how to print only the first one that is found.

For example, if my data has four columns:
115 34 8 0
85 112 97 0
147 77 70 0
136 35 9 0
130 31 9 0
171 120 64 1
180 141 78 1
135 87 119 1
97 130 80 1
135 177 89 1

I want to scan the file till I get to the line which has '1' in the fourth column and then print out the line number at that point, and then exit. This is what I have so far:
Code:
 awk -F " " '{if($4 == "1") print NR}' filename

Is there a way to do this without using awk as well?

Thanks!
# 2  
Old 03-13-2012
Code:
awk '$4==1 {print NR; exit(0)}' filename

# 3  
Old 03-13-2012
Thanks Smilie But is there a way to do this without using awk?
# 4  
Old 03-14-2012
Code:
$ i=0;while read a b c d; do i=$(expr $i + 1) && [ "$d" -eq "1" ] && echo $i && break;done < input.txt                                               
6

---------- Post updated at 11:54 AM ---------- Previous update was at 11:51 AM ----------

Code:
$ perl -lane 'if ($F[3]==1){print $.;exit}' input.txt
6

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Avoid printing entire line if string not found

so im searching the process table with: ps -ef | awk -F"./rello.java" '{ print substr($0, index($0,$2)) }' I only want it to print everything that's infront of the "./rello.java". That's because im basically getting the arguments that was passed to the rello.java script. this works. ... (2 Replies)
Discussion started by: SkySmart
2 Replies

2. Shell Programming and Scripting

Print line when closest number if found

so i have a code that identifies which value is the closest to a value provided by the user. awk -F"," -v c=${COLUMN} -v t=${USTIME} '{a=$c}END{ asort(a);d=a-t;d=d<0?-d:d;v = a for(i=NR-1;i>=1;i--){ m=a-t;m=m<0?-m:m if(m<d){ ... (3 Replies)
Discussion started by: SkySmart
3 Replies

3. Homework & Coursework Questions

Lex: analyzing a C file and printing out identifiers and line numbers they're found on

Florida State University, Tallahassee, FL USA, Dr. Whalley, COP4342 1. The problem statement, all variables and given/known data: Create a lex specification file that reads a C source program that ignores keywords and collects all identifiers (regular variable names) and also displays the line... (3 Replies)
Discussion started by: D2K
3 Replies

4. Shell Programming and Scripting

Printing Number of Fields with the line number

Hi, How to print the number of fields in each record with the line number? Lets saw I have 3212|shipped|received| 3213|shipped|undelivered| 3214|shipped|received|delivered I tried the code awk -F '|' '{print NF}' This gives me ouput as 3 3 4 (5 Replies)
Discussion started by: machomaddy
5 Replies

5. Shell Programming and Scripting

search a string in a particular column of file and return the line number of the line

Hi All, Can you please guide me to search a string in a particular column of file and return the line number of the line where it was found using awk. As an example : abc.txt 7000,john,2,1,0,1,6 7001,elen,2,2,0,1,7 7002,sami,2,3,0,1,6 7003,mike,1,4,0,2,1 8001,nike,1,5,0,1,8... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

6. Shell Programming and Scripting

Printing the line number in bash script

Hi, I would like to know how do I print the line # in a script. My requirement is, I have a script which is about ~5000 lines long. If there are any errors happen I just exit. And I would like to add the line # of the script where the error happened. Thanks, (6 Replies)
Discussion started by: suryaemlinux
6 Replies

7. Shell Programming and Scripting

Comparing two files and printing 2nd column if match found

Hi guys, I'm rather new at using UNIX based systems, and when it comes to scripting etc I'm even newer. I have two files which i need to compare. file1: (some random ID's) 451245 451288 136588 784522 file2: (random ID's + e-mail assigned to ID) 123888 xc@xc.com 451245 ... (21 Replies)
Discussion started by: spirm8
21 Replies

8. Shell Programming and Scripting

regarding about printing line number

Hello, I am testing some data to get line number at cursor position 9 and found some problem, the code is below.Assume we got 3 attribute. At second attribute, there are some data(eg.A41/A6) missing like at the fourth and six line 11006 A41 1888 11006 ... (7 Replies)
Discussion started by: davidkhan
7 Replies

9. Shell Programming and Scripting

printing line number

hi, i have a file, i need to search for a string , if the line contains i need to print that line number and line , please help thanks in advance Satya (5 Replies)
Discussion started by: Satyak
5 Replies

10. Shell Programming and Scripting

printing a line number using awk

Hi Chaps, I'm trying to print the line number of a comma delimited file where the second field in the line is blank using AWK. Here is the code I have so far where am I going wrong. It is the last column in the file. nawk -v x==0 'BEGIN {FS=",";OFS=","} x++ if ($2 == " ") print $x' bob.tst ... (3 Replies)
Discussion started by: rjsha1
3 Replies
Login or Register to Ask a Question