AWK for multiple line records RS="^" FS="#"

I have to pull multiple line records with ^ as the record separator(RS)... # should be my field separator (FS)...

Sample record is:

^-60#ORA-00060: deadlock detected while waiting for resource
ORA-00001: unique constraint (SARADM.TCKNUM_PK) violated#PROC:AVAILABLE_FOR_GETNXTTIC#02/27/2012 03:43:45#^ 

When I print $0, I get the full record properly:

ads00539:/export/home/vid>cat 123456
^-60#ORA-00060: deadlock detected while waiting for resource
ORA-00001: unique constraint (SARADM.TCKNUM_PK) violated#PROC:AVAILABLE_FOR_GETNXTTIC#02/27/2012 03:43:45#^
 
ads00539:/export/home/vid>awk 'BEGIN {RS="^"; FS="#";} { print $0}' 123456
-60#ORA-00060: deadlock detected while waiting for resource
ORA-00001: unique constraint (SARADM.TCKNUM_PK) violated#PROC:AVAILABLE_FOR_GETNXTTIC#02/27/2012 03:43:45#

if I set that # to be the FS, my $2 should be
ORA-00060: deadlock detected while waiting for resource
ORA-00001: unique constraint (SARADM.TCKNUM_PK) violated

but it pulls only the first line part as $2... and the second line part, it takes as $3

ads00539:/export/home/vid>awk 'BEGIN {RS="^"; FS="#";} { print $1}' 123456
-60
 
ads00539:/export/home/vid>awk 'BEGIN {RS="^"; FS="#";} { print $2}' 123456
ORA-00060: deadlock detected while waiting for resource
 
ads00539:/export/home/vid>awk 'BEGIN {RS="^"; FS="#";} { print $3}' 123456
ORA-00001: unique constraint (SARADM.TCKNUM_PK) violated
 
ads00539:/export/home/vid>awk 'BEGIN {RS="^"; FS="#";} { print $4}' 123456
PROC:AVAILABLE_FOR_GETNXTTIC

If you see above, $1 and $2 are seperated by #... and so is $3 and $4... but $2 and $3 are seperated by new line character (\n)

which means, it is treating # as a FS and also \n...
Can you please let me know, how can I make it to have only # to be the FS and not \n???

Thanks,
Vidhyaprakash

Hi Vidhyaprakash,

I only can say that it works as expected in my system:

$ awk --version | head -1                                                                                                                                                                                                                    
GNU Awk 4.0.0                                                                                                                                                                                                                                
$ cat 123456                                                                                                                                                                                                                                 
^-60#ORA-00060: deadlock detected while waiting for resource                                                                                                                                                                                 
ORA-00001: unique constraint (SARADM.TCKNUM_PK) violated#PROC:AVAILABLE_FOR_GETNXTTIC#02/27/2012 03:43:45#^                                                                                                                                  
$ awk 'BEGIN {RS="^"; FS="#";} { print $2}' 123456
                                                                                                                                                                                                                                             
ORA-00060: deadlock detected while waiting for resource                                                                                                                                                                                      
ORA-00001: unique constraint (SARADM.TCKNUM_PK) violated                                                                                                                                                                                     

$ awk -c 'BEGIN {RS="^"; FS="#";} { print $2}' 123456

ORA-00060: deadlock detected while waiting for resource
ORA-00001: unique constraint (SARADM.TCKNUM_PK) violated

Regards,
Birei

1 Like

may be the reason is i have the older version of awk???

Maybe an older version of awk takes RS as a regular expression and matches it with the beginning of each line.

How about trying to substitute ^ with other strange char in your input file and in your RS variable and run your script again? Only for testing.

Regards,
Birei

1 Like

Or

RS="[^]"
1 Like

That assignment gives error, because it understands ^] as "except ]". It could be RS="[\\^]"

$ LANG=en awk 'BEGIN {RS="[^]"; FS="#";} { print $2}' 123456
awk: cmd. line:1: fatal: Unmatched [ or [^: /[^]/
$ LANG=en awk 'BEGIN {RS="[\^]"; FS="#";} { print $2}' 123456
awk: cmd. line:1: warning: escape sequence `\^' treated as plain `^'
awk: cmd. line:1: fatal: Unmatched [ or [^: /[^]/
$ LANG=en awk 'BEGIN {RS="[\\^]"; FS="#";} { print $2}' 123456

ORA-00060: deadlock detected while waiting for resource
ORA-00001: unique constraint (SARADM.TCKNUM_PK) violated

Regards,
Birei

1 Like

I tried:

awk '{print $2}' FS=# RS=^ infile

and it worked on various awks.
What awk are you using?. Are you on Solaris? => use /usr/xpg4/bin/awk instead

1 Like

Scrut - AWEEEESSOMMMMEEEEE!!!!

/usr/xpg4/bin/awk simply works!!
Possibly I have an older awk version...

Thanks a lot everyone!!! :slight_smile: :smiley: :slight_smile: