Help with awk script, changing the FS for a single variable

Hi all, im new to awk and would apreciate if you could tell me how to do this, i have a file with several entries like this:

2008-09-09 21:57:45   44  403 CUSTOM_EVENT                      Upgrade - end1
2008-09-09 21:57:46   45  403 CUSTOM_EVENT                      Component Check - start
2008-09-09 21:57:56   46  403 CUSTOM_EVENT                      Component Check - end
2008-09-09 21:57:56   47  403 CUSTOM_EVENT                      OSChecksum - start
2008-09-09 21:59:15   48  403 CUSTOM_EVENT                      OSChecksum - end
2008-09-09 21:59:15   49  403 CUSTOM_EVENT                      SELLogCheck - start
2008-09-09 22:01:39   50  403 CUSTOM_EVENT                      SELLogCheck - end
2008-09-09 22:01:40   51  403 CUSTOM_EVENT                      USB to Serial Connection Test - start
2008-09-09 22:43:46   52  403 CUSTOM_EVENT                      USB to Serial Connection Test - start
2008-09-09 22:44:15   53  403 CUSTOM_EVENT                      MemoryCheck - start
2008-09-09 22:44:16   54  403 CUSTOM_EVENT                      MemoryCheck - end

im trying to get the values on the last field, which would be the description of the event (eg. USB to Serial Connection Test - start) , but i need to further separate this field with a "-" to know if the test started or ended, the last field is a bit variable so i figured y could use something like this:

cat $1 | awk '{description = $6" "$7" "$8" "$9" "$10" "$11" "$12" "$13" "$14" "$15 ; print description }'

first i get all the fields from 6 to 15, and then, i tryed changing the FS to - and print the second field...

cat $1 | awk '{description = $6" "$7" "$8" "$9" "$10" "$11" "$12" "$13" "$14" "$15 ; FS="-" ; print description $2}'

of course, that prints the second field of the entire line, which is the year... hehe... is there a way to tell awk to output the variable's second field delimited by a "-"?

thanks all :slight_smile:

With AWK:

awk '{
  for (i=6; i<=NF; i++) 
    s = s ? s FS $i : $i    
  split(s, t, "-")
  print "desc:", t[1], "state:", t[2]
  s = ""  
}' infile

With Perl:

perl -lane'
  print "desc: @F[5..$#F-2] state: $F[-1]"
  ' infile

Actually, you don't need to split explicitly with AWK:

awk '{
  for (i=6; i<=NF-2; i++) 
    s = s ? s FS $i : $i    
  print "desc:", s, "state:", $NF
  s = ""  
}' infile

I added a comment to show that the two fields were separated, and added the "ed" to the action verb (just because it sounds better that way).

> cat file74
2008-09-09 21:57:45   44  403 CUSTOM_EVENT                      Upgrade - end1
2008-09-09 21:57:46   45  403 CUSTOM_EVENT                      Component Check - start
2008-09-09 21:57:56   46  403 CUSTOM_EVENT                      Component Check - end
2008-09-09 21:57:56   47  403 CUSTOM_EVENT                      OSChecksum - start
2008-09-09 21:59:15   48  403 CUSTOM_EVENT                      OSChecksum - end
2008-09-09 21:59:15   49  403 CUSTOM_EVENT                      SELLogCheck - start
2008-09-09 22:01:39   50  403 CUSTOM_EVENT                      SELLogCheck - end
2008-09-09 22:01:40   51  403 CUSTOM_EVENT                      USB to Serial Connection Test - start
2008-09-09 22:43:46   52  403 CUSTOM_EVENT                      USB to Serial Connection Test - start
2008-09-09 22:44:15   53  403 CUSTOM_EVENT                      MemoryCheck - start
2008-09-09 22:44:16   54  403 CUSTOM_EVENT                      MemoryCheck - end

> cut -c65- file74 | awk '{FS="-"}{print $1" _which was_ "$2"ed"}'
Upgrade _which was_ -ed
Component Check  _which was_  started
Component Check  _which was_  ended
OSChecksum  _which was_  started
OSChecksum  _which was_  ended
SELLogCheck  _which was_  started
SELLogCheck  _which was_  ended
USB to Serial Connection Test  _which was_  started
USB to Serial Connection Test  _which was_  started
MemoryCheck  _which was_  started
MemoryCheck  _which was_  ended