LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-17-2007, 06:39 AM   #1
Volcano
Member
 
Registered: Sep 2005
Posts: 172

Rep: Reputation: 15
need a linux command


i have a big log file in a directory.

in this log file , there is line which contains keyword "XML MESSAGE".

i want to print -5 lines above and +5 lines below (including that line also) to that line.

whats the linux command for this ?
 
Old 01-17-2007, 06:43 AM   #2
avallach
Member
 
Registered: Sep 2006
Location: Silesia
Distribution: Debian GNU/Linux 4.0, ArchLinux, OpenBSD
Posts: 190
Blog Entries: 2

Rep: Reputation: 31
Code:
cat your_log_file | grep XML MESSAGE -A 5 -B 5
 
Old 01-17-2007, 06:50 AM   #3
Volcano
Member
 
Registered: Sep 2005
Posts: 172

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by avallach
Code:
cat your_log_file | grep XML MESSAGE -A 5 -B 5
i guess you meant

cat your_log_file | grep "XML MESSAGE" -A 5 -B 5 .

please note "XML MESSAGE" is a keyword , a fixed string
 
Old 01-17-2007, 06:52 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by avallach
Code:
cat your_log_file | grep XML MESSAGE -A 5 -B 5
Good the avallach solution! Just a note: you have to double quote the "XML MESSAGE" or escape the blank space:

Code:
grep XML\ MESSAGE -A5 -B5 your_log_file
 
Old 01-17-2007, 06:55 AM   #5
Volcano
Member
 
Registered: Sep 2005
Posts: 172

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix
Good the avallach solution! Just a note: you have to double quote the "XML MESSAGE" or escape the blank space:

Code:
grep XML\ MESSAGE -A5 -B5 your_log_file


not working !

i got the following error msg :

grep: can't open -A
grep: can't open 5
grep: can't open -B
grep: can't open 5



i used this ..

vi mylogfile.log | grep "Xml To Publish : " -A 5 -B 5

whats wrong here ?
 
Old 01-17-2007, 07:03 AM   #6
Volcano
Member
 
Registered: Sep 2005
Posts: 172

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Volcano
not working !

i got the following error msg :

grep: can't open -A
grep: can't open 5
grep: can't open -B
grep: can't open 5



i used this ..

vi mylogfile.log | grep "Xml To Publish : " -A 5 -B 5

whats wrong here ?
let me know whether i have done any mistake ...why its not working ?
 
Old 01-17-2007, 07:09 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
It works for me, using grep-2.5.1-52.2 on FC5.

Quote:
vi mylogfile.log | grep "Xml To Publish : " -A 5 -B 5
You cannot pipe the output of vi command, because vi is an interactive editor. Use cat instead, as in the examples above.
 
Old 01-17-2007, 07:11 AM   #8
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
You need to use cat, not vi, to display the contents of the file. This:
Code:
vi mylogfile.log | grep "Xml To Publish : " -A 5 -B 5
needs to be
Code:
cat mylogfile.log | grep "Xml To Publish : " -A 5 -B 5
 
Old 01-17-2007, 07:14 AM   #9
Volcano
Member
 
Registered: Sep 2005
Posts: 172

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by pwc101
You need to use cat, not vi, to display the contents of the file. This:
Code:
vi mylogfile.log | grep "Xml To Publish : " -A 5 -B 5
needs to be
Code:
cat mylogfile.log | grep "Xml To Publish : " -A 5 -B 5
still not working .

see below..

server_dir> cat mylogfile.log | grep "Xml To Publish : " -A 5 -B 5
grep: can't open -A
grep: can't open 5
grep: can't open -B
grep: can't open 5
 
Old 01-17-2007, 07:15 AM   #10
Volcano
Member
 
Registered: Sep 2005
Posts: 172

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Volcano
still not working .

see below..

server_dir> cat mylogfile.log | grep "Xml To Publish : " -A 5 -B 5
grep: can't open -A
grep: can't open 5
grep: can't open -B
grep: can't open 5
is this command working in your machine ?
 
Old 01-17-2007, 07:16 AM   #11
Volcano
Member
 
Registered: Sep 2005
Posts: 172

Original Poster
Rep: Reputation: 15
do you have any other alternative which could solve the same purpose ?
 
Old 01-17-2007, 07:20 AM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
It works quite well! Which version of grep do you have?
 
Old 01-17-2007, 07:23 AM   #13
Volcano
Member
 
Registered: Sep 2005
Posts: 172

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix
It works quite well! Which version of grep do you have?
whats the command to check that ?

please tell me the command ..i'll check and post it .

can you tell any other alternative which could solve my problem ?
 
Old 01-17-2007, 07:34 AM   #14
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by Volcano
whats the command to check that ?
grep -V
or
grep --version

Quote:
can you tell any other alternative which could solve my problem ?
Not a one-line command! I have to think for a while, however...
 
Old 01-17-2007, 07:48 AM   #15
Volcano
Member
 
Registered: Sep 2005
Posts: 172

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix
grep -V
or
grep --version



Not a one-line command! I have to think for a while, however...

i found

grep -V
grep: illegal option -- V
Usage: grep -hblcnsviw pattern file . . .


i believe this is a unix system ...because tail command works fine in this system.....i work via telnet only....do you think ,..its a unix box ?

if so whats the equivalent command in unix which could solve my purpose ?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Require Linux/Perl equivalent command for windows Command alix123 Programming 7 08-19-2005 03:23 AM
unix command to linux command leonidas Linux - General 1 09-10-2004 01:40 AM


All times are GMT -5. The time now is 01:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration