LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 09-24-2010, 12:15 AM   #1
VMthinker
LQ Newbie
 
Registered: Sep 2010
Posts: 2

Rep: Reputation: 0
How to Awk Paragraph in complex text file?


Hi there! I have a simple log file which is very messy and I need it to be neat. The file contains log headers but are all jumbled up together therefore I need to sort the log files according to the log headers. There are no static number of lines that means that there is no fix number of lines for the each header of the text file. And I am using AWK to sort out the headers.

The Log files goes something like this:
Code:

Code:
Car LogFile Header
<text>
<text>
<text>
Car LogFile Header
<text>
Car LogFile Header
<and so forth>
I have done up/searched a simple code but it does not seem to be working. Can someone please guide me? Thanks!

Code:

Code:
#!/bin/bash

# usage: pargrep <infile> <searchpattern>

inFile="$1"
searchString="$2"

awk '
BEGIN {
    FS="\n"
    RS="-----"
}
/'"$searchString"'/ { print }
' ${inFile}
 
Old 09-24-2010, 01:15 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,577

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Setting FS may not be necessary but can't hurt and as long as there are five dashes between each records it looks ok.
I would steer away from using the mix of quotes and just assign the bash variable to an awk one:
Code:
awk -v searchString="$searchString" ''
Are you getting errors or just the wrong output?
You may wish to give example input that would actually work with what you have as obviously the current input does not.
 
  


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
[SOLVED] How to Awk Paragraph in complex text file? VMthinker Linux - General 1 09-24-2010 05:41 AM
how to rearrange a text file with awk keenboy Programming 9 11-04-2009 07:07 AM
Pattern matching in a text file - use of AWK?? wtaicken Programming 19 02-06-2009 05:54 PM
Manipulating Text File with awk or sed kushalkoolwal Programming 2 09-10-2008 07:35 PM
Help with a script to edit text file (awk? sed?) rickh Linux - Newbie 8 04-21-2005 08:24 PM


All times are GMT -5. The time now is 12:35 PM.

Main Menu
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