[K12OSN] Plea for Offlist sed/awk help...

Jim Wildman jim at rossberry.com
Thu Sep 16 00:46:57 UTC 2004

On Wed, 15 Sep 2004, Shawn Powers wrote:

> I have one big data file that I need to modify before I can "awk" it 
> into different files/folders.  I'm more comfortable with awk than with 
> sed, so here's my problem:
So just use awk to do it all....

> I need to take a chunk of data (that has been awked out of the big data 
> file) and remove all the spaces, then chop off the last 2 characters. 
> After that, I'm pretty sure I can use awk to create folders based on 
> certain fields, and copy specific data into files named by other fields. 
>   It's just that bit of "seding" that has got me...
> Here's a sample of my chunk of the specific field of data:
> "MAT 101     6 8"
> "WBL -J      7 8"
> "NCMC443     6 3"
> That reminds me, I also need to strip those darn quotes out too. :)

Most people don't know it, but awk can do some pretty amazing things.
Here is the file t.awk

	numflds = split($0,newstring)
	printf("$0 is %s\n",$0)
	for (val in newstring) {
		printf("val %s of newstring is %s\n",val,newstring[val])
	delete newstring

Here is the output of awk -f t.awk data (where data had the data snippet
from your original mail).
$0 is MAT 101     6 8
val 4 of newstring is 8
val 1 of newstring is MAT
val 2 of newstring is 101
val 3 of newstring is 6
$0 is WBL -J      7 8
val 4 of newstring is 8
val 1 of newstring is WBL
val 2 of newstring is -J
val 3 of newstring is 7
$0 is NCMC443     6 3
val 1 of newstring is NCMC443
val 2 of newstring is 6
val 3 of newstring is 3

I'm sure you can do more interesting things than just print it out.  As
you can notice, the for loop returns the array elements in pseudo random
order, which can be fixed with a call to sort.  You can deal with the
varying numbers of fields by working backwards from the last one (NF).

Jim Wildman, CISSP, RHCE       jim at rossberry.com http://www.rossberry.com
"Our political way of life is by the laws of nature, of nature's God, and
of course presupposes the existence of God, the moral ruler of the
universe, and a rule of right and wrong, of just and unjust, binding upon
man, preceding all institutions of human society and government."
John Quincy Adamns

More information about the K12OSN mailing list