striping the text in AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting striping the text in AWK
# 1  
Old 11-06-2007
striping the text in AWK

hello,
what I have:

---

lots of text bla bla bla

PATCH MANAGEMENT AND SOFTWARE UPDATES - EXTERNAL
Risk Severity: High Area: External Network Relative Rating: Medium


lots of text bla bla bla

WIRELESS AP
Risk Severity: High Area: External Network Relative Rating: Poor

lots of text bla bla bla

IT DOCUMENTATION
Risk Severity: High Area: Administrative Relative Rating: High

lots of text bla bla bla

and so on and so on...
---
what I need to do is convert all "medium", "low" or "high" to numeric values for risk severity and calculate averages:

High - 5
Medium - 3
Low - 1

For relative rating:

Inadequate - 1
Poor - 2
Medium - 3
High - 4
Excellent - 5

there are 4 areas (internal, external, physical and administrative),

so at the end it should be grouped by areas and look like:
--
PATCH MANAGEMENT AND SOFTWARE UPDATES - EXTERNAL:Risk Severity:5:Area:External Network: Relative Rating:4
WIRELESS AP:Risk Severity:5:Area:External Network: Relative Rating:2

External Network Averages:Risk Severity:5:Relative Rating:3

IT DOCUMENTATION:Risk Severity:5:Area:Administrative:Relative Rating:4
..
..
..
Administrative Area Averages:Risk Severity:3.2:Relative Rating:3.6
---

So far what I have is:

#BEGIN {

# print "Title:Risk_Severity:Risk_Severity_Value:Area:Area_Value:Relative_Rating:Relative_Rating_Value"
#}

# selecting titles with ratings
/Risk Severity/ { for (i=(NR-1); i<NR; i++) { print b[i%3] }; print} { b[NR%3]=$0 }

#
# getline

# Replace the special character for a hypen with a hypen
# gsub("\226","-")
# title = $0

if (NR % 2 == 1) then
getline
title = $0
else
getline

split(....

---

May somebody guide me on what to do next, I installed awk yesterday Smilie
# 2  
Old 11-06-2007
Try and adapt the following awk program :
Code:
BEGIN {
   Severity["high"  ]   = 5;
   Severity["medium"]   = 3;
   Severity["low"   ]   = 1;

   Rating["inadequate"] = 1;
   Rating["poor"      ] = 2;
   Rating["medium"    ] = 3;
   Rating["high"      ] = 4;
   Rating["excellent" ] = 5;
}

/^Risk Severity:.*Relative Rating:/ {

   val = tolower($3);
   if (val in Severity)
      $2 = Severity[val]

   val = tolower($NF);
   if (val in Rating)
      $NF = Rating[val];
      
   area = $5;
   Areas[area] = Areas[area] prv_line ":Risk Severity:" $2 ":Area:" $5 " Network: Realative Rating:" $NF"\n";
   Sev[area] += $2;
   Rat[area] += $NF;
   Cnt[area] += 1;
}

{ prv_line = $0 }

END {
   for (area in Areas) {
      print "--- Area:", area;
      print Areas[area];
      if (Cnt[area] == 0) 
         Cnt[area] = 1;
      printf("%s Network Averages:Risk Severity:%01.1f:Relative Rating:%01.1f\n", area, Sev[area]/Cnt[area], Rat[area]/Cnt[area]);
      print ""
   }

}

Output:
Code:
--- Area: External
PATCH MANAGEMENT AND SOFTWARE UPDATES - EXTERNAL:Risk Severity:5:Area:External Network: Realative Rating:3
WIRELESS AP:Risk Severity:5:Area:External Network: Realative Rating:2

External Network Averages:Risk Severity:5,0:Relative Rating:2,5

--- Area: Administrative
IT DOCUMENTATION:Risk Severity:5:Area:Administrative Network: Realative Rating:4

Administrative Network Averages:Risk Severity:5,0:Relative Rating:4,0

Jean-Pierre.
# 3  
Old 11-07-2007
awk

Hi Guy,
I like this one, it is really a interesting question.
Hope below can help you.

input:
Code:
A
Risk Severity: High Area: External Network Relative Rating: Medium
B
Risk Severity: High Area: External Network Relative Rating: Poor
C
Risk Severity: High Area: Administrative Relative Rating: High
D
Risk Severity: High Area: Internal Network Relative Rating: Medium
E
Risk Severity: High Area: Internal Network Relative Rating: Inadequate
F
Risk Severity: High Area: Administrative Relative Rating: Excellent

output:
Code:
C Risk Severity: 5 Area: Administrative Relative Rating: 4
F Risk Severity: 5 Area: Administrative Relative Rating: 5
Administrative Average:Risk Severity:5 ------ Relative Rating:4.5

D Risk Severity: 5 Area: Internal Network Relative Rating: 3
E Risk Severity: 5 Area: Internal Network Relative Rating: 1
Internal Average:Risk Severity:5 ------ Relative Rating:2

A Risk Severity: 5 Area: External Network Relative Rating: 3
B Risk Severity: 5 Area: External Network Relative Rating: 2
External Average:Risk Severity:5 ------ Relative Rating:2.5

code:
Code:
awk 'BEGIN{
sev["High"]=5
sev["Medium"]=3
sev["Low"]=1
rate["Inadequate"]=1
rate["Poor"]=2
rate["Medium"]=3
rate["High"]=4
rate["Excellent"]=5
}
{
if (NF>2)
{
	$3=sev[$3]
	$NF=rate[$NF]
	sum_rate[$5]=sum_rate[$5]+$NF
	sum_sev[$5]=sum_sev[$5]+$3
	num[$5]=num[$5]+1
	out[$5]=sprintf("%s\n%s %s",out[$5],temp,$0)
}
else
{
	temp=$0
}
}
END{
for(i in out)
{
	print out[i]
	print i" Average:Risk Severity:"sum_sev[i]/num[i]" ------ Relative Rating:"sum_rate[i]/num[i]
}
}' filename

