Using grep to extract line number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using grep to extract line number
# 1  
Old 03-19-2005
Using grep to extract line number

I'm trying to use grep to get the line number only. This is the command I'm using:
grep -n "Content-Disposition: attachment" mbox

The output I get is:
45:Content-Disposition: attachment; filename="test.txt"

So now I just want to get the line number (45) from this output.

Can someone help me with this? Thanks
# 2  
Old 03-19-2005
Quote:
Originally Posted by mskarica
I'm trying to use grep to get the line number only. This is the command I'm using:
grep -n "Content-Disposition: attachment" mbox

The output I get is:
45:Content-Disposition: attachment; filename="test.txt"

So now I just want to get the line number (45) from this output.

Can someone help me with this? Thanks
Code:
grep -n "Content-Disposition: attachment" mbox | sed -n 's/^\([0-9]*\)[:].*/\1/p'

# 3  
Old 03-19-2005
Thanks. That worked exactly how I needed it to. If you don't mind, can you explain what this means for me so I know in the future:

's/^\([0-9]*\)[:].*/ \ 1/p'

Thanks a lot.
# 4  
Old 03-19-2005
Quote:
Originally Posted by mskarica
Thanks. That worked exactly how I needed it to. If you don't mind, can you explain what this means for me so I know in the future:

's/^\([0-9]*\)[:].*/ \ 1/p'

Thanks a lot.
You'll need to read up on regular expressions to really make much use of it, but here it is anyway:

The -n means not to print anything unless it's explicitly requested.

s - substitute
/ - beginning of patter to match
^ - The null character at the start of the line
\(....\) - store this in the pattern buffer
[0-9]* - match any number of occurrences numbers in the range 0-9
[:] - match the ":" character
.* - match any number of any characters (the rest of the line)
/ - end on the match patter and beginning on the replace pattern
\1 - the first entry in the pattern buffer ( what was stored with \(...\) )
/ - end of the replace pattern
p - print
This User Gave Thanks to reborg For This Post:
# 5  
Old 03-19-2005
you can also try this ...

Code:
grep -n "Content-Disposition: attachment" mbox | awk -F: '{print $1}'

# 6  
Old 03-19-2005
Or even more concisely....
Code:
sed -n '/Content-Disposition: attachment/=' file_name

Cheers
ZB
# 7  
Old 03-19-2005
I tried all 3, and all 3 worked perfectly. The last 2 options are a whole lot easier for me to understand, even though I did read up on regular expressions, and was able to follow some of the first option.

Thanks for your help Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Retrieving line number from grep

Hi. im trying to retrieve the line number from grep. i have 1 part of my code here. grep -n $tgt file.txt | cut -f 1 -d ":" when i do not cut the value i have is 12:aaa:abc:aaa:aaa:aaa how can i store the value of 12 or my whole line of string into a variable with grep? (6 Replies)
Discussion started by: One_2_three
6 Replies

2. Shell Programming and Scripting

extract a line from a file by line number

Hi guys, does anyone know how to extract(grep) a line from the file, if I know the line number? Thanks a lot. (9 Replies)
Discussion started by: aoussenko
9 Replies

3. Shell Programming and Scripting

Extract string from multiple file based on line count number

Hi, I search all forum, but I can not find solutions of my problem :( I have multiple files (5000 files), inside there is this data : FILE 1: 1195.921 -898.995 0.750312E-02-0.497526E-02 0.195382E-05 0.609417E-05 -2021.287 1305.479-0.819754E-02 0.107572E-01 0.313018E-05 0.885066E-05 ... (15 Replies)
Discussion started by: guns
15 Replies

4. Shell Programming and Scripting

extract the lines between specific line number from a text file

Hi I want to extract certain text between two line numbers like 23234234324 and 54446655567567 How do I do this with a simple sed or awk command? Thank you. ---------- Post updated at 06:16 PM ---------- Previous update was at 05:55 PM ---------- found it: sed -n '#1,#2p'... (1 Reply)
Discussion started by: return_user
1 Replies

5. UNIX for Dummies Questions & Answers

Using GREP/AWK to extract a number and store it as a variable

Hello, I have a question regarding the awk command. Here is the line I need to grep: 1 F= -.13250138E+03 E0= -.13249556E+03 d E =-.174650E-01 mag= 35.2157 Instead of displaying the number in red I would like to store it as a variable such as X. Is there a way to do this? Thanks for any... (3 Replies)
Discussion started by: modey3
3 Replies

6. Shell Programming and Scripting

extract a line from a file using the line number

Hello, I am having trouble extracting a specific line from a file when the line number is known. My first attempt involved grep -n 'hi' (the word 'hi will always be there) to get the line number before the line that I actually want (line 4). Extra Notes: -I am working in a bash script. -The... (7 Replies)
Discussion started by: grandtheftander
7 Replies

7. Shell Programming and Scripting

Extract a line from a file using the line number

I have a shell script and want to assign a value to a variable. The value is the line exctrated from a file using the line number. The line number it is not fix, and could change any time. I have tried sed, awk, head .. See my script # Get randome line number from the file #selectedline = `awk... (1 Reply)
Discussion started by: zambo
1 Replies

8. Shell Programming and Scripting

Grep a number from a line in ksh

In file.name, I have a line that reads $IDIR/imgen -usemonths -dropcheck -monitor -sizelimit 80000000 -interval 120 -volcal HSI How can I get the size limit, i.e. 80000000 out and pass it to a variable called SIZE? Thanks. I tried echo "grep sizelimit file.name" | sed -n -e... (3 Replies)
Discussion started by: rodluo
3 Replies

9. UNIX for Dummies Questions & Answers

Get Filename and Line Number using grep

Hi, I am using the Korne shell to try and get the filename, line number and the line of text using grep e.g. find ./ -type f -name "*.java" -exec grep -nf test.txt '{}' \; (test.txt contains strings to search) will return the line number and the line of text. grep -l would return the... (4 Replies)
Discussion started by: ceemh3
4 Replies

10. UNIX for Dummies Questions & Answers

is there any why to get the number of line in grep result ?

Hello all when I do simple grep on file im getting the results of "filename : stringResult " is there any way to present also the line number in the file ? (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question