Finding the line with the exact same number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding the line with the exact same number
# 1  
Old 07-28-2010
Finding the line with the exact same number

Hello All,

What i am doing is , i tail a file from certain chatacter and then cat -n to get the line numbers.I search for a particular string and gets it line number. What i am interested in is the next line immediately after the pattern i search.

But grep gives me result for all line statring with those digits.
For Ex:

I got the pattern in line as:

10 Pattern i searched.

Now, I need contents of line 11(i store in variable say $next_line).

so i do :
Code:
tail -c [number]  | grep "^[[:space:]]* $next_line

But it gives me result for the lines like 11, 111, 110 and so on .

Is there a way to only search for the exact number in grep.

Moderator's Comments:
Mod Comment Use code tags please, ty.

Last edited by kailash19; 07-28-2010 at 09:41 AM..
# 2  
Old 07-28-2010
If I understood correct, you do not really need the line numbers but used them to identify the next line.

Maybe these examples works for you:
Code:
$> cat infile
eins
zwei
drei
vier
fuenf
$> sed -n '/drei/ {n;p;q}' infile
vier
$> awk '/drei/ {getline; print}' infile
vier

# 3  
Old 07-28-2010
Thanks for reply.
Yes, you are right, i just track numbers to get the contents, but the thing is there are several lines in file with the same contents i grep. So i process them one by one . Also its on different boxes, i do ssh and use this. By line number i keep track which i processed and which didn't.
Will be good if i get something in grep itself or anything which allows only to search the exact number.
# 4  
Old 07-28-2010
Ok, not sure how you correlate the stuff you get via ssh, but basically processing a file step by step should be no problem. Here is an example with awk, where the pattern is more than once in the file (if I undestood correct):

Code:
$> cat infile
1       one
2       two
3       three
4       one
5       two
6       four
7       five
8       one
9       two
10      one
$> awk '/two/ {getline; print}' infile
3       three
6       four
10      one

I added numbers so you can see that it is working on all three patterns it found which is in this case "three" Smilie

awk processes the whole file so you could put something it should do with the found patterns (ie. the following line) into the curled brackets.

Last edited by zaxxon; 07-28-2010 at 11:10 AM.. Reason: changed the values in the example to show more variance
# 5  
Old 07-28-2010
Thanks a lot , it works Smilie.

Can you please explain the command ( not much familiar with awk).
# 6  
Old 07-28-2010
Code:
/two/

match this pattern somewhere in a line; if true, execute the commands in the curled braces

Code:
getline

get the next line in the file

Code:
print

and print it

That's all.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

finding number in exact column

Dear all, I want to find a number in exact column but I don't know how to do it. Here is the thing, data is shown below, and I want to find 416 in the first column and print it out, how should I deal with it? Thank you very much! ab33 50S01S 958 279.068999 67.251013 -150.172544 67.250000... (5 Replies)
Discussion started by: handsonzhao
5 Replies

2. Shell Programming and Scripting

help for fast way of finding line number for a regex

Hello, I am trying to find out the line numbers where regex match and put them into a file with below command: awk '/'$pat'/ {print NR}' $fileName >> temp.txt where $pat is the regex but this command is taking a lot of time to execute with bigger files for size more than 5000000... (8 Replies)
Discussion started by: JoeColeEPL9
8 Replies

3. Shell Programming and Scripting

finding the line number of a particular line in a file

Hi Frnds, I need to find the line number of a particular line in a file and store that line number to a variable. if a file named myfile contains following look at the sun look at the moon look at the star look at the ocean i need to get the line number of the line 'look at the... (3 Replies)
Discussion started by: mvignesh
3 Replies

4. Shell Programming and Scripting

Finding line with highest number in a file

Hi All, My file looks some thing like this, File 1: - A 10 B 30 C 5 D 25 E 72 F 23 now my requirement is to find the line with highest number in it, i;e the result should be E 72 Thanks in Advance (1 Reply)
Discussion started by: balu_puttaganti
1 Replies

5. Shell Programming and Scripting

finding the number of occurence of a word in a line

suppose i have this line abs|der|gt|dftnrk|dtre i want to count the number of "|" in this line.. how can i do that. plz help:confused: (9 Replies)
Discussion started by: priyanka3006
9 Replies

6. Shell Programming and Scripting

finding the line number from a grep ?

Hi there does anybody know how i can get the line number from an entry or entries in a file ?? for example if i had a file test1 test2 test3 test1 and i needed to get the line numbers for all instances of test1 in that file with the answer being (1,4) Would anybody be able... (7 Replies)
Discussion started by: hcclnoodles
7 Replies

7. Shell Programming and Scripting

finding line number if the line contains

hi i have a file , that contains data like 34343538 3136414D 45583030 30302E54 445816AMEX0000.T 524E2020 20202020 20202020 20202020 RN 20202020 20203030 38303030 30303030 0080000000 30303030 30300D 000000. 20080724-051254.668419 D 473... (1 Reply)
Discussion started by: Satyak
1 Replies

8. Shell Programming and Scripting

awk or sed for finding closest pattern to a line number

hi guys, I want to do pattern matching with awk or sed but I don't know how. here's what I want: I have a line number for a pattern that I have already found using grep, and I know a pattern like "---" that happens a few lines above that certain line number. I want to print out the chunk... (1 Reply)
Discussion started by: alirezan
1 Replies

9. Shell Programming and Scripting

Finding the line number of matching braces

Hi,I am new to shell scripting and i want to find the line numbers of matching braces. The file contents are as follows File XXX.dat 1 ( CLASS "FRUIT" 2 (TYPE "PERSISTENT") 3 (MESSAGE_TYPE "M") 4 (GET_REQRD "Y") 5 (SET_REQRD "Y") 6 ) 7 ( CLASS... (3 Replies)
Discussion started by: Rajendra_1510
3 Replies

10. UNIX for Dummies Questions & Answers

finding exact characters

I want to grep from a file an exact character match. I tried grep -c "$a $b" $file where a=6 and b=2 the problem is that I get: 6 2 and 6 20 I just need a count of the occurrence. I'm using the Bourne shell. I've also tried grep -c '$a $b' $file; not sure how to do this - any suggestions? (3 Replies)
Discussion started by: jrdnoland1
3 Replies
Login or Register to Ask a Question