Print selection of line based on line number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print selection of line based on line number
# 1  
Old 02-12-2010
Print selection of line based on line number

Hi Unix gurus

Basically i am searching for the pattern and getting the line numbers of the grepped pattern. I am trying to print the series of lines from 7 lines before the grepped line number to the grepped line number.

I am trying to use the following code. but it is not working.
Code:
cat filename | grep -n pattern | sed 's/:/ /g' | awk 'NR==$1-7,NR==$1'

Please help

Last edited by Scott; 02-12-2010 at 08:24 PM.. Reason: Please use code tags
# 2  
Old 02-13-2010
Quote:
I am trying to print the series of lines from 7 lines before the grepped line number to the grepped line number
Use ggrep for this, ggrep can be found under samba:

Code:
/usr/sfw/bin/ggrep "PATTERN" -A 0 -B 7 yourfile

Besides, you can print line number of 4th occurance of a pattern "mohan":

Code:
nawk '$0~/mohan/{i++} i==4{print NR;exit}' yourfile

Using these knowledge you can try to solve your issue,

Regards

Last edited by EAGL€; 02-13-2010 at 05:18 AM..
# 3  
Old 02-15-2010
Hi thanks for your reply

Both ggrep and nwak is not available in the system.

Please any other way using awk.
# 4  
Old 02-15-2010
Try
Code:
$ grep -n pattern file | grep -B 6 .

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

2. UNIX for Beginners Questions & Answers

selection particular number of line from a bunch by user defined limits.

hello i am having a file having a matrix as the following 4.1 5.5 6.55 7.2 8.2 1.002 i am having around 1 lakh rows, now i need a program in which i show give min x and min y and min z values and as well as max x max y max z, the values between these minimun and maximum values should be... (1 Reply)
Discussion started by: charan pattabhi
1 Replies

3. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

4. Shell Programming and Scripting

Print lines based on line number and specified condition

Hi, I have a file like below. 1,2,3,4,5,6,7,8,9I would like to print or copied to a file based of line count in perl If I gave a condition 1 to 3 then it should iterate over above file and print 1 to 3 and then again 1 to 3 etc. output should be 1,2,3 4,5,6 7,8,9 (10 Replies)
Discussion started by: Anjan1
10 Replies

5. Shell Programming and Scripting

[Solved] How to separate one line to mutiple line based on certain number of characters?

hi Gurus, I need separate a file which is one huge line to multiple lines based on certain number of charactors. for example: abcdefghi high abaddffdd I want to separate the line to multiple lines for every 4 charactors. the result should be abcd efgh i hi gh a badd ffdd Thanks in... (5 Replies)
Discussion started by: ken6503
5 Replies

6. Shell Programming and Scripting

Print line number

how i need help again i have file input.txt like this 48:20111008_20111122:31231|123| 78:20111008_20111122:435453|321112| where 48 and 78 is line number 20111008_20111122 is a file and 31231|123| i want get in file, i am using manual awk for get that file like this awk 'NR==48... (6 Replies)
Discussion started by: zvtral
6 Replies

7. Shell Programming and Scripting

sed script - print the line number along with the line

Hi, I want to print the line number with the pattern of the line on a same line using multi-patterns in sed. But i don't know how to do it. For example, I have a file abc def ghi I want to print 1 abc 2 def 3 ghi I know how to write it one line code, but i don't know how to put... (11 Replies)
Discussion started by: ntpntp
11 Replies

8. Shell Programming and Scripting

Print Selection of Line between two Identifiers.

I have a following containing DATA in the following format: DATA....------ --------------- -------------- DATA.....------ -------------------- ------------------ DATA....------ --------------- -------------- I want to extract the selective DATA in between identifiers and ... (4 Replies)
Discussion started by: parshant_bvcoe
4 Replies

9. Shell Programming and Scripting

print first number in each line

I have a file such as: .....12345......67890...xxx ....123456....78901...yyy ...1234567...89012...zzz ..12345678.90123...aaa Where the '.' character is a SPACE. I'm trying to print just the first number in each line. such as: 12345 123456 1234567 12345678 Both the number of... (1 Reply)
Discussion started by: dcfargo
1 Replies

10. Shell Programming and Scripting

extracting a line based on line number

i want to cut all the entries from the /etc/passwd file in which the uid is> 500 for this i was writing this ,m quiet new to all this.. scripting but on the 6th n 8th line ,, i hav to specify a line number .. to get the commnd working .. but i want to use variable i instead of that ,,... (2 Replies)
Discussion started by: narendra.pant
2 Replies
Login or Register to Ask a Question