awk command: column operations


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk command: column operations
# 1  
Old 08-23-2010
awk command: column operations

I have a txt file with two columns

Code:
Freq    Loss
10         30
20         40
30         10
40         50
50         60

i have used the below code to get the minimum value out of this array in second cloumn.

Code:
awk 'NR==N{min=$N;max=$N}NR>N{if ($N>max){max=$N};if ($N<min){min=$N}}END {print ""min" "max}' $dummyfile.arr

my code finds out the minimum in second coulmn, but

1) I want to get correcponding Freq for tht minimum value

---------- Post updated at 12:08 PM ---------- Previous update was at 12:06 PM ----------

I want to output that freq to a txt file
# 2  
Old 08-23-2010
Code:
awk 'NR==1{min=$2}{a[$2]=$1;if($2<min)min=$2}END{print "Min Loss: "min," Freq: "a[min]}' file > out.txt

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 08-23-2010
Do you need ouput the freq or both freq & loss?

Code:
$ cat urfile

Freq    Loss
10         30
20         40
30         10
40         50
50         6

$ awk 'NR==1 {print;loss=999999;next} {if (loss>$2) {loss=$2;freq=$1}}END {print freq "\t" loss}' urfile

Freq    Loss
30      10

This User Gave Thanks to rdcwayx For This Post:
# 4  
Old 08-23-2010
Code:
awk 'END{print l}{if(i>=$2||!i){i=$2;l=$2FS$1}}' file

This User Gave Thanks to danmero For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Mathematical Operations on Column

Hi All, I want to perform a mathematical operation on column. Can anyone please help? Here is the sample of operation to be performed: 123 996 100 123 996 200 123 996 200 2015-09-21 123 996 100 123 996 200 123 996 100 What I want is to multiple all values of column # 3 by 100 and... (3 Replies)
Discussion started by: Zaib
3 Replies

2. Shell Programming and Scripting

How to Replace the value of a column using awk command?

Hi cat test.txt H|123|341|567|asfg D|dfg|trtyy|errt D|ert|frty|wer Here I need to replace the third column value with 100 of the first record only and while printing I need to print the full file content also..I am expecting a result like this H|123|100|567|asfg D|dfg|trtyy|errt... (3 Replies)
Discussion started by: saj
3 Replies

3. Shell Programming and Scripting

awk command to print only selected rows in a particular column specified by column name

Dear All, I have a data file input.csv like below. (Only five column shown here for example.) Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 3,2,4,5,6 5,3,5,5,6 From this I want the below output Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 where the second column... (4 Replies)
Discussion started by: ks_reddy
4 Replies

4. Shell Programming and Scripting

Arthmetic & Operations Using AWK

Trying to do math while using awk and if statements. awk 'BEGIN {FS = ":"} ; {if($2=1) {$3 +=}; print "Total employee work hours: " $3}' works_on.txt The file format is 123456789:1:32.5 123456789:2:7.5 666884444:3:40.0 453453453:1:20.0 453453453:2:20.0 333445555:2:10.0 ... (6 Replies)
Discussion started by: Rapcher
6 Replies

5. Shell Programming and Scripting

how to do arithmetic operations in one line command

I have a file like this product qty1 qty2 value cola 50 25 1 pepsi 100 75 2 muffin 25 30 0.5 would like to do the following operations with one line command 1) disply the line having "Qty1" greater than or equal to 50 2) display the line Qty1 - Qty2 ... (2 Replies)
Discussion started by: johnveslin
2 Replies

6. UNIX for Dummies Questions & Answers

arithmetic operations on 1 column of a file

Hi, I have a file with thousands of lines like this: Chr1 477515 . ACCCC ACCC 17.7 . INDEL;DP=17;AF1=1;CI95=0.5,1;DP4=0,1,0,3;MQ=32;PV4=1,0.036,1,1 Chr1 481987 . A AAAT 62 . INDEL;DP=11;AF1=1;CI95=0.5,1;DP4=0,0,1,3;MQ=41 I want to make a file with... (2 Replies)
Discussion started by: fadista
2 Replies

7. Shell Programming and Scripting

awk command - column merging

I have two files having 3 coulms and 1 column respectively file1.txt 0 22.89 35.60 10 22.80 35.61 20 22.70 35.63 30 22.32 35.68 50 19.23 35.79 75 16.10 35.59 100 15.00 35.52 125 14.45 35.46 150 13.91 35.41 200 12.94 35.28 ... (7 Replies)
Discussion started by: shashi792
7 Replies

8. Shell Programming and Scripting

Help With Array Operations in AWK

I have two files file1 A 2 4 6 8 B 1 3 5 7 C 1 3 5 7 D 1 3 5 7 E 1 3 5 7 file2 C D E F G H I J K L I need to add field 1 of file1 to both field 2s of file2 and repeat the same on all the rows in... (2 Replies)
Discussion started by: cold_Que
2 Replies

9. Shell Programming and Scripting

Masking a column using awk command

Thanks...It was a spoiler for my Team... (5 Replies)
Discussion started by: Diggi
5 Replies

10. Shell Programming and Scripting

command for a series of operations

I want to append a file containing a list of names and the sorted contents of the file should be copied to a new file. All this should be on a single line( using pipes and redirection) Pls help .. thankyou (1 Reply)
Discussion started by: phiber_optik
1 Replies
Login or Register to Ask a Question