fetch last line no form file which is match with specific pattern by grep command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting fetch last line no form file which is match with specific pattern by grep command
# 1  
Old 02-01-2011
fetch last line no form file which is match with specific pattern by grep command

Hi

i have a file which have a pattern like this
Code:
Nov 10  session closed
Nov 10  Nov 9 08:14:27 EST5EDT 2010 on tty .
Nov 10  Oct 19 02:14:21 EST5EDT 2010 on pts/tk .
Nov 10  afrtetryytr
Nov 10  session closed
Nov 10  Nov 10 03:21:04 EST5EDT 2010 
Dec  8   Nov 10 05:03:02 EST5EDT 2010

i have written a code for fatch the line no
Code:
Max_LIneNo=`grep -n "Nov 10" purge_exp1.txt | cut -f1 -d: | tail -1

that will give last line no number.
For above example it gives me out put 7

but i need to match only starting 2 words only
so my output should be 6

so where i could make change to get the output 6

please Help me

thanx in advance

Last edited by Yogesh Sawant; 02-01-2011 at 06:23 AM.. Reason: added code tags
# 2  
Old 02-01-2011
Try like this,
Code:
Max_LIneNo=`grep -n "^Nov 10" purge_exp1.txt | cut -f1 -d: | tail -1`

OR
Code:
awk '/^Nov 10/{i=NR} END {print i}' purge_exp1.txt

This User Gave Thanks to pravin27 For This Post:
# 3  
Old 02-01-2011
try:
Code:
grep -wn "^Nov 10" purge_exp1.txt | cut -f1 -d: | tail -1

or:
Code:
awk '/^Nov 10/{v=NR}END{print v}'

This User Gave Thanks to yinyuemi For This Post:
# 4  
Old 02-01-2011
hay guys thanx a lot
all script run properly
@ yinyuemi - why you use -wn ? if we use -n it also working.


one more question
: which is fast awk or grep?
# 5  
Old 02-01-2011
grep -w :match the whole form word
like:
Code:
echo "1
11
111" |grep 1
1
11
111

Code:
echo "1
11
111" |grep -w 1
1

The second question:
it's hard to say which is faster, it's dependent on your files and your requirement. For your question here, awk would be faster, I guess.Smilie

Last edited by Yogesh Sawant; 02-01-2011 at 06:24 AM.. Reason: added code tags
# 6  
Old 02-01-2011
Hi guys there is problem again

Max_LIneNo=`grep -wn "^Dec 3" purge_exp1.txt | cut -f1 -d: | tail -1`

then it is not give any output
-------------------------------
Max_LIneNo=`grep -wn "^Dec 3" purge_exp1.txt | cut -f1 -d: | tail -1`

var5=`awk '/^Dec 3/{i=NR} END {print i}' purge_exp1.txt`

but when we write above two command them it will me the line no of Dec 30 instead of DEC 3.



---------- Post updated at 03:42 PM ---------- Previous update was at 03:35 PM ----------

Hi Guys
there is problem again

when i run this command
Max_LIneNo=`grep -wn "^Dec 3" purge_exp1.txt | cut -f1 -d: | tail -1`
it was not give any output to me

but when i run below two command
Max_LIneNo=`grep -n "^Dec 3" purge_exp1.txt | cut -f1 -d: | tail -1`
var5=`awk '/^Dec 3/{i=NR} END {print i}' purge_exp1.txt`

then it will give me line no of Dec 31 line instead of Dec 3
# 7  
Old 02-01-2011
Try this,
Code:
awk '$1FS$2 =="Nov 3" {i=NR} END {print i}' inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Fetch latest version form a file?

Hi, I have a file where versions will be updated, i need to get latest/last updated version from that file. Could you please help? File looks like below - <versions> <version>R20180417.006</version> <version>R20180421.007</version> <version>R20180421.008</version> ... (5 Replies)
Discussion started by: schandra128
5 Replies

2. Shell Programming and Scripting

Match all lines in file where specific text pattern is less than

In the below file I am trying to grep or similar, all lines where only AF= is less than 0.4.. Thank you :). grep grep "AF=" ,+ .4 file file 12 112036782 . T C 34.0248 PASS ... (3 Replies)
Discussion started by: cmccabe
3 Replies

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

4. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

5. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

6. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

7. Shell Programming and Scripting

Fetch entries with specific pattern

Hi all, I have following sample input file which is a part of big file: ID AINX_HUMAN Reviewed; 499 AA. AC Q16352; B1AQK0; Q9BRC5; DT 30-MAY-2000, integrated into UniProtKB/Swiss-Prot. DT 23-JAN-2002, sequence version 2. DT 28-NOV-2012, entry version 123.... (2 Replies)
Discussion started by: kareena
2 Replies

8. Shell Programming and Scripting

Help with replace line based on specific pattern match

Input file data20714 7327 7366 detail data20714 7327 7366 main data250821 56532 57634 detail data250821 57527 57634 main data250821 57359 57474 main data250821 57212 57301 main data250821 57140 57159 detail data250821 56834 57082 main data250821 56708 56779 main ... (3 Replies)
Discussion started by: perl_beginner
3 Replies

9. Shell Programming and Scripting

How to fetch a specific line from file

Hi, I have text file in the following strucher . The files contain hondreds of lines. value1;value2;value3;value4 I would like to get back the line with lowest date (values4 field). In this case its line number 3. groupa;Listener;1;20110120162018 groupb;Database;0;20110201122641... (4 Replies)
Discussion started by: yoavbe
4 Replies

10. Shell Programming and Scripting

Merge two file data together based on specific pattern match

My input: File_1: 2000_t g1110.b1 abb.1 2001_t g1111.b1 abb.2 abb.2 g1112.b1 abb.3 2002_t . . File_2: 2000_t Ali england 135 abb.1 Zoe british 150 2001_t Ali england 305 g1111.b1 Lucy russia 126 (6 Replies)
Discussion started by: patrick87
6 Replies
Login or Register to Ask a Question