Visit Our UNIX and Linux User Community

display 5 lines from the particular text

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting display 5 lines from the particular text
# 1  
Old 09-29-2009
MySQL display 5 lines from the particular text

Hi All,
please help me to display 5 continious lines from a particular text.

my file is as below.


I want to diplay 5 lines from the word Good.

I tried the below command

grep -A5 "Good" file1.txt

I got the below error message

grep: illegal option -- A
grep: illegal option -- 5
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] -e pattern_list...
        [-f pattern_file...] [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] [-e pattern_list...]
        -f pattern_file... [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] pattern [file...]

please help me to over come this issue. Thanks!

Last edited by Franklin52; 09-29-2009 at 07:07 AM.. Reason: Please use code tags!
# 2  
Old 09-29-2009
Something like this?

awk '/Good/{c=5;next}c && c-- > 0' file

# 3  
Old 09-29-2009
or may be sed...
sed -n '/Good/{g;N;N;N;N;N
}' filename

# 4  
Old 09-29-2009
just a little modification to Franklin code:

awk '/Good/{c=4;next} c-->0 ' file1.txt

Thanks to Franklin

# 5  
Old 09-29-2009
If you are using GNU sed,

$sed -n '/Good/,+5p' filename

Previous Thread | Next Thread
Test Your Knowledge in Computers #698
Difficulty: Medium
"Homesteading the Noosphere" (abbreviated HtN) is an essay written by Eric S. Raymond about the social workings of proprietary, corporate software development.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

Display lines between timestamp

Hi Gurus, I have a software which logs event in the log file and it has become to big to search into it. I want to display all the lines from the log files between <Jul 21, 2016 3:30:37 PM BST> to <Jul 21, 2016 3:45:37 PM BST> that is 15 min data . Please help Use code tags, thanks. (10 Replies)
Discussion started by: guddu_12
10 Replies

3. Shell Programming and Scripting

To display last 5 lines of a file

Hi Guys, I want to echo last 5 lines of a file to a mail. My script getting continuously looped and not getting the output. can anyone help? #!/bin/bash read karthick; tail -5 $karthick; echo $karthick | mail -s "genius" Thanks NK (2 Replies)
Discussion started by: Karthick N
2 Replies

4. UNIX for Dummies Questions & Answers

How to display lines by range specified?

My input file is N3*123 ABC FLATS~ REF*D9*10000000001~ N3*223 ABC FLATS~ REF*D9*10000000002~ N3*323 ABC FLATS~ REF*D9*10000000003~ N3*423 ABC FLATS~ REF*D9*10000000004~ N3*523 ABC FLATS~ REF*D9*10000000005~ N3*623 ABC FLATS~ REF*D9*10000000006~ N3*723 ABC FLATS~ REF*D9*10000000007~ N3*823... (2 Replies)
Discussion started by: nsuresh316
2 Replies

5. UNIX for Dummies Questions & Answers

Display last few lines

I have huge text files and I only want to display on the screen the last lines. with less -G file.txt i get the beginning of the file. (2 Replies)
Discussion started by: FelipeAd
2 Replies

6. UNIX for Dummies Questions & Answers

Display lines 30 to 40 of a text file using head and/or tail commands

Assume that the text file contains over 40 lines. How do you do this?!?!? (5 Replies)
Discussion started by: phunkypants
5 Replies

7. Solaris

grep and display few lines before and after

Hi is there a way in grep to display few lines before and after the pattern?? I tried options A and B and after-context and before-context. But they don't work on Solaris platform. please advise. (13 Replies)
Discussion started by: melanie_pfefer
13 Replies

8. Shell Programming and Scripting

display all lines

Dear Experts, can any one tell me how to display all lines except the last line of a file using awk. take care Regards, SHARY (3 Replies)
Discussion started by: shary
3 Replies

9. Shell Programming and Scripting

display no of empty lines

I want to display the number of empty lines in a file. I guess i should use 'grep'...but how.. 10x for those who'll help. (5 Replies)
Discussion started by: atticus
5 Replies

10. UNIX for Dummies Questions & Answers

display few lines of the file

Hi, If I want to have a look at few lines of the file, how do I, what command to use. Eg: If I have a file having length 2000 lines and I want to have a look at the content between 1400 and 1600, How do I look at it ? Also, If I want to have a look at function alone in a file, how do I go... (4 Replies)
Discussion started by: sharuvman
4 Replies

Featured Tech Videos