LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to Awk Paragraph in complex text file? (http://www.linuxquestions.org/questions/linux-newbie-8/how-to-awk-paragraph-in-complex-text-file-834155/)

VMthinker 09-24-2010 12:15 AM

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}


grail 09-24-2010 01:15 AM

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.


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