AWK

awk
assign01

BEGIN {print "Name		" "Average"}

# Match the SCORES line, where
# Field $1 will be strored in RawScores at index 1, and so on ...
# RawScores[1] = "SCORES"
# RawScores[2] = Total number of raw scores --> according to the sample text file, it is 11
/SCORES/{
		# Store "total number of raw scores" in a variable,
		# so it will be used in the lines having the students scores
		scores = $2

		# RawScores[1] = "SCORES"
		# RawScores[2] = Total number of raw scores --> according to the sample text file "cores01", it is 11
		for (i=1; i<scores+3; i++){ # or i<=NF
			RawScores[i]=$i
		}
}

# Match every line following the SCORES line
{
	# Match the 2nd line and downward
	if (NR>=2){

		# Calculating the average score of each student
		for(i=3; i<scores+3; i++){
			StuScore[i] = $i
			avg += ((StuScore[i]/RawScores[i])/scores)*100
		}

		# Print student Last Name & First Name followed by average score of that student
		print $2 "	" $1 "	" avg

		# Before reinitialize the avg score of the current student,
		# accumulate the average score of each student in the TotalAvg variable
		TotalAvg += avg

		# reinitialize avg variable to 0,
		# so it will be used for the next line
		avg = 0
	}
}

# Print: Students Names, their average scores, and the total students average
END {print "\n"  NR-1 " students	" TotalAvg/(NR-1)}

scores

SCORES 11 10 10 100 10 10 15 100 20 10 10 100
Ritchey Tom 5 8 88 10 9 11 82 17 10 9 91
Wall Larry 9 9 90 9 0 10 91 20 10 10 100

assign02_01

BEGIN {
	{print "Desktop Calculator Initialized"}
	#i = 0
	IR = 0 # Internal Register
	MR[10] # Memory Registers
}

{
	# when matching '=' sign, print the current value of the IR
	if($0 ~ /^([ \t]*[\=][ \t]*)$/) {print "> " IR}

	# when matching 'c', clear all registers
	else if($0 ~ /^([c])$/) {
		IR = 0

		for (j=0; j<10; j++)
			MR[j] = 0

		print "> " IR
	}

	# when matching a digite followed by '+' operator, that digit will be added to IR
	# Note: I can use the same idea I used in the Memory Registers below. That's, performing all operations on the typed number in one Regular Expression
	else if($0 ~ /^([ \t]*[0-9]+[\+][ \t]*)$/)
	{
		IR+=$1
		print "> " IR
	}

	# when matching a digite followed by '-' operator, that digit will be subtracted from IR
	# Note: I can use the same idea I used in the Memory Registers below. That's, performing all operations on the typed number in one Regular Expression
	else if($0 ~ /^([ \t]*[0-9]+[\-][ \t]*)$/)
	{
		IR-=$1
		print "> " IR
	}

	# when matching a digite followed by '*' operator, that digit will multiply IR
	# Note: I can use the same idea I used in the Memory Registers below. That's, performing all operations on the typed number in one Regular Expression
	else if($0 ~ /^([ \t]*[0-9]+[\*][ \t]*)$/)
	{
		IR*=$1
		print "> " IR
	}

	# when matching a digite followed by '+' operator, that digit will devide IR
	# Note: I can use the same idea I used in the Memory Registers below. That's, performing all operations on the typed number in one Regular Expression
	else if($0 ~ /^([ \t]*[0-9]+[\/][ \t]*)$/)
	{
		IR/=$1
		print "> " IR
	}

	# when matching 'm' followed by a digit, replace IR with that Memory Register
	# Instead of writing 10 RegExps for each Memory Register, I used 'substr' to extract the designated Memory Register
	else if($0 ~ /^([ \t]*[m][0-9][ \t]*)$/)
	{
		memIndex = substr($0,2)
		IR = MR[memIndex]
		print "> " IR
	}

	# when matching 'm' followed by a digit and an operator, perform that operation on that Memory Register by IR. Result is back in that Memory Register
	# Instead of writing 10 RegExps for each Memory Register, I used 'substr' to extract the designated Memory Register
	else if($0 ~ /^([ \t]*[m][0-9][\+][ \t]*)$/)
	{
		memIndex = substr($0,2,1)
		MR[memIndex] += IR
		print "> " MR[memIndex]
	}

	# Or we can make an if-else statement in the previous fragment, and will use the substr()
	else if($0 ~ /^([ \t]*[m][0-9][\-][ \t]*)$/)
	{
		memIndex = substr($0,2,1)
		MR[memIndex] -= IR
		print "> " MR[memIndex]
	}

	# This is an extra RegExp. When mathing 'mem', it will print the Memory Registers
	else if($0 ~ /^([ \t]*(mem)[ \t]*)$/)
	{
		for (j=0; j<10; j++)
		        print "Memory_Register# " j  " = "MR[j]

	}

	# when matching 'q', quit the program
	else if($0 ~ /^([ \t]*[q][ \t]*)$/)
	{
		exit
	}

	# when matching non of the above, print "Invalid Input"
	else
	{
		print "Invlaid Input"
	}
}

assign02_02

#!/usr/bin/gawk -f
# else-if statements can be replaced by if statemens in case more than a date classification matches
# e.g. a valid C-Family Style Variable can be a valid Password

