LinuxQuestions.org
Help answer threads with 0 replies.
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 06-14-2011, 04:42 AM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Rep: Reputation: 16
grep with several fields in bash


Hi,

I've a log file with 1GB of length, and I would like to filter the log by two fields (Name and City).

The logs appear in this form:
Code:
...
DEBUG: Name: XXXX
(...)
DEBUG: What city you're from: YYYY
(...)
These 2 fields appear in different lines but they are related to each other.

So, at the end of the filter I would like to have a file in one of these 2 forms:

Form1:
Code:
DEBUG: Name: XXXX
DEBUG: What city you're from: YYYY
or,

Code:
DEBUG: Name: XXXX      DEBUG: What city you're from: YYYY
How can I do that in bash?
 
Old 06-14-2011, 05:02 AM   #2
ssrameez
Member
 
Registered: Oct 2006
Location: bangalore
Distribution: Fedora, Ubuntu, Debian, Redhat
Posts: 82

Rep: Reputation: 6
egrep 'Name|city' <from the logfile>
 
Old 06-14-2011, 08:09 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
It could be done entirely in bash but would be horribly slow and relatively complex. Better use an external command as ssrameez sugested. egrep is the same as grep with the -E option. Here's using that and in a more robust way (not tested):
Code:
grep -E "DEBUG: Name: | DEBUG: What city you're from: " <name of the log file>
 
Old 06-14-2011, 10:37 AM   #4
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
Thanks,
 
  


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
[bash] Generating a table with aligned fields Aveltium Programming 2 04-09-2009 04:58 PM
bash shell script find and edit fields in a file hchoonbeng Programming 9 10-29-2008 02:13 AM
Bash: sorting by two fields humbletech99 Programming 7 09-25-2007 03:30 PM


All times are GMT -5. The time now is 04:45 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