LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-24-2012, 02:17 AM   #1
Kashif_Bash
LQ Newbie
 
Registered: Apr 2012
Posts: 20

Rep: Reputation: Disabled
get two/more specific words on a line and print next few lines


currently I am searching one word at last occurrence in file for which I use following thing.

Code:

grep -A 20 -e 'address 5' input | tail -n 21 > output

This works great for last occurrence searching for one word and then print that line and next 20 lines. but If i want to search 2 or 3 words? like last occurrence of "address 5" and "usb", together in one line... for example as in following line:

Code:
Apr 20 15:58:47 box2 kernel: usb 1-3: USB disconnect, address 5
now i want to search that line which has words "address 5" and "usb" and it will be last combined occurrence of these words in this file. or if there can be any generic way, like 3 words , 4 words???

Please note: I want to get last occurrence of word "address 5" AND "usb" in file and next 20 lines. thanks in advance
 
Old 04-24-2012, 04:05 AM   #2
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware64 14.1
Posts: 396

Rep: Reputation: 153Reputation: 153
Use a regular expression in the pattern for grep. For instance:

Code:
grep -e 'USB.*address 5' /var/log/messages
That will match lines that contain any amount of characters between 'USB' and 'address 5'
 
Old 04-24-2012, 11:07 AM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,164

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Quote:
Originally Posted by Diantre View Post
Use a regular expression in the pattern for grep. For instance:

Code:
grep -e 'USB.*address 5' /var/log/messages
That will match lines that contain any amount of characters between 'USB' and 'address 5'
Nitpick: this grep finds the line or lines where "address 5" follows "USB." OP asked for a way to find lines which contain both strings. Your RegEx fits his example but his words were more general. Perhaps a more elaborate RegEx will serve the purpose. However, if OP wants to identify lines which contain 3 or 4 strings without specifying their sequential relationship the complexity is compounded.

Daniel B. Martin
 
Old 04-24-2012, 11:22 AM   #4
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 719

Rep: Reputation: 72
Hi.

I have used glark for this kind of work:
Code:
NAME
       glark - Search text files for complex regular expressions

SYNOPSIS
       glark [options] expression file ...

DESCRIPTION
       Similar to "grep", "glark" offers: Perl-compatible regular expressions,
       color highlighting of matches, context around matches, complex
       expressions ("and" and "or"), grep output emulation, and automatic
       exclusion of non-text files. Its regular expressions should be familiar
       to persons experienced in Perl, Python, or Ruby. File may also be a
       list of files in the form of a path.
( ... )
It was in the Debian repositories. The web site is http://www.incava.org/projects/glark

The code is a Ruby script, and it is fairly slow, of the 20+ utilities and languages that I compared for string searches, it was near the lower end for speed: e.g. 10 times slower than sed. The flexibility, however, is amazing.

Best wishes ... cheers, makyo

Last edited by makyo; 04-24-2012 at 11:23 AM.
 
Old 04-24-2012, 11:39 AM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,164

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Quote:
Originally Posted by Kashif_Bash View Post
... can be any generic way, like 3 words ,4 words???
This is not elegant but it works. For testing I used the Edgar Allen Poe poem "The Raven". This code seeks the last line containing "chamber" and "visitor" and "entrance" in any order, prints that line and the following 4 lines.

Code:
egrep  -A 4 "chamber" $InFile \
|egrep -A 4 "visitor"         \
|egrep -A 4 "entrance"        \
|tail -5                      \
> $OutFile3
Daniel B. Martin
 
Old 04-24-2012, 01:48 PM   #6
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware64 14.1
Posts: 396

Rep: Reputation: 153Reputation: 153
Quote:
Originally Posted by danielbmartin View Post
Nitpick: this grep finds the line or lines where "address 5" follows "USB." OP asked for a way to find lines which contain both strings. Your RegEx fits his example but his words were more general. Perhaps a more elaborate RegEx will serve the purpose. However, if OP wants to identify lines which contain 3 or 4 strings without specifying their sequential relationship the complexity is compounded.
Yes, you are right. A regular expression to find 3 or 4 words could be something like this:

Code:
grep -e 'USB\|address\|mount' /var/log/messages
That would find lines containing 'USB' or 'address' or 'mount'.

If, for instance, one wants to find lines that contain 'address' following 'USB' or 'mount' following 'EXT4', it could be written:

Code:
grep -e 'USB.*address\|EXT4.*mounted' /var/log/messages
More regular expressions can be added using the alternation character (|).
 
Old 04-24-2012, 02:01 PM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,164

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Quote:
Originally Posted by Diantre View Post
Code:
grep -e 'USB\|address\|mount' /var/log/messages
That would find lines containing 'USB' or 'address' or 'mount'.
With respect, you have shown how to use grep with OR. OP wants to use grep with AND. He said
Quote:
like last occurrence of "address 5" and "usb", together in one line
Daniel B. Martin
 
Old 04-24-2012, 02:29 PM   #8
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 719

Rep: Reputation: 72
Hi.

