[AWK] handeling data spread on multiple lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [AWK] handeling data spread on multiple lines
# 1  
Old 03-11-2011
[AWK] handeling data spread on multiple lines

Hello all,

first off great forum.

Now for my little problem.

Using RHEL 5.4 and awk. Been doing code since a few month. So just starting.

My problem is handeling data on multiple lines.

Code:
{
if ($1 != LASTKEY && h["canonicalName"] ~ /.*\/s_fr_[a-z]/)  {
        checkgecos( h["unixname"], h["gecos"] ) 
        h[unixname]=""
        h[gecos]=""
}
if ($2 == "gecos" || $2 == "unixname" || $2 == "canonicalName") {
        h[$2] = $3
}
LASTKEY = $1
}

This code handles great the following data:

toto1:unixname:toto1
toto1:uid:4567
toto1:gecos:Toto Toto
toto1:canonicalName:us.bla.bla.bla
bob2:unixname:bob2
bob2:uid:7676
....

But i can't figure out how to handle this kind of data spread on multiple lines (ex: sudo.log file)

Mar 9 17:24:28 : toto : TTY=pts/2 ; PWD=/home/toto ; USER=root
; COMMAND=/bin/cat /var/log/sudo.log

Of course to add more problem... It can be spread on more then 2 lines...

Jan 29 12:10:50 : toto : TTY=unknown ; PWD=/home/toto ; USER=root ;
COMMAND=/bin/nice find / /export /boot blablabla -mount
-type f -name catalina.jar -o -name license.bea -o -name weblogic.jar -o
-name httpd -o -name config.nice

Tried with a while and getline to get second line but that didn't seem to do the trick.

What are you guy's tricks for handleling multiple line data?
# 2  
Old 03-11-2011
I'd suggest changing the RS and/or FS built-in variables of awk, then you can input text with all newlines replaced, something like this:
Code:
cat file_with_multi_lines | tr '\n' ' ' | awk '
BEGIN{RS=""; FS="__FS__";} 
{
  $0=gensub(/[[:upper:]][[:lower:]][[:lower:]] [0-9][0-9]? [0-9][0-9]:[0-9][0-9]:[0-9][0-9]/, "__FS__", "g", $0);  
  for(i=2; i<=NF; i++) {
     print "NEWFIELD: " $i;}
}'

That looks kinda clumsy, I have to admit... let me explain: here I first replace all newlines with spaces (tr) , then pipe to awk, which will handle it all as one record; the big regex will capture date and replace it with __FS__; and then you have your data in the fields...
Instead of 'tr' you could hack the RS built-in, and replace the newlines within awk code...

Not quite elegant, but should work. Smilie
This User Gave Thanks to mirni For This Post:
# 3  
Old 03-14-2011
Works great!

I modified it a little bit cause some lines where not being handle correctly but now it works like a charm.

I will remember this trick!

Thanks!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove newline character from column spread over multiple lines in a file

Hi, I came across one issue recently where output from one of the columns of the table from where i am creating input file has newline characters hence, record in the file is spread over multiple lines. Fields in the file are separated by pipe (|) delimiter. As header will never have newline... (4 Replies)
Discussion started by: Prathmesh
4 Replies

2. Shell Programming and Scripting

Merging multiple lines to columns with awk, while inserting commas for missing lines

Hello all, I have a large csv file where there are four types of rows I need to merge into one row per person, where there is a column for each possible code / type of row, even if that code/row isn't there for that person. In the csv, a person may be listed from one to four times... (9 Replies)
Discussion started by: RalphNY
9 Replies

3. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

4. Shell Programming and Scripting

FTP GET with exception handeling and multiple conditions

Thanks everyone for the wonderful and helping environment.. And the problem I asked.. forget it... 4 days wait for a decent reply was such a moral booster.. (3 Replies)
Discussion started by: ReignOfChaos
3 Replies

5. UNIX for Dummies Questions & Answers

how to fill the name of the spread sheet as a cell's data

In open office spreadsheet, i would like to fill a cell with the name of the spread sheet. That is if the file name of spread sheet is, team.ods, then i have to fill a specific cell with that name 'team'. How to do that ? I have more spread sheets to be created, so i want it to be done... (0 Replies)
Discussion started by: thegeek
0 Replies

6. Shell Programming and Scripting

search for a string spread in multiple line

Hi, I have to look for the full string below in a large file(>5GB) , but that string may be spread in two lines. string :-- "f8f8 f5f0 f0f0 f1f7 f4f7 f7c1" By grep "f8f8 f5f0 f0f0 f1f7 f4f7 f7c1" file | tail -1 , I am getting incorrect result because there is a line after this also which... (1 Reply)
Discussion started by: finder255
1 Replies

7. Shell Programming and Scripting

removing pattern which is spread in multiple lines

I have several huge files wich contains oracle table creation scripts as follows: I would need to remove the pattern colored in red above. Any sed/awk/pearl code will be of much help. Thanks (2 Replies)
Discussion started by: sabyasm
2 Replies

8. Programming

spawning multiple processes spread across two files

I want to spawn n child processes but have two different classes..with the foremost one forking/spawning the child process and the latter performing a function w/ the spawned processes. I can do this in one class with an if statement and the simple if((pid=fork())==0) //child process { ... (1 Reply)
Discussion started by: StrengthThaDon
1 Replies

9. UNIX for Dummies Questions & Answers

Can I spread commands over multiple lines?

Below an example of what I mean. The first attempt does what I want; the second doesn't, because bash assumes a line break means the end of an individual "command unix". Is there some way that I can convince bash to parse out, eg, to the closing parenthesis? I'm thinking this would allow for... (1 Reply)
Discussion started by: tphyahoo
1 Replies

10. Shell Programming and Scripting

Handeling multiple options in script

i need to be able to handel if multiple commands are passed into my script. (ie) -f -r -i . does anyone know a simple solution to handeling this. Thanks (2 Replies)
Discussion started by: virus_stinger
2 Replies
Login or Register to Ask a Question