trouble with gawk and columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting trouble with gawk and columns
# 1  
Old 12-08-2010
trouble with gawk and columns

I am trying to make a script that would take the highest value of each columns for example

Code:
cat file
0     0     0     0
0     8     9     1
3     8     3     9
7     7     7     7

the results would be
Code:
7     8     9     9

I found this code, and it takes the highest value with a single column. I am going for all the columns

Code:
BEGIN { max=0
	print "The most expensive car is"
      }

$5>max { max=$5
	 result=$0
       }
END{print result}

I tried putting in a for command to try and take the max of all the fields but I get a syntax error for the $count>max {max=$count line.

Code:
{       for (count = 1; count <= nfields; count++)   
                        {
                $count>max { max=$count
                        }
                print max
                }

Can I get some help with this script or some insight to make this work thank youuu.Smilie
# 2  
Old 12-08-2010
Code:
awk '{for (i=1;i<=NF;i++) {a[i]=(a[i]>$i)?a[i]:$i;s=(s>NF)?s:NF}}
END {for (i=1;i<=s;i++)printf a[i] "\t"}' infile

# 3  
Old 12-08-2010
Code:
while(<DATA>){
  my @tmp=split;
  for(my $i=0;$i<=$#tmp;$i++){
    $hash{$i}=($tmp[$i]>=$hash{$i})?$tmp[$i]:$hash{$i};
  }
}
for my $key(sort {$a<=>$b} keys %hash){
  print $hash{$key}," ";
}
__DATA__
0     0     0     0
0     8     9     1
3     8     3     9
7     7     7     7

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. SCO

Need help with gawk

I am trying to use gawk to search a file and put the second value of the string into a string. gawk -F: '$1~/CXFR/ {print $2}' go.dat Below is the file 'go.dat' ==================== HOME :/ CTMP :/tmp CUTL :/u/rdiiulio/bin CWRK :/u/work CXFR :/u/xfer ... (1 Reply)
Discussion started by: trolley
1 Replies

2. Shell Programming and Scripting

Trouble converting columns of integers to floating decimals

I have a document that has 7 columns. eg. $1 $2 $3 $4 $5 $6 $7 string string string string integer integer integer The 6th and 7th columns are a mix of integers and floating decimals (with 4 decimal places). I need to convert the last 2 columns so that all values are floating decimals w/4... (1 Reply)
Discussion started by: kadm
1 Replies

3. Shell Programming and Scripting

Doubt with gawk

Hi All, I have a doubt with gawk. I have a shell script "cleanup" which calls a gawk script "cleanawk" in it. we have two unix servers epsun532 and wpsun712. So i tested the script in both the environments. In epsun532 while calling the gawk script i just mentioned something like this ... (1 Reply)
Discussion started by: Diddy
1 Replies

4. Shell Programming and Scripting

awk (gawk) grep & columns

Hi, I'm working with gawk (on DOS) today. A goal is: find a string for-instance '123', cut a line in two columns and write second one. The problem is: command line works OK, awk file doesn't. But I would like to work with file because there are many strings to find. input: line command: awk... (4 Replies)
Discussion started by: frajer
4 Replies

5. Shell Programming and Scripting

Gawk Help

Hi, I am using the script to print the portion of the file containing a particular string. But it is giving error "For Reading (No such file or directory). I am using cygwin as unix simulator. cat TT35*.log | gawk -v search="12345678" ' /mSOriginating /,/disconnectingParty/ { ... (1 Reply)
Discussion started by: vanand420
1 Replies

6. Shell Programming and Scripting

gawk - reading two files & re arrange the columns

Hi, I am trying to read 2 files and writing to the 3rd file if I find the same elements in 2 files. my first file is 1 0 kb12124819 766409 1.586e-01 1 0 kb17160939 773886 8.674e-01 1 0 kb4475691 836671 8.142e-01 1 0 ... (2 Replies)
Discussion started by: ezhil01
2 Replies

7. Shell Programming and Scripting

gawk and strftime()

Strange behaviour of the strftime() function from gawk (3.1.5): $ awk 'BEGIN{print strftime("%T", 3600)}' > 02:00:00 $ awk 'BEGIN{print strftime("%T", 0)}' > 01:00:00 Obviously something with DST but I can not figure out why? To me 3600 epoch seconds remains 01:00, DST or not. From... (2 Replies)
Discussion started by: ripat
2 Replies

8. Shell Programming and Scripting

gawk will work or not ?

Hai I am using bash-2.03$ bash --version GNU bash, version 2.03.0(1)-release (sparc-sun-solaris) I am not able to use gawk command its showing command not found , why ? Eg: awk 'NR==1' fix.txt | gawk 'BEGIN { FIELDWIDTHS = "3 2" } { printf($1"|"$2); }'... (3 Replies)
Discussion started by: tkbharani
3 Replies

9. Shell Programming and Scripting

gawk HELP

I have to compare records in two files. It can be done using gawk/awk but i am unable to do it. Please help me File1 ABAAAAAB BC asa sa ABAAABAA BC bsa sm ABBBBAAA BC bxz sa ABAAABAB BC csa sa ABAAAAAA BC dsa sm ABBBBAAB BC dxz sa File 2 ABAAAAAB BC aas ba ABAAAAAB BC asa sa... (6 Replies)
Discussion started by: sandeep_hi
6 Replies

10. Shell Programming and Scripting

rs and ors in gawk ...????

:D dear members I have a good knowledge of gawk and seem to do quite well with it.. but I have never understood what the use of the rs and ors are for or how they are used.. i am thinking they are for seperating lines and paragraphs but i have absolutely no idea how to make it work, if that is what... (2 Replies)
Discussion started by: moxxx68
2 Replies
Login or Register to Ask a Question