Here is an example of glark on chapter one of Moby Dick:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate multiple-pattern match, any order, glark.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
edges() { local _f _n _l;: ${1?"edges: need file"}; _f=$1;_l=$(wc -l $_f);
  head -${_n:=3} $_f ; pe "--- ( $_l: lines total )" ; tail -$_n $_f ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C glark specimen

FILE=${1-data1}

# Use edges if specimen not available.
pl " Sample of input data file $FILE:"
specimen $FILE
# edges $FILE

pl " Results:"
glark --after-context=1 --no-highlight \( me --and swayed --and that \) $FILE

exit 0
producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
glark version 1.8.0
specimen (local) 1.17

-----
 Sample of input data file data1:
Edges: 5:0:5 of 206 lines in file "data1"
# Moby Dick, Chapter 1 The Loomings.  Page numbers removed.
# obtained from gopher at University of Minnesota, 94.09.16.

Call me Ishmael.  Some years ago--never mind how long precisely
--having little or no money in my purse, and nothing particular
   ---
flood-gates of the wonder-world swung open, and in the wild
conceits that swayed me to my purpose, two and two there floated
into my inmost soul, endless processions of the whale, and, mid
most of them all, one grand hooded phantom, like a snow hill in
the air.

-----
 Results:
  203 : conceits that swayed me to my purpose, two and two there floated
  204 + into my inmost soul, endless processions of the whale, and, mid
Leading line identification can be omitted with an option. The same technique with tail could be used to get the last segment matched ... cheers, makyo
 
Old 04-24-2012, 02:34 PM   #9
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware64 14.1
Posts: 396

Rep: Reputation: 153Reputation: 153
Quote:
Originally Posted by Kashif_Bash View Post
Please note: I want to get last occurrence of word "address 5" AND "usb"
Quote:
Originally Posted by danielbmartin View Post
With respect, you have shown how to use grep with OR. OP wants to use grep with AND.
Thank you for being respectful. Also with respect, I believe I already mentioned how to match a line with two specific strings:

Code:
grep -e 'USB.*address 5' /var/log/messages
That would be something like an AND, "match 'USB' AND 'address 5'". I also mentioned the alternation, which is an OR as you point out:

Code:
grep -e 'USB.*address\|EXT4.*mounted' /var/log/messages
Match lines with "'USB' AND 'address' OR 'EXT4' AND 'mounted'".

But perhaps I'm completely misunderstanding the point of the OP. Would the OP be so kind as to further comment on the answers?
 
Old 04-24-2012, 06:31 PM   #10
Kashif_Bash
LQ Newbie
 
Registered: Apr 2012
Posts: 20

Original Poster
Rep: Reputation: Disabled
ok. sorry for any confusion that I'm making. Here is exact scenario:

I have following line in log file:

Quote:
Apr 24 14:32:15 box2 kernel: usb 1-3: new high speed USB device using ehci_hcd and address 3
in log file, after above line, usb detail is given that I'm trying to get.

and I have only two words with me

Quote:
"usb 1-3" and "address 3"
Now I want to go in log file. and find last occurrence of these words (together) in line and then read next 16 lines (as these lines contains usb detail like size, serial number, manufacturer etc)

@danielbmartin: egrep didn't work.
tried this:

Quote:
egrep -A 4 "usb 1-3" /var/log/messages |egrep -A 4 "address 3" |tail -8 > tempfile
got this:
Quote:
Apr 18 20:55:24 box2 kernel: usb 4-1: New USB device found, idVendor=0411, idProduct=0105
Apr 18 20:55:24 box2 kernel: usb 4-1: New USB device strings: Mfr=1, Product=2, SerialNumber=5
--
Apr 24 14:32:15 box2 kernel: usb 1-3: new high speed USB device using ehci_hcd and address 3
Apr 24 14:32:15 box2 kernel: usb 1-3: New USB device found, idVendor=0411, idProduct=0105
Apr 24 14:32:15 box2 kernel: usb 1-3: New USB device strings: Mfr=1, Product=2, SerialNumber=5
Apr 24 14:32:15 box2 kernel: usb 1-3: Product: USB-SATA Bridge
Apr 24 14:32:15 box2 kernel: usb 1-3: Manufacturer: BUFFALO

I hope I made it clear. if still not then do let me know. thanks for the efforts of all guys.
 
Old 04-24-2012, 06:56 PM   #11
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware64 14.1
Posts: 396

Rep: Reputation: 153Reputation: 153
Try this:

Code:
egrep -A 16 'usb 1-3.*address 3' logfile
That should give you all lines matching 'usb 1-3', any amount of characters and then 'address 3'. The '-A 16' parameter displays the next 16 lines after the matches.
 
1 members found this post helpful.
Old 04-26-2012, 01:15 AM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
I just added a post to your other related thread.

http://www.linuxquestions.org/questi...ic-word-940850

Questions concerning a single topic should really be kept to one thread, to keep down the amount of duplicated effort.
 
  


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
print lines form nth line to mth line which fulfill specific condition cristalp Programming 4 11-07-2011 08:39 AM
Text file manipulation: selecting specific lines/columns using awk and print CHARL0TTE Linux - Newbie 2 02-27-2010 03:40 AM
substitute few words + change all the lines starting with a specific word + put blank rahmathullakm Programming 6 01-18-2009 12:35 PM
Get all lines containing 23 specific words with AWK cgcamal Programming 3 11-05-2008 11:51 AM
copy files containing specific words in a specified line abenmao Linux - Newbie 5 08-28-2008 10:04 AM


All times are GMT -5. The time now is 03:47 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration