awk Scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk Scripting
# 1  
Old 08-06-2013
awk Scripting

Hey guys,

I want to get all the columns in this input file tab-delimited, i need to get the column send them to a variable. From there i could print them in shuffle and pick and select columns i want.

Here is the input sample

Code:
2013/08/05 06:50:38:067 MINOR   SiteScope       SiteScope:domain.test.com:Customer B: Oracle Database Server 11G - Basic:Database Counter: Tablespace Monitoring  domain.test.com   Metric 'CATTBS1/% Free' changed status from 'good' to 'warning' Metric 'CATTBS1/% Free' crossed 'CATTBS1/% Free >= 90' with value '96.88'       DatabaseCounter CATTBS1/% Free  domain.test.com:9354ce4d-0298-44f0-92ff-26c5ccadd4db:CATTBS1/% Free:Unknown:MINOR domain.test.com:9354ce4d-0298-44f0-92ff-26c5ccadd4db:CATTBS1/% Free       CiHint:@@domain.test.com  Unknown:Unknown:96.88   SiteScope@@domain.test.comSiteScope:domain.test.com:CATTBS1/% Free  http://domain.test.com:8080/SiteScope/servlet/Main?activeid=200707457&activerighttop=dashboard&view=new&dashboard_view=Details&dashboard_model=true&sis_silent_login_type=encrypted&login=%28sisp%29knjxbqDESkAn5mKcvgTmj%2FyFwHH5Ke3m%0D%0A&password=%28sisp%29EzqXbIXEFD%2BJbE1N1T%2FZlELjja0DKaN7%0D%0A        SiteScopeMonitor:OracleDatabaseServer11GBasic.1:200707457       1
2013/08/05 06:50:38:067 MINOR   SiteScope       SiteScope:domain.test.com:Customer B: Oracle Database Server 11G - Basic:Database Counter: Tablespace Monitoring  domain.test.com   Metric 'CATTBS/% Free' changed status from 'good' to 'warning'  Metric 'CATTBS/% Free' crossed 'CATTBS/% Free >= 90' with value '96.88' DatabaseCounter CATTBS/% Free   domain.test.com:9354ce4d-0298-44f0-92ff-26c5ccadd4db:CATTBS/% Free:Unknown:MINOR  domain.test.com:9354ce4d-0298-44f0-92ff-26c5ccadd4db:CATTBS/% Free        CiHint:@@domain.test.com  Unknown:Unknown:96.88   SiteScope@@domain.test.com       SiteScope:domain.test.com:CATTBS/% Free    http://domain.test.com:8080/SiteScope/servlet/Main?activeid=200707457&activerighttop=dashboard&view=new&dashboard_view=Details&dashboard_model=true&sis_silent_login_type=encrypted&login=%28sisp%29knjxbqDESkAn5mKcvgTmj%2FyFwHH5Ke3m%0D%0A&password=%28sisp%29EzqXbIXEFD%2BJbE1N1T%2FZlELjja0DKaN7%0D%0A        SiteScopeMonitor:OracleDatabaseServer11GBasic.1:200707457       1
2013/08/05 06:50:38:067 CRITICAL        SiteScope       SiteScope:domain.test.com:Customer B: Oracle Database Server 11G - Basic:Database Counter: Tablespace Monitoring  domain.test.com   Metric 'CATTBS3/% Free' changed status from 'good' to 'error'   Metric 'CATTBS3/% Free' crossed 'CATTBS3/% Free >= 98' with value '99.61'       DatabaseCounter CATTBS3/% Free  domain.test.com:9354ce4d-0298-44f0-92ff-26c5ccadd4db:CATTBS3/% Free:Unknown:CRITICAL      domain.test.com:9354ce4d-0298-44f0-92ff-26c5ccadd4db:CATTBS3/% Free       CiHint:@@domain.test.com  Unknown:Unknown:99.61   SiteScope@@domain.test.com        SiteScope:domain.test.com:CATTBS3/% Free  http://domain.test.com:8080/SiteScope/servlet/Main?activeid=200707457&activerighttop=dashboard&view=new&dashboard_view=Details&dashboard_model=true&sis_silent_login_type=encrypted&login=%28sisp%29knjxbqDESkAn5mKcvgTmj%2FyFwHH5Ke3m%0D%0A&password=%28sisp%29EzqXbIXEFD%2BJbE1N1T%2FZlELjja0DKaN7%0D%0A        SiteScopeMonitor:OracleDatabaseServer11GBasic.1:200707457       1
2013/08/05 06:50:38:067 MINOR   SiteScope       SiteScope:domain.test.com:Customer B: Oracle Database Server 11G - Basic:Database Counter: Tablespace Monitoring  domain.test.com   Metric 'UNDOTBS1/% Free' changed status from 'good' to 'warning'        Metric 'UNDOTBS1/% Free' crossed 'UNDOTBS1/% Free >= 90' with value '96.78'     DatabaseCounter UNDOTBS1/% Free domain.test.com:9354ce4d-0298-44f0-92ff-26c5ccadd4db:UNDOTBS1/% Free:Unknown:MINOR        domain.test.com:9354ce4d-0298-44f0-92ff-26c5ccadd4db:UNDOTBS1/% Free      CiHint:@@domain.test.com  Unknown:Unknown:96.78   SiteScope@@domain.test.com        SiteScope:domain.test.com:UNDOTBS1/% Free http://domain.test.com:8080/SiteScope/servlet/Main?activeid=200707457&activerighttop=dashboard&view=new&dashboard_view=Details&dashboard_model=true&sis_silent_login_type=encrypted&login=%28sisp%29knjxbqDESkAn5mKcvgTmj%2FyFwHH5Ke3m%0D%0A&password=%28sisp%29EzqXbIXEFD%2BJbE1N1T%2FZlELjja0DKaN7%0D%0A        SiteScopeMonitor:OracleDatabaseServer11GBasic.1:200707457       1

