LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to Awk Paragraph in complex text file? (http://www.linuxquestions.org/questions/linux-general-1/how-to-awk-paragraph-in-complex-text-file-834146/)

VMthinker 09-23-2010 10:09 PM

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:

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:

#!/bin/bash

# usage: pargrep <infile> <searchpattern>

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

awk '
BEGIN {
    FS="\n"
    RS="-----"
}
/'"$searchString"'/ { print }
' ${inFile}


GrapefruiTgirl 09-24-2010 05:41 AM

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.


All times are GMT -5. The time now is 03:46 PM.