# 4  
Old 11-08-2007
Thank you, guys! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - help in removing some text

Hey Guys, Earlier I asked a question under Solaris, which I got great help... thanks. Although I got the script working for what it really needed to do, I am looking for a bit of help to change the output for nicer reading. my script gets a list of zones under a global-zone and puts this... (4 Replies)
Discussion started by: dakelly
4 Replies

2. Shell Programming and Scripting

awk to skip lines find text and add text based on number

I am trying to use awk skip each line with a ## or # and check each line after for STB= and if that value in greater than or = to 0.8, then at the end of line the text "STRAND BIAS" is written in else "GOOD". So in the file of 4 entries attached. awk tried: awk NR > "##"' "#" -F"STB="... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

Text processing using awk

I dispose of two tab-delimited files (the first column is the primary key): File 1 (there are multiple rows sharing the same key, I cannot merge them) A 28,29,30,31 A 17,18,19 B 11,13,14,15 B 8,9File 2 (there is one only row beginning with a given key) A 2,8,18,30,31 B ... (3 Replies)
Discussion started by: dovah
3 Replies

4. Shell Programming and Scripting

Difference between /text/ and "text" in awk

Why does this search give different result in awk I do see a mix of this in the example around the net. What to use and why? data 1 = red 2 = green 3 = blue 4 = black awk '$3 == /blue/' data "no result" awk '$3 == "blue"' data 3 = blue awk '$3 ~ /blue/' data 3 = blue (9 Replies)
Discussion started by: Jotne
9 Replies

5. Shell Programming and Scripting

Extracting text from within a section of text using AWK

I have a command which returns the below output. How can I write a script to extract mainhost and secondhost from this output and put it into an array? I may sometimes have more hosts like thirdhost. I am redirecting this output to a variable. So I guess there should be a awk or sed command to... (7 Replies)
Discussion started by: heykiran
7 Replies

6. Shell Programming and Scripting

sed - striping out html tags

I have pasted the contents of a log file (swmbackup.wrkstn.1262071383.sales2a) below: Workstation: sales2a<BR Vault sales2a-hogwarts will be initialized.<BR <font color="red"There was a problem mounting /mnt/sales2a/desktop$ </FONT<BR <font color="red"There was a problem mounting... (4 Replies)
Discussion started by: bigtonydallas
4 Replies

7. Shell Programming and Scripting

using awk to format text

I'm new to awk and would appreciate a jump start. I've got a text doc of people with first and last names, ages, home cities, and a phrase about the individual. I want to parse the text into fields and rows separated by tabs with the field names of - Firstname, Lastname, Age, City, Dollar... (5 Replies)
Discussion started by: jkandel
5 Replies

8. Windows & DOS: Issues & Discussions

"Striping" the background of an Rxvt/Urxvt window in Cygwin

To get this: https://www.unix.com/members/silversleevesx-albums-incidental-shot-glass-picture127-termshot-rxvt-rootless.png out of Cygwin's rxvt, you have to tweak your /cygwin/etc/x11/app-defaults/rxvt file, which is here:... (0 Replies)
Discussion started by: SilversleevesX
0 Replies

9. Shell Programming and Scripting

echo is striping mah quotes

As the title says, echo is striping my quotes and I need them. echo "<?xml version="1.0" encoding="ISO-8859-1"?>" returns: <?xml version=1.0 encoding=ISO-8859-1?> Obviously, echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" returns the result I need: <?xml version="1.0"... (3 Replies)
Discussion started by: s_becker
3 Replies

10. UNIX for Dummies Questions & Answers

Striping process variable

Hello. I would like to strip out the process name from the ${0##*\} variable. So that if I have a script named "mytest.script" the variable would be populated with just "mytest" I have tried doing a substring but it doesnt seem to work. name=`expr match "${0}" : './*\(/*.\)'` Doing this... (4 Replies)
Discussion started by: new9021
4 Replies
Login or Register to Ask a Question