1757146 Members
2337 Online
108858 Solutions
New Discussion юеВ

Re: Scripting

 
SOLVED
Go to solution
Nobody's Hero
Valued Contributor

Scripting

I am writing a script and I've hit the wall with this idea. Before I do, I just wanted to see if I can do this, if not, I have other ideas. I have a file that looks like this:
-----------------------------------------------------------
Audit mode : Login-Success, Failure, Login-Failure
Group connections :
Group Owner Date Time Type
cweis eacnobody 25-Jul-2008 23:19 Regular
eiisgw eacnobody 25-Jul-2008 23:19 Regular
Owner : eacnobody (USER )
Inactive days : 0
Password interval : 0
Full name : Richard El
Create time : 25-Jul-2008 23:18
Update time : 23-Feb-2008 03:05
Updated by : 7972

(localhost)
Data for USER '336'
----------------------------------------------------------------------------------------------------------------------
Audit mode : Login-Success, Failure, Login-Failure
Group connections :
Group Owner Date Time Type
cweis eacnobody 25-Jul-2008 23:19 Regular
eiisgw eacnobody 25-Jul-2008 23:19 Regular
Owner : eacnobody (USER )
Inactive days : 0
Password interval : 0
Full name : Rich Oll
Create time : 25-Jul-2008 23:18
Update time : 23-Feb-2008 03:05
Updated by : u672

(localhost)
Data for USER 'u036e'
-----------------------------------------------------------

Instead of reading in the entire file, can I use the ---'s as a record seperator? so from --- to --- is one file and so on?
UNIX IS GOOD
2 REPLIES 2
Dennis Handly
Acclaimed Contributor

Re: Scripting

>Instead of reading in the entire file

You have to read the file, nothing will seek for you.

>can I use the "---"s as a record separator?

No, you must read every record and then recognize the "---" lines.
Using RS="-" in awk fails because you use "-" in your dates and text.

You can check for lines with "---" in awk to do whatever you want.

What are you really trying to do with your file?
Hein van den Heuvel
Honored Contributor
Solution

Re: Scripting

>> Instead of reading in the entire file, can I use the ---'s as a record seperator? so from --- to --- is one file and so on?

May we assume you meant 'one line', not 'one file' ?

Yes, tools like AWK and PERL are happy to accept alternative line terminators = "RECORD SEPERATORS".

Witness, for the sample data presented:

$ awk 'BEGIN { } END { print NR }' tmp.txt
33
$ awk 'BEGIN { RS="\n--" } END { print NR }' tmp.txt
3

Often there are minor glitches for the first and/or last seperator.

Personally I often prefer the 'hunt and gather' technique. Hunt for recognizable handles and gather what goes with that.
I recently posted an example in:
http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=1306454

Not knowing what problem you are really trying to solve, I'm guessing that in your case that could look like:


$ awk '/^Fu/{name=$3 " " $4 " " $5} /^Update t/{date=$4} /^Da/{ print $4, date, name }' tmp.txt
'336' 23-Feb-2008 : Richard El
'u036e' 23-Feb-2008 : Rich Oll

Or...

$ awk 'BEGIN {FS=": "} /^Fu/{n=$2} /^Update t/{d=$2} /^Da/{ print $0, d, n;n=d="" }' tmp.txt
Data for USER '336' 23-Feb-2008 03:05 Richard El
Data for USER 'u036e' 23-Feb-2008 03:05 Rich Oll

Enjoy!
Hein.