Grepping text by providing line numbers.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grepping text by providing line numbers.
# 1  
Old 06-22-2009
Grepping text by providing line numbers.

Dear Friends,
I have a flat file from which I want to grep line no. 7,10, 19 to 35, 37.
How can it be done?

Thank you in advance
Anushree
# 2  
Old 06-22-2009
Hi.

When you say "grep lines", do you mean extract lines?

There are a number of tools that can do this.

But based on what you asked, this should work...

sed "7p;10p;19,35p;37p;d" my_file | grep "....."
# 3  
Old 06-22-2009
Code:
awk 'NR ~ /^(7|10|19)$/' file

# 4  
Old 06-22-2009
Use gawk, nawk or /usr/xpg4/bin/awk on Solaris:

Code:
awk 'NR~/^7|1[09]|2[0-9]|3[0-5,7]$/' infile


Last edited by radoulov; 06-22-2009 at 12:04 PM..
# 5  
Old 06-22-2009
One way to do it in perl:

Code:
perl -ne 'BEGIN{foreach $i (7,10,(19..35),37){$x{$i}=1}}{print if $x{$.}==1}' infile

tyler_durden
# 6  
Old 06-22-2009
Quote:
Originally Posted by durden_tyler
One way to do it in perl:
[...]
Code:
(19..35)...

I have to read more carefully,
I corrected my post.

Using the same logic with Perl:

Code:
perl -ne'$.=~/^7|1[09]|2[0-9]|3[0-5,7]$/and print' infile


Last edited by radoulov; 06-22-2009 at 12:09 PM..
# 7  
Old 07-01-2009
Hey friends
Almost all solutions worked fine
Thank you for the quick reply.
Take care.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting values based on line-column numbers from multiple text files

Dear All, I have to solve the following problems with multiple tab-separated text file but I don't know how. Any help would be greatly appreciated. I have access to Linux mint (but not as a professional). I have multiple tab-delimited files with the following structure: file1: 1 44 2 ... (5 Replies)
Discussion started by: Bastami
5 Replies

2. Shell Programming and Scripting

How to add line numbers (multiples of 5: 0,5,10,15,20) to a text file?

Hi, I need to number the lines in my text file. I know how to do this with standard numbering (1,2,3,4, etc) but I need to count in multiples of 5, beginning 0,5,10,15... example existing file: abcd efg hijklm nopqrs desired output 0 abcd 5 efg 10 hijklm 15 ... (11 Replies)
Discussion started by: livbaddeley
11 Replies

3. Shell Programming and Scripting

How Select numbers from a line of text, and remove leading spaces?

I have a text file with a line of text that contains numbers and text formatted into groups. I need to extract the number that can be either 1,2 or 3 digits long. Then write it to a variable, but i need to remove any leading spaces in the number first. I can get the numbers out but how to remove... (12 Replies)
Discussion started by: kcpoole
12 Replies

4. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

5. Shell Programming and Scripting

Help with grepping data from a text file

Hello, I have a text file which contains a list of strings which I want to grep from another file where these strings occur and print out only these lines. I had earlier used the grep command where File1 was the file containing the strings to be grepped (Source File) and File2 the Target File... (4 Replies)
Discussion started by: gimley
4 Replies

6. UNIX for Dummies Questions & Answers

Print numbers and associated text belonging to an interval of numbers

##### (0 Replies)
Discussion started by: lucasvs
0 Replies

7. Shell Programming and Scripting

how to add line numbers in text file

Hi all How to add line numbers in text file.. ex abcd cdef result 1. abcd 2. cdef thx in advance (4 Replies)
Discussion started by: suryanarayana
4 Replies

8. Shell Programming and Scripting

find 2 line numbers, grab text in between

hi, i have a large text file that I just want to extract the important information from. It will be a random number of lines but between two specific line numbers/markers. I was thinking I could get the line number for the first marker: Tablespace Percent Total Free Then get the line... (11 Replies)
Discussion started by: Da_Duck
11 Replies

9. Shell Programming and Scripting

how to print out line numbers of a text file?

i have this text file name test.txt which contain : aaaaa bbb iiiiiiiiiiiii ccf ddaaa ddd and i need a script that can print out the line numbers using a while loop.. so when the script is run..it will have this: 1 2 3 any ideas? :) thanks guys (4 Replies)
Discussion started by: forevercalz
4 Replies

10. UNIX for Dummies Questions & Answers

grepping for numbers in between

Ok here's the situation I have a log like the following: (S0) Time: 124937.326033 - SendingTime: '20041108-12:49:29' (S0) Time: 124937.389946 - SendingTime: '20041108-12:49:29' (S0) Time: 124937.462494 - SendingTime: '20041108-12:49:29' (S0) Time: 124937.551056 - ... (1 Reply)
Discussion started by: fusion99
1 Replies
Login or Register to Ask a Question