again this is tab delimited and would output like this

Code:
[sitescope@server logs]$ awk -F"\t" '{print $1}' test.log
2013/08/05 06:50:38:067
2013/08/05 06:50:38:067
2013/08/05 06:50:38:067
2013/08/05 06:50:38:067
[sitescope@server logs]$

[sitescope@server logs]$ awk -F"\t" '{print $2}' test.log
MINOR
MINOR
CRITICAL
MINOR
[sitescope@server logs]$

[sitescope@server logs]$ awk -F"\t" '{print $4}' test.log
SiteScope:domain.test.com:Customer B: Oracle Database Server 11G - Basic:Database Counter: Tablespace Monitoring
SiteScope:domain.test.com:Customer B: Oracle Database Server 11G - Basic:Database Counter: Tablespace Monitoring
SiteScope:domain.test.com:Customer B: Oracle Database Server 11G - Basic:Database Counter: Tablespace Monitoring
SiteScope:domain.test.com:Customer B: Oracle Database Server 11G - Basic:Database Counter: Tablespace Monitoring
[sitescope@server logs]$

if you see a double space or more, it means a tab.


I can create a shell script with this setup like and print the variables.

Code:
date=`echo $line | awk -F"\t" '{print $1}`
severity=`echo $line | awk -F"\t" '{print $2}'`

But i believe this is really inefficient, i would like to make this all of this in awk good for me to expand my knowledge as well.

please point me to the right direction. thanks
# 2  
Old 08-06-2013
You can use the split function in awk, then select columns to work with like s[1] s[8]
Code:
awk '{split($0,s,"\t");print s[1]}' test.log
2013/08/05 06:50:38:067
2013/08/05 06:50:38:067
2013/08/05 06:50:38:067
2013/08/05 06:50:38:067

This User Gave Thanks to Jotne For This Post:
# 3  
Old 08-06-2013
thanks for this. would you mind sharing the awk structure as well.
i would like to have something like this written in awk.

Code:
while read line
do
	date=`echo $line | awk -F"\t" '{print $1}'`
	severity=`echo $line | awk -F"\t" '{print $2}'`
	server=`echo $line | awk -F"\t" '{print $5}'`
        echo "server:$server date:$date severity:$severity"
done < test.log

i have fairly enough experience when it comes to shell script but not in awk and i would like to see how all of this is done in awk. thanks
# 4  
Old 08-06-2013
It may be that you can do everything inside awk and do not need to use variables.
You should also change from back tics `` to parentheses $()
date=$(echo $line | awk -F"\t" '{print $1}')

If you post your complete data and what you like to get out of it, we may help you more.
# 5  
Old 08-06-2013
thanks. yes your right i might not need to assign it to any variables.
the
Code:
 date=$(echo $line | awk -F"\t" '{print $1}')

is really inefficient and what i am trying to avoid. you see if i have 50K lines it would execute awk 50K*no. of variables.

