The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
display changing variable in one place on screen in ksh raidzero Shell Programming and Scripting 7 09-25-2008 09:24 AM
rerun .profile after changing variable sboxtops AIX 2 09-02-2008 02:19 PM
bash script execution with a variable in a single line shoeb Shell Programming and Scripting 1 07-06-2008 04:14 AM
IFS changing the variable value pvar Shell Programming and Scripting 1 02-25-2005 06:21 PM
Very simple question about changing PS1 variable at startup! abidmalik UNIX for Dummies Questions & Answers 2 08-26-2002 01:05 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-18-2008
sx3v1l_1n51de sx3v1l_1n51de is offline
Registered User
  
 

Join Date: Jan 2005
Posts: 27
Talking 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:

Code:
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
  #2 (permalink)  
Old 11-18-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,794
With AWK:

Code:
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:

Code:
perl -lane'
  print "desc: @F[5..$#F-2] state: $F[-1]"
  ' infile
  #3 (permalink)  
Old 11-18-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,794
Actually, you don't need to split explicitly with AWK:

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

Last edited by radoulov; 11-18-2008 at 04:17 PM..
  #4 (permalink)  
Old 11-18-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Cool Not sure of your desired result, but

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).

Code:
> 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
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 08:10 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0