How to display the line number of file while searching for a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to display the line number of file while searching for a pattern
# 1  
Old 02-06-2012
How to display the line number of file while searching for a pattern

Code:
awk 'BEGIN{IGNORECASE=1} /error|warning|exception/  { ++x } END { print x }' filename

The above command returning the number of times the pattern present in the file. But I want the the line number as well. please help me out

Last edited by methyl; 02-06-2012 at 12:25 PM.. Reason: please use code tags
# 2  
Old 02-06-2012
Hi arukuku,

But there could be many line numbers. How do you want the output?

Regards,
Birei
# 3  
Old 02-06-2012
The builtin awk variable NR stands for the line no...so knowing that you can add some code to your awk script to print out NR whenver the desired pattern is matched...
# 4  
Old 02-06-2012
Birei , I want the line number for which the pattern matches.
Sham rock, Ya tryin to do that. But not able to do it . I want to do it in awk only
# 5  
Old 02-06-2012
where it says ++x you can also print the line number (NR)
# 6  
Old 02-06-2012
Code:
grep -n 'pattern'

# 7  
Old 02-06-2012
Good thought, but we would need ERE for different choices and we need to ignore case, so grep -Ein 'pattern' would be the ticket, but then we would not get the total number of matches, although we could run it through wc -l separately in case we'd really need to know..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep pattern after specific line number in a file

Hi guys, I am running a while loop in a script ro read a file line by line. Now I want to run a grep only on the lines below the line I am that is being read by the while loop. Eg: If my while loop is on line 4 of the file, the grep only runs below line 4 and does not include line 1,2... (3 Replies)
Discussion started by: Junaid Subhani
3 Replies

2. Shell Programming and Scripting

How to split a file based on pattern line number?

Hi i have requirement like below M <form_name> sdasadasdMklkM D ...... D ..... M form_name> sdasadasdMklkM D ...... D ..... D ...... D ..... M form_name> sdasadasdMklkM D ...... M form_name> sdasadasdMklkM i want split file based on line number by finding... (10 Replies)
Discussion started by: bhaskar v
10 Replies

3. UNIX for Dummies Questions & Answers

Get line numbers while searching the pattern in log

Hi Folks, I am searching for a pattern in logs through putty by opening the file in vi editor and reaching to the last of the file by $ and then searching the pattern , lets say I have to search the pattern abc then it would be ?abc Now I want line numbers along with the matching pattern to be... (3 Replies)
Discussion started by: SankalpS
3 Replies

4. Shell Programming and Scripting

Place digit in front of the line searching pattern using sed command

hi All, i want to add the single digit front of the line in the report file and string compare with pattern file. patter file: pattern1.txt pattern num like 4 love 3 john 2 report file: report.txt i like very much but john is good boy i will love u so after execute... (9 Replies)
Discussion started by: krbala1985
9 Replies

5. Shell Programming and Scripting

Display string at line number

I have a code here , which should display lines 6,10,14,18,35 of a text file #!/bin/ksh line=6 line=10 line=14 line=18 line=35 for i in 1 2 3 4 5 do val=`echo ${line}` act=`awk 'NR~/^($val)$/' db_CHECKOUT.txt` done; This code is not working. The purpose of the line below is... (3 Replies)
Discussion started by: njafri
3 Replies

6. Shell Programming and Scripting

Searching a pattern in file and deleting th ewhole line containing the pattern

Hi All, Please can someone assist in the script I have made that searches a pattern in a file and delete the whole line containing the pattern. #!bin/sh # The pattern that user want to add to the files echo "Enter the pattern of the redirect" read value # check if the user has... (1 Reply)
Discussion started by: Shazin
1 Replies

7. UNIX for Dummies Questions & Answers

Awk help needed for display particular field alone for searching pattern

Hi, I have a requirement for taking an particular number in a log file. if i grep for the particular string it will retrieve the entire line for the particular string. but i want to display only the string from each line which i am searching for, Note: The searching field varies its position... (3 Replies)
Discussion started by: senthilkumar_ak
3 Replies

8. Shell Programming and Scripting

Split File Based on Line Number Pattern

Hello all. Sorry, I know this question is similar to many others, but I just can seem to put together exactly what I need. My file is tab delimitted and contains approximately 1 million rows. I would like to send lines 1,4,& 7 to a file. Lines 2, 5, & 8 to a second file. Lines 3, 6, & 9 to... (11 Replies)
Discussion started by: shankster
11 Replies

9. UNIX for Dummies Questions & Answers

how to display line number for tail -f

Hi, Just wonder if there is any quick way to display line number when monitoring a log file with tail -f? (4 Replies)
Discussion started by: iengca
4 Replies

10. UNIX for Dummies Questions & Answers

display lines after a particular line number

I have a file that has 1k lines and i want to print all the lines after 900th line. an 2)I want to move files f1 ,f2,f3,f4 to p1,p2,p3,p4 Please give me the commands. Thanx in adv. (6 Replies)
Discussion started by: rajashekar.y
6 Replies
Login or Register to Ask a Question