Re: Help!! crazy awk unix shlle script
- From: Prateek <prateek.a@xxxxxxxxx>
- Date: Wed, 03 Oct 2007 00:00:22 -0700
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.
.
- Follow-Ups:
- Re: Help!! crazy awk unix shlle script
- From: Robert Figura
- Re: Help!! crazy awk unix shlle script
- References:
- Help!! crazy awk unix shlle script
- From: rdprasad . ravi
- Help!! crazy awk unix shlle script
- Prev by Date: Re: Help!! crazy awk unix shlle script
- Next by Date: Re: Help!! crazy awk unix shlle script
- Previous by thread: Re: Help!! crazy awk unix shlle script
- Next by thread: Re: Help!! crazy awk unix shlle script
- Index(es):
Relevant Pages
|