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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with awk script, changing the FS for a single variable
# 1  
Old 11-18-2008
Computer 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 Smilie
# 2  
Old 11-18-2008
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  
Old 11-18-2008
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  
Old 11-18-2008
Tools 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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. UNIX for Dummies Questions & Answers

HELP - NEED Small Script ( or single awk Command)

Hi All REQUIREMENT:- ========== There is one folder named /data/ds/dpr_ukdw_sys/working/unixfile In that folders there are files are like below CODS_ACTMZ_TRANS_ALL_20150911.TXT CODS_ACTMZ_REF_CTR_ACT_MIL_20150911.TXT CODS_ACTMZ_REF_NHA_ALL_20150911.TXT ... (4 Replies)
Discussion started by: Nagarjuna4347
4 Replies

3. Shell Programming and Scripting

Changing the variable using awk?

Dear all, I have kind of used both the awk/sed command and found them really useful. But at the necessity I am having right now, I need help. Actually, I would like to do the following in file script.sh PATH535="/eos/uscms/store/user/pooja04//analysis2012/535/mc/summer12/002/tt/" ... (2 Replies)
Discussion started by: emily
2 Replies

4. UNIX for Dummies Questions & Answers

Changing different value to single value

I have a input file like this select column1,column2 from tablename where column3='1000000001'; select column1,column2 from tablename where column3='1000000002'; select column1,column2 from tablename where column3='1000000003'; select column1,column2 from tablename where column3='1000000004';... (4 Replies)
Discussion started by: nsuresh316
4 Replies

5. UNIX for Dummies Questions & Answers

awk for inserting a variable containing single and double quotes

Hi i have to insert the below line into a specific line number of another file export MBR_CNT_PRCP_TYPE_CODES_DEL="'01','02','04','05','49','55','UNK'" I have passed the above line to a variable say ins_line. I have used below command to perform the insert awk 'NR==3{print "'"${ins_line}"'"}1'... (1 Reply)
Discussion started by: sathishteradata
1 Replies

6. Homework & Coursework Questions

Unix Script - Changing Variable Question

This is a problem with basic Unix scripting. Thanks for looking! 1. The problem statement, all variables and given/known data: Make a script that will compare 2 given directories and output those filenames that are in Directory 1 and not 2 2. Relevant commands, code, scripts, algorithms:... (1 Reply)
Discussion started by: iamhungry
1 Replies

7. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

8. UNIX for Dummies Questions & Answers

single output of awk script processing multiple files

Helllo UNIX Forum :) Since I am posting on this board, yes, I am new to UNIX! I read a copy of "UNIX made easy" from 1990, which felt like a making a "computer-science time jump" backwards ;) So, basically I have some sort of understanding what the basic concept is. Problem Description:... (6 Replies)
Discussion started by: Kasimir
6 Replies

9. Shell Programming and Scripting

Changing value of a variable inside a shell script

I have a continous polling happening inside a shell script on AIX. This actually calls a PL/SQL block. Is there a way I can set up a variable or pass an interrupt to end the script gracefully. I cant read from the config once the job starts running. Ideally I should change value of a variable and... (1 Reply)
Discussion started by: kshyju
1 Replies

10. Shell Programming and Scripting

bash script execution with a variable in a single line

Let a script needs a variable to execute. For example if i run ./test.sh then it needs a variable as there is a <STDIN> in the script. I want to execute it as in command line. Let test.sh requires a variable name $number I want to execute it by >test number <enter> how is it possible? (1 Reply)
Discussion started by: shoeb
1 Replies
Login or Register to Ask a Question