Re: Help!! crazy awk unix shlle script



Hey Prasad,
As Morton said, this assignment was sweating but interesting one. The
solution consists of 4 files including the data. The 4 files are:
data, main.ksh, locate_column.awk, column_pick.awk

Put all the four in same directory. Execute teh program by typing:
.. main.ksh data HAIR AGE EYES

data:
-------
AGE:EYES:HAIR
32:BLUE:BLONDE
54:BROWN:BROWN

main.ksh
----------
let index=2

while (( index <= $# ))
do
x="echo $`echo $index`"
y=$(eval $x)
column="$column $(head -1 $1|gawk -v str=$y -f
locate_column.awk)"
# echo $y $column
(( index=$index+1 ))
done

gawk -F\: -v param="$column" -f column_pick.awk $1

unset x y index column


locate_column.awk
-------------------
BEGIN {RS=":"}

$0~str {print NR;}



column_pick.awk
------------------
BEGIN {split(param,arr," ");}
NR!=1 {
for (a in arr)
{
printf("%s ", $arr[a])
}
print "";
}



I have not coded for invalid column names. You can modify the above
code for that. Let me know if the above gives problem. I tested the
above with different column combinations and all worked great.

Prateek




On Oct 2, 11:29 pm, rdprasad.r...@xxxxxxxxx wrote:
consider a file with the following format: a header row with

colon-separated field names, and data rows with colon-separated
values. for

example:

AGE:EYES:HAIR

32:BLUE:BLONDE

54:BROWN:BROWN

write a script, named extract_columns, that prints out the values

corresponding to the fields specified by the user on the command line,
in
the

order specified by the user. suppose that the above file was named
data.txt
.

then typing:

%> extract_columns data.txt EYES

should produce

BLUE

BROWN

and typing:

%> extract_columns data.txt HAIR AGE

should produce

BLONDE 32

BROWN 54

the script should die with a warning if the user requests a field that
does

not exist in the data file. The script should run on any input file
that

conforms to the format described above.


.



Relevant Pages

  • Re: Help!! crazy awk unix shlle script
    ... colon-separated field names, and data rows with colon-separated ... write a script, named extract_columns, that prints out the values ... then typing: ...
    (comp.lang.awk)
  • Re: Help!!! crazy unix , awk scripting program
    ... colon-separated field names, and data rows with colon-separated ... write a script, named extract_columns, that prints out the values ... then typing: ...
    (comp.unix.shell)
  • Re: On topic - great software for an MT
    ... professional expander, and, believe me, you really, really, really want ... to simply typing a few characters and hitting a double space or F10. ... been completely blown away by the results, a single script that lets ...
    (sci.med.transcription)
  • lesspipe [Was: Re: OpenSSH]
    ... of work, which was to revise my /usr/local/bin/lesspipe script, ... Then 'less' can display squished files just by typing, e.g., ... It obviates the need for 'zless', usually supplied by gzip source, and ...
    (comp.unix.sco.misc)
  • Re: convert hex to decimal?
    ... >> Not when you count time for typing, debugging, etc. ... > in a script), ... Michael Heiming - RHCE ...
    (comp.unix.shell)