LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 09-23-2010, 11:09 PM   #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:
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}

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

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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 06:45 AM.
 
  


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 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 08:07 AM
Pattern matching in a text file - use of AWK?? wtaicken Programming 19 02-06-2009 06:54 PM
Manipulating Text File with awk or sed kushalkoolwal Programming 2 09-10-2008 08:35 PM
Help with a script to edit text file (awk? sed?) rickh Linux - Newbie 8 04-21-2005 09:24 PM
/etc/rc.sysinit: /bin/awk: Text file busy teeno Linux - Software 5 02-23-2005 03:19 AM


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