lets say i wanted to create in awk a format like this.

date being $1 severity is $2 and servername is $5, how would i accomplish this all in awk? i would prefer it stored in variables so i can easily control and print from there.
Code:
server:domain.test.com date:2013/08/05 06:50:38:067 severity:MINOR
server:domain.test.com date:2013/08/05 06:50:38:067 severity:MINOR
server:domain.test.com date:2013/08/05 06:50:38:067 severity:CRITICAL
server:domain.test.com date:2013/08/05 06:50:38:067 severity:MINOR

# 6  
Old 08-06-2013
Is the goal to convert the log to another format?
Or do you do anything with the data?

You could use this and print the filed you like.
Code:
awk '{split($0,s,"\t");print "server:"s[4],"date:"s[1],"serverity:"s[2]}' test.log

Its some hard to test, since you example data does not contain tab as your original data does.
This User Gave Thanks to Jotne For This Post:
# 7  
Old 08-06-2013
yes exactly, i need to convert the log to another format. the reason i did not paste the output here is because i dont know yet.
but what you have provided is working so i guess i can expand from there. Smilie

Last edited by ryandegreat25; 08-06-2013 at 05:27 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

New at scripting awk with variable

I am trying to write a shell script that will add a date to and awk command from the command prompt. here is the line I am having difficulties. zgrep -i playback $dir/$1-*-errors.out.gz |cut -d '|' -f 1,11,12,15 | awk -v start=${start} -v end=${end} -F '|' '{$1>=start && $1 <=end} {print $2... (7 Replies)
Discussion started by: infinity0880
7 Replies

2. Shell Programming and Scripting

Need help in awk scripting

Hi I am beginner of shell/AWK scripting , can you please help me in select particular column and column between two pattern from a multiple column file. file1.txt number status date1 date2 description category ... (7 Replies)
Discussion started by: vijay_rajni
7 Replies

3. Shell Programming and Scripting

Need help with awk scripting

hi all, I am working on awk scripting.I have created two awk files and now have a requirement of replacing the contents of first file with some contents of second file. Please find below the two files created.File1 has 3 records and File2 has 4 records. cat File1 111,0165,CB21031251,0165,... (3 Replies)
Discussion started by: csrohit
3 Replies

4. Shell Programming and Scripting

Help with AWK and Scripting!

Hi, This is the first time I am working with awk and I am not familiar with any commands in it. But I managed to do most of my work just left with one more. Needing your help! I have to extract only the matrix (written within ) from a text file. For example: 1JTJ_0006_ACGC_NPNP_A_12_15.pdb ... (17 Replies)
Discussion started by: SriJit
17 Replies

5. Shell Programming and Scripting

Need help with awk scripting.

Hi, i am newbie to this site and hope to learn but problem is s but need help urgently. Plz pm me if you are good at this. Help will be appreciated. (11 Replies)
Discussion started by: Rookie80
11 Replies

6. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

7. Shell Programming and Scripting

awk scripting

Hi I have 2 files of large size( 10 Miilions) , i want to join 2 files based on some condition . for this taking lot of time and 100 cpu .i want to iterate the based on some value (like 1 lakh) I put the 2 files in the associative arrays . if the array limit reaches the 1 lach join the with... (2 Replies)
Discussion started by: kiranmosarla
2 Replies

8. Shell Programming and Scripting

Scripting via awk

Hi, I am trying to understand what is happening here, new to scripting: I have a couple of these, but if I knew what was going on in one I can figure out the rest: awk '/rpc-100083/ { $2 = "enable -r" } $3 ~ /.NOS99dtlogin/ { $t = $2; $2 = $3; $3 = $t } { print }' /var/svc/profile/upgrade... (2 Replies)
Discussion started by: ulemsee
2 Replies

9. Shell Programming and Scripting

AWK scripting

I have a text file in which the text has been divided into paragraphs (two line breaks or tab marks a new paragraph) and I want to make a script which output would delete line breaks within the paragraph and the different paragraphs would be separated by two line breaks. So, if my input file... (14 Replies)
Discussion started by: Muki101
14 Replies

10. UNIX for Dummies Questions & Answers

Awk scripting

Hi, I'm new to unix and i am kind of familiar with the basic commands. can anyone suggest some good books especially for AWK scripting and SHELL scripting thanks, Hari (2 Replies)
Discussion started by: rharee
2 Replies
Login or Register to Ask a Question