BEGIN{
	print "Enter a file name:"

	# Since NR is not updated when reading records from files, NR will be overriden here
	NR = 0;

	getline
	filename = $0

	while((getline < filename )!=0)
	{
		# If you want to see the actual data, uncomment this line:
		 print $0

		# Match Signed Integers
                if($0 ~ /(^0$|^((\+|\-)[1-9][0-9]*)$)/)
                {
                        print "Line " NR ": Found Signed Integers"
                }

		# Match Signed Floating Point Numbers
		# I assummed the following floating points in these forms are invalid: 1. .1 01.0 1.010 1.00 and so on instead 0.1 1.0 0.01 ...etc are the valid ones
                else if($0 ~ /^((0.0)|(((\+|\-)(0|[1-9][0-9]*)\.(0|[0-9]*[1-9]+))([eE](\+|\-)?(0|[1-9][0-9]*))?))$/)
                {
                        print "Line " NR ": Found Signed Floating Numbers"
                }

		# Match Strings
		# must be wrapped with either " or '
            	else if($0 ~ /(^([\"][a-zA-Z0-9\t ]+[\"])$)|(^([\'][a-zA-Z0-9\t ]+[\'])$)/)
                {
                        print "Line " NR ": Found String"
                }

		# Match C-Family Style Variables
                else if($0 ~ /^[_a-zA-Z][a-zA-Z0-9_]*$/)
                {
			print "Line " NR ": Found C-Family Style Variable"
                }

		# Match Passwords
		# minimum of 6 characters, must contain lower and upper-case character, a digit, and one special character
		else if($0 ~ /^((.*[a-z].*))$/ && ($0 ~ /^(.*[A-Z].*)$/) && ($0 ~ /^(.*[0-9].*)$/) && ($0 ~ /^(.*[^0-9a-zA-Z \t].*)$/) && ($0 ~ /^(......+)$/))

		# OR we can use the 'match' function instead:
		# else if (match($0, /^((.*[a-z].*))$/) && match($0, $0 ~ /^(.*[A-Z].*)$/) && match ($0, $0 ~ /^(.*[0-9].*)$/) && match($0, /^(.*[^0-9a-zA-Z \t].*)$/) && match($0, $0 ~ /^(......+)$/))
		{
			print "Line " NR ": Found Password"
		}
		# Date
		# either YYYY/MM/DD or MM/DD/YYYY and make sure the day is no more than 31 and month is no more than 12
                else if($0 ~ /^(([0-9][0-9][0-9][0-9][\/\-](([0][1-9])|([1][0-2]))[\/\-](([0-2][1-9])|([3][0-1])))|((([0-2][1-9])|([3][0-1]))[\/\-](([0][1-9])|([1][0-2]))[\/\-]([0-9][0-9][0-9][0-9])))$/)
                {
			print "Line " NR ": Found Date"
                }

		# Match Emails
		else if($0 ~ /^([a-zA-Z][a-zA-Z0-9]*[\.]?[a-zA-Z0-9]*[\@][a-z]+[\.][a-z]+)$/)
		{
			print "Line " NR ": Found Email"
		}

		else
		{
			print "Line " NR ": Invalid Data"
		}

		NR++;
	}
	exit
}

assign02_03

#!/usr/bin/gawk -f
# This script will extract any URL from any file.

{
	# whenever "match" is used RSTART and RLENGTH values are reflected
	while(match($0, /(http[s]?)(\:\/\/)[a-zA-Z]+[\/]?[0-9a-zA-Z\/\.\_]*/)){
		# Print the first match
		print "Found URL: " substr($0, RSTART, RLENGTH)

		# Reassign the starting index value to be after the last match
		$0 = substr($0, RSTART+RLENGTH)
	}
}

major exam question

BEGIN{
	FS = "(:|,|&)"
	OFS = " "
}

/^name/ {
	if($4 == "salary"){
		print $3, $2",", $7",", $5 "\n"
	}
	else{
		print $3, $2",", $5",", $7 "\n"
	}
}

major exam qv2


BEGIN{
	FS = ","
	ORS = ""
}
/^name/{
	{split($0, Record, /\&/)}
	{split(Record[1], R1, /(\:|,)/)}
	{split(Record[2], R2, /\:/)}
	{split(Record[3], R3, /\:/)}

	{print R1[3], R1[2]","}
	if(R2[1] == "salary"){
		{print R2[2]","R3[2] "\n"}
	}
	else{
		{print R3[2]","R2[2] "\n"}
	}

}

test

/forget/ { ignore = 1 }
/see/ { ignore = 0 }

#/1/ {if (!ignore)  print "found 1"}
#/2/ {if (!ignore)  print "found 2"}

/[0-9][0-9][0-9][0-9][\-/][0-9][0-9][\-/]|[0-9][0-9][\-/][0-9][0-9][\-/][0-9][0-9][0-9][0-9]/ {print "found"}

BEGIN{
	ignore = 0
}

#// {print  a}
/exit/ {exit(0)}

trest

BEGIN{
	FS = "(:|\t| )+"
	OFS = ","
	ORS = "\n----------------------------------------\n"
}
{print $1, $2, $3}

#/[0-9]*/ {print match($0,/[0-9]+/)}
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: