grep to get line numbers


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep to get line numbers
# 1  
Old 04-14-2011
grep to get line numbers

I know if i use grep -n that the output will have the lines numbered but is there a way to grep the actually line number.

so like this
Code:
grep -n "one" /usr/dict/numbers
1:one
21:twenty-one
31:thirty-one
41:forty-one
51:fifty-one
61:sixty-one
71:seventy-one
81:eighty-one
91:ninety-one

but using grep to actually work with the line numbers. So if i wanted to use grep for only even line numbers, or line numbers that are any number followed by 1.

---------- Post updated at 06:50 PM ---------- Previous update was at 06:48 PM ----------

Can i use delimiters or fields with grep/egrep?

So like when using

Code:
cut -d ":" -f1

# 2  
Old 04-14-2011
grep does one thing: match lines. It's not a language, so it can't match things conditionally.

awk is a language though, with features that let you build small scripts that do a lot.

It seems cryptic but there's a simple secret to understanding it: The code inside the single big { } block is run repeatedly, once every single line, and sets variables $0(entire line), $1 (first token), $2, ... $NR as it splits the line apart on spaces. The difference between N and $N, is that N gives you the contents of the variable N, and $N gives you the contents of token number N(if N was 4, you get token 4)

Code:
# Print with line numbers.
# It sets the entire line, $0, to the special var NR (line number),
# followed by :, followed by the rest of the original line.
# The 1 at the end is a statement telling it whether to print or not.
# 1 is always true, so always prints.
awk '{ $0=NR ":" $0; } 1' inputfile

# /one/ is only true when the input string contains one, so
# this numbers the lines but only prints lines containing 'one'.
awk '{ $0=NR ":" $0; } /one/' inputfile

# The same as above, with another condition, odd lines only
awk '{ $0=NR ":" $0; } /one/ && (NR%2)' inputfile

# Same as above except even lines only
awk '{ $0=NR ":" $0; } /one/ && !(NR%2)' inputfile

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Grep for a line containing only 5 numbers

How would you grep for a line containing only 5 numbers? Something like this. 10 2 12 1 13 (4 Replies)
Discussion started by: cokedude
4 Replies

2. Shell Programming and Scripting

Select only line numbers using grep

Hai, I want to select only line numbers into a file if some pattern matches. I have written my script like below but its not working. #!/bin/sh file='/home/testfile1' filesearch='/home/test00' while read line do search=`echo $line |cut -c 1-24` echo $search echo `grep -n ""... (3 Replies)
Discussion started by: Subbu123
3 Replies

3. UNIX for Dummies Questions & Answers

Grep lines with numbers greater than 2 digits at the end of the line

I'm trying to grep lines where the digits at the end of each line are greater than digits. Tried this but it will only allow me to specify 2 digits. Any ideas would greatly be appreciated. grep -i '\<\{3,4,5\}\>' file ---------- Post updated at 05:58 PM ---------- Previous update was at 05:41... (1 Reply)
Discussion started by: jimmyf
1 Replies

4. UNIX for Dummies Questions & Answers

Grep for a range of numbers?

I am trying to extract specific information from a large *.sam file (it's originally 28Gb). I want to extract all lines that are on chr3 somewhere in the range of 112,937,439-113,437,438. Here is a sample line from my file so you can get a feel for what each line looks like: seq.4 0 ... (8 Replies)
Discussion started by: genGirl23
8 Replies

5. Shell Programming and Scripting

Assign Line Numbers to each line of the file

Hi! I'm trying to assign line numbers to each line of the file for example consider the following.. The contents of the input file are hello how are you? I'm fine. How about you? I'm trying to get the following output.. 1 hello how are you? 2 I'm fine. 3 How about you? ... (8 Replies)
Discussion started by: abk07
8 Replies

6. Shell Programming and Scripting

grep a pattern with line numbers.

I have a txt file with more than 10000 lines. There is a unique pattern which is scattered into the file. it starts with @9 and it has 15 characters. i need to grep them and display along with line numbers. Eg: File - Test1 test message.... .... .. .. @9qwerty89 ...test message... (8 Replies)
Discussion started by: abinash
8 Replies

7. UNIX Desktop Questions & Answers

grep numbers

Hi all, I am new to unix and struggling to do the below I have few lines in a xml <title>abc:1</title> <description>abc:2</description> <language>abc:3</language> Is it possible to extract only the entire word like abc:1 abc:2 abc:3 instead of the entire line into a new file . Kindly... (3 Replies)
Discussion started by: umapearl
3 Replies

8. UNIX for Dummies Questions & Answers

grep numbers

Hello, I'm trying to grep for digits surrounded by non digits and I'm obviously misinformed. Could someone help me get this sorted out here is what I have that is not working grep -ho '\D(\{11\})\D' *.txt (5 Replies)
Discussion started by: mcgrailm
5 Replies

9. UNIX for Advanced & Expert Users

Add line numbers to end of each line

Hi i would like to add line numbers to end of each line in a file. I am able to do it in the front of each line using sed, but not able to add at the end of the file. Can anyone suggest The following code adds line number to start of each line sed = filename | sed 'N;s/\n/\t/' how can i... (5 Replies)
Discussion started by: rudoraj
5 Replies

10. Shell Programming and Scripting

grep for non numbers

Hi, I want to find out whether a string contains non numbers and + and - example : Str="0005000A" - It contains A Str="0005000+" - No problem What I have done is , echo $Str | grep I will have to list out all non numeric characters... (6 Replies)
Discussion started by: shihabvk
6 Replies
Login or Register to Ask a Question