AWK multiple line fields sorting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK multiple line fields sorting
# 1  
Old 03-31-2010
AWK multiple line fields sorting

I have a bash script which takes a log file with each record separated by a #. The records have multiple fields but field $1 is always the date and time. When the script is run it prints the record just fine from oldest to newest. I need to have records print out from newest first.
Here is the part of the code:
Code:
LOGFILE=`if [ $MONTH = "" ] && [ $uid = "" ] && [ $ROUTER = "" ] ; then
nawk 'BEGIN {FS="\n"}{RS="#"}{print $0;}' tmp/file.log
echo "$LOGFILE"

Here is a sample of the output:
Code:
03-31-2010 00:13:51
ab1234
SUBMITTED FROM URL: tool.cgi
show mem 
#
03-31-2010 00:15:35
bc1234
SUBMITTED FROM URL: tool.cgi
show mem 
sh ip access-list PROTECT_RE
#
03-31-2010 12:51:20
de1234
SUBMITTED FROM URL: ncg.cgi
vi aaa 
sho subscribers username 
#
03-31-2010 12:51:39
fg1234
SUBMITTED FROM URL: config_ncg.cgi
vi bbb 
sho subscribers username 
#

Needs to be printed out like this:
Code:
03-31-2010 12:51:39
fg1234
SUBMITTED FROM URL: config_ncg.cgi
vi bbb 
sho subscribers username 
#
03-31-2010 12:51:20
de1234
SUBMITTED FROM URL: ncg.cgi
vi aaa 
sho subscribers username 
#
03-31-2010 00:15:35
bc1234
SUBMITTED FROM URL: tool.cgi
show mem 
sh ip access-list PROTECT_RE
#
03-31-2010 00:13:51
ab1234
SUBMITTED FROM URL: tool.cgi
show mem 
#


Last edited by radoulov; 03-31-2010 at 05:39 PM.. Reason: Added more code tags.
# 2  
Old 03-31-2010
Could you post some small representative sample input (from your original data) and the output you'd like to get given that input.
# 3  
Old 03-31-2010
The idea behind this is to display the log content via a browser. I have some filters to only print records that have a match. But mostly, I just need it to dispay from newest record first.

The orginal data file looks like:

Code:
#
03-31-2010 00:13:51
ab1234
SUBMITTED FROM URL: tool.cgi
show mem 
#
03-31-2010 00:15:35
bc1234
SUBMITTED FROM URL: tool.cgi
show mem 
sh ip access-list PROTECT_RE
#
03-31-2010 12:51:20
de1234
SUBMITTED FROM URL: ncg.cgi
vi aaa 
sho subscribers username 
#
03-31-2010 12:51:39
fg1234
SUBMITTED FROM URL: config_ncg.cgi
vi bbb 
sho subscribers username 
#

The output looks the same but without the #

Code:
03-31-2010 00:13:51
ab1234
SUBMITTED FROM URL: tool.cgi
show mem 

03-31-2010 00:15:35
bc1234
SUBMITTED FROM URL: tool.cgi
show mem 
sh ip access-list PROTECT_RE

03-31-2010 12:51:20
de1234
SUBMITTED FROM URL: ncg.cgi
vi aaa 
sho subscribers username 

03-31-2010 12:51:39
fg1234
SUBMITTED FROM URL: config_ncg.cgi
vi bbb 
sho subscribers username 


Last edited by vgersh99; 03-31-2010 at 06:29 PM.. Reason: code tags, please!
# 4  
Old 03-31-2010
Code:
nawk '{a[FNR]=$0;fnr=FNR}END{for(i=fnr;i--;) print a[i]}' RS='#' myFile

# 5  
Old 03-31-2010
Thank you vgersh99, works like a charm!
# 6  
Old 04-24-2010
One more obstacle for me to over come. I need to put an if statement in the nawk statement. I only want it to print if field2 starts with my date variable and of course in reverse order.

I tried this but it is a no go.
Code:
nawk '{a[FNR]=$0;fnr=FNR} END {for(i=fnr;i--;) && if($2 ~ /^'$date'/) print a[i]}' RS='#' myFile

# 7  
Old 04-24-2010
How about:
Code:
nawk '{a[NR]=$0} END{for(i=NR;i--;) if(a[i]~d) print a[i]}' RS='#' d="$date"  myFile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print multiple fields with awk

so its common knowledge one can print multiple fields with simple commands like this: echo 12 44 45 552 24 | awk '{print $1,$4,$3}' but suppose i want to avoid specifying the "$" symbol. is that possible? can something like this be done: echo 12 44 45 552 24 | awk '{print $(1,4,3)}' ... (9 Replies)
Discussion started by: SkySmart
9 Replies

2. Shell Programming and Scripting

Replace 0 with 1 in multiple fields with awk

Hello, I have the following input file: 1 3 3 2 3 3 4 0 4 0 5 4 5 2 2 0 5 3 4 0 6 0 3 2 I am trying to remove all zeroes in fields 2 and 4 and replace them with "1's" I tried the following, but it's not working awk -F"\t" '{ if (($2==0) || ($4==0) $2=1; $4=1; print $0 ) }' input ... (8 Replies)
Discussion started by: Rabu
8 Replies

3. Shell Programming and Scripting

awk multiple fields separators

Can you please help me with this .... Input File share "FTPTransfer" "/v31_fs01/root/FTP-Transfer" umask=022 maxusr=4294967295 netbios=NJ09FIL530 share "Test" "/v31_fs01/root/Test" umask=022 maxusr=4294967295 netbios=NJ09FIL530 share "ENR California" "/v31_fs01/root/ENR California"... (14 Replies)
Discussion started by: greycells
14 Replies

4. Shell Programming and Scripting

awk gsub multiple fields

Hi, I am trying to execute this line awk -F ";" -v OFS=";" '{gsub(/\./,",",$6); print}' FILE but for multiple fields $6 $7 $8 Do you have a suggstion? Tried: awk -F ";" -v OFS="";"" "function GSUB( F ) {gsub(/\./,\",\",$F); print} { GSUB( 6 ); GSUB( 7 ); GSUB( 8 ) } 1"... (2 Replies)
Discussion started by: nakaedu
2 Replies

5. Shell Programming and Scripting

awk (or other) script that assigns fields from a line to multiple variables

Hey all, Unfortunately I have only basic knowledge of awk and/or scripting. If I have a file with lines that can look similar to this: Name=line1 Arg1=valueA Arg2=valueB Arg3=valueC Name=line2 Arg1=valueD Name=line3 Arg1=valueE Arg3=valueF Name=line4 Arg2=valueG ... (4 Replies)
Discussion started by: Rike255
4 Replies

6. UNIX for Dummies Questions & Answers

Formatting Multiple fields on 1 line to multiple rows

I'm trying extract a number of filename fields from a log file and copy them out as separate rows in a text file so i can load them into a table. I'm able to get the filenames but the all appear on one line. I tried using the cut command with the -d (delimiter) option but cant seem to make it... (1 Reply)
Discussion started by: Sinbad-66
1 Replies

7. Shell Programming and Scripting

AWK multiple fields separators

I need to print the second field of a file, taking spaces, tab and = as field separators. ; for 16-bit app support MAPI=1 CMC=1 CMCDLLNAME32=mapi32.dll CMCDLLNAME=mapi.dll MAPIX=1 MAPIXVER=1.0.0.1 OLEMessaging=1 asf=MPEGVideo asx=MPEGVideo ivf=MPEGVideo m3u=MPEGVideo (2 Replies)
Discussion started by: PamPam
2 Replies

8. Shell Programming and Scripting

sorting(both Ascending & Descending) files based on multiple fields

Hi All, I am encountered with a problem while sorting a file based on multiple columns . I need to sort like: (field2,ascending) , (field3,ascending) ,(field8,descending) , (field7,ascending),(field13,ascending). So far i was sorting only in ascending order but here i need to use one... (1 Reply)
Discussion started by: apjneeraj
1 Replies

9. Shell Programming and Scripting

Compare multiple fields in file1 to file2 and print line and next line

Hello, I have two files that I need to compare and print out the line from file2 that has the first 6 fields matching the first 6 fields in file1. Complicating this are the following restrictions 1. file1 is only a few thousand lines at most and file2 is greater than 2 million 2. I need to... (7 Replies)
Discussion started by: gillesc_mac
7 Replies

10. UNIX for Dummies Questions & Answers

sorting fields of a line

Hi all, I have a file that looks like this... ########## 1zz2_15-43 1ouy_0-13-35 1.12619901947 2gfs_41-7 1yqj_3 0.793602121208 1bl7_11-3 1wbo_1-3-4 0.791065168287 1ywr_16-3 2ghl_22 0.956896171134 2exc_11-35 1pmq_13-15-87 0.597677672501 2bal_25-7 1ouk_17-19-21-228-58 0.668388304836... (6 Replies)
Discussion started by: Digby
6 Replies
Login or Register to Ask a Question