Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > Column manipulation in awk
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 31st May 20:01
mark e. berres
External User
 
Posts: 1
Default Column manipulation in awk


Hi All,

I have a _m by _n array (row by column) of values separated by whitespace. I
would like
to extract randomly _p *columns* of this data, leaving the rows intact.
Doing this for rows
in the array presented no problem. However, I cannot get it to work for
entire columns. Any
suggestions?

Sincerely,

Mark

markb@ravel.zoology.wisc.edu
  Reply With Quote


  sponsored links


2 31st May 20:01
demas
External User
 
Posts: 1
Default Column manipulation in awk


Post your code.


Chuck Demas

--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
demas@theworld.com | \___/ | http://world.std.com/~cpd
  Reply With Quote


  sponsored links


3 31st May 20:01
robert katz
External User
 
Posts: 1
Default Column manipulation in awk


Without checking for sane input, here's one way, using 7 random columns
of data. Just replace _p with what you want.

awk -v _n=$(awk '{print NF; exit}' arrayfile) -v _p=7 '
BEGIN {
srand()
for (i = 1; i <= _n; i++) b[i] = i
while (--i > _n - _p) {
A[b[j = 1 + int(i * rand())]]
b[j] = b[i]
}
}
{ j = 0
for (i = 1; i <= NF; i++)
if (i in A) {
printf "%6g", $i
printf (++j < _p) ? " " : "\n"
}
}' arrayfile

--
Regards,

---Robert
  Reply With Quote
4 31st May 20:01
robert katz
External User
 
Posts: 1
Default Column manipulation in awk


Well, here's one that attempts to use awk only and does some error
checking and column alignment as well.

Usage:
randcol NUM_RANDCOL array

#!/usr/bin/awk -f
##
## @(#) randcol -- prints _p random columns from an _n column array
## @(#) Usage: randcol NUM_RANDCOL array
## @(#) Robert Katz -- Sat Nov 15 20:26:43 EST 2003 ## BEGIN {
errstream = "cat >&2"
if (ARGC != 3) {
print "Usage:\n randcol NUM_RANDCOL array" | \
errstream
exit 1
}
if ((_p = ARGV[1]) !~ /^[1-9][0-9]*$/) {
print "Number of random columns must be a positive integer" | \
errstream
exit 1
}
ARGV[1] = ""
} NR == 1 {
if (_p > (_n = NF)) {
print "Number of random columns can't be greater than " _n | \
errstream
err = 1
exit err
}
srand()
for (i = 1; i <= _n; i++) b[i] = i
while (--i > _n - _p) {
A[b[j = 1 + int(i * rand())]]
b[j] = b[i] } }
NR > 1 && NF != _n {
print "Not all rows have the same number of columns" | \
errstream
err = 1
exit err
}
{
for (i = 1; i <= _n; i++)
if (i in A) {
if (length($i) > len) len = length($i)
R[++k] = $i
}
}
END {
if (!err)
for (i = 1; i <= _p * NR; i++) {
printf "%*s", len, R[i]
printf (i % _p) ? " " : "\n"
}
}


--
Regards,

---Robert
  Reply With Quote
5 31st May 20:01
mark e. berres
External User
 
Posts: 1
Default Column manipulation in awk


Hi All,

I have a _m by _n array (row by column) of values separated by whitespace. I
would like
to extract randomly _p *columns* of this data, leaving the rows intact.
Doing this for rows
in the array presented no problem. However, I cannot get it to work for
entire columns. Any
suggestions?

Sincerely,

Mark

markb@ravel.zoology.wisc.edu
  Reply With Quote
6 31st May 20:01
demas
External User
 
Posts: 1
Default Column manipulation in awk


Post your code.


Chuck Demas

--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
demas@theworld.com | \___/ | http://world.std.com/~cpd
  Reply With Quote
7 31st May 20:01
robert katz
External User
 
Posts: 1
Default Column manipulation in awk


Without checking for sane input, here's one way, using 7 random columns
of data. Just replace _p with what you want.

awk -v _n=$(awk '{print NF; exit}' arrayfile) -v _p=7 '
BEGIN {
srand()
for (i = 1; i <= _n; i++) b[i] = i
while (--i > _n - _p) {
A[b[j = 1 + int(i * rand())]]
b[j] = b[i]
}
}
{ j = 0
for (i = 1; i <= NF; i++)
if (i in A) {
printf "%6g", $i
printf (++j < _p) ? " " : "\n"
}
}' arrayfile

--
Regards,

---Robert
  Reply With Quote
8 31st May 20:01
robert katz
External User
 
Posts: 1
Default Column manipulation in awk


Well, here's one that attempts to use awk only and does some error
checking and column alignment as well.

Usage:
randcol NUM_RANDCOL array

#!/usr/bin/awk -f
##
## @(#) randcol -- prints _p random columns from an _n column array
## @(#) Usage: randcol NUM_RANDCOL array
## @(#) Robert Katz -- Sat Nov 15 20:26:43 EST 2003 ## BEGIN {
errstream = "cat >&2"
if (ARGC != 3) {
print "Usage:\n randcol NUM_RANDCOL array" | \
errstream
exit 1
}
if ((_p = ARGV[1]) !~ /^[1-9][0-9]*$/) {
print "Number of random columns must be a positive integer" | \
errstream
exit 1
}
ARGV[1] = ""
} NR == 1 {
if (_p > (_n = NF)) {
print "Number of random columns can't be greater than " _n | \
errstream
err = 1
exit err
}
srand()
for (i = 1; i <= _n; i++) b[i] = i
while (--i > _n - _p) {
A[b[j = 1 + int(i * rand())]]
b[j] = b[i] } }
NR > 1 && NF != _n {
print "Not all rows have the same number of columns" | \
errstream
err = 1
exit err
}
{
for (i = 1; i <= _n; i++)
if (i in A) {
if (length($i) > len) len = length($i)
R[++k] = $i
}
}
END {
if (!err)
for (i = 1; i <= _p * NR; i++) {
printf "%*s", len, R[i]
printf (i % _p) ? " " : "\n"
}
}


--
Regards,

---Robert
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666