Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 09-23-2010, 10:09 PM   #1
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:
Car LogFile Header
Car LogFile Header
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!


# usage: pargrep <infile> <searchpattern>


awk '
/'"$searchString"'/ { print }
' ${inFile}

Last edited by VMthinker; 09-24-2010 at 12:16 AM.
Old 09-24-2010, 05:41 AM   #2
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Hi there!

Methinks we need some more information in order to help, as there seems to be a disconnect between the sample log you showed, the desired output, and the code you showed..

It isn't clear what criteria you wish to sort the headers by, and exactly what output you want from your program. In your lead-in, you write that you wish to sort the log file, but the sample code you gave does not sort anything; it merely searches for some keyword or string, and prints the matching record or field.

The "searchString" - is that part of a record, or part of a record header? So, when the string is found within a record (or in the header), do you then wish to print that entire record? Or only print the header or field itself which contains the search string?

2) The RS="-----" - where does that come from? Does it actually separate log entries inside the log file? It isn't shown in your sample log file.

What I propose, to get a clear picture of what's what here, would be for you to show us a snippet of the real log file, showing at least 2 headers+records as they exist in the log file. Show us an example then of a "searchString" you might want to look for in that log file; and finally, show us the exact output/results you would like to have produced by your program if the searchString is found..

I'm pretty sure someone can help - we just need a little clarification.

Thanks, & good luck.

EDIT - Oh, and P.S., when you say "it does not seem to be working", that doesn't tell us anything. Please be more verbose, telling us exactly how it isn't working, and/or what it does do correctly vs. what it is not doing correctly.

Last edited by GrapefruiTgirl; 09-24-2010 at 05:45 AM.


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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
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
/etc/rc.sysinit: /bin/awk: Text file busy teeno Linux - Software 5 02-23-2005 02:19 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:11 AM.

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