[Solved] I need help with a text file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] I need help with a text file.
# 1  
Old 10-20-2011
[Solved] I need help with a text file.

Hello everyone, I need to write a shell script for a file consisting of 3 columns, first column is frequency the second one is power and the last one is number of occurence. I basically need to get the power and the frequency corresponding to the highest number of occurrence number. Below is the part of my file.
Code:
 0.5000  -142   8 0.5000  -143   19 0.5000  -144   14 0.5000  -145   102 0.5000  -146   122 0.5000  -147   106 0.5452  -96     13 0.5452  -97     14 0.5452  -105   3 0.5452  -110   3 .... .... 16.000 -162   87 16.000 -163   115 16.000 -164   151 16.000 -165  77 .....

So at the end I would like to get something like this:
Code:
 0.5000 -146 122 0.5452  -97  14 .... 16.000 -164 151 .....

Can you help me out with this problem?

---------- Post updated at 12:55 PM ---------- Previous update was at 12:46 PM ----------

Sorry somehow the text is not aligned correctly
Here is the sample file

Code:
0.5000  -142   8 
0.5000  -143   19 
0.5000  -144   14 
0.5000  -145   102 
0.5000  -146   122 
0.5000  -147   106 
0.5452  -96     13 
0.5452  -97     14 
0.5452  -105   3 
0.5452  -110   3 
.... .... 
16.000 -162   87 
16.000 -163   115 
16.000 -164   151 
16.000 -165  77 
.....

And the the file I want is;

Code:
0.5000 -146 122 
0.5452  -97  14 
.... 
16.000 -164 151 
.....

# 2  
Old 10-20-2011
please restate you requirements, they are unclear.
does your file look like that below, or its is a single continuous stream unbroken by newline as per your example.
if it looks like the below, does your statement
Quote:
I basically need to get the power and the frequency corresponding to the highest number of occurrence number.
which column is power, frequency,occurrence ...

....
0.5000 -142 8
0.5000 -143 19
0.5000 -144 14
0.5000 -145 102
0.5000 -146 122
0.5000 -147 106
0.5452 -96 13
0.5452 -97 14
0.5452 -105 3
0.5452 -110 3
.... ....
16.000 -162 87
16.000 -163 115
16.000 -164 151
16.000 -165 77
.....

---------- Post updated at 11:10 AM ---------- Previous update was at 11:05 AM ----------

hmmm

given your request and the following dataset
Quote:
0.5000 -142 8
0.5000 -143 19
0.5000 -144 14
0.5000 -145 102
0.5000 -146 122
0.5000 -147 106
0.5452 -96 13
0.5452 -97 14
0.5452 -105 3
0.5452 -110 3
.... ....
16.000 -162 87
16.000 -163 115
16.000 -164 151
16.000 -165 77
i would expect

0.5000 -147 122
0.5452 -110 14
16.000 -165 151

not
Quote:
0.5000 -146 122
0.5452 -97 14
....
16.000 -164 151

so, please restate to avoid ambiguity
# 3  
Old 10-20-2011
Thank you for your reply, It is my first post in this forum so I made some mistakes writing the text file. The file is not a single continuous stream. It is broken by newlines, it has 3 columns; the first column is frequency, the second is the power and the third one is the Number of repeat value of that power in the second column. I would like to obtain for each frequency the maximum repeated power value. So in my file for 0.5000 frequency the maximum repeated power is -146 since it has repeated 122 times(3. column).

So my new file should look like this;

0.5000 -146 122
0.5452 -97 14
16.000 -164 151

I hope this explains better.
# 4  
Old 10-20-2011
Code:
for i in `nawk '{print $1|"sort -u"}' infile`
do 
     grep "^$i" infile | nawk '$3 > max {max=$3; maxline=$0}; END{ print maxline}'
done
0.5000  -146   122
0.5452  -97     14
16.000 -164   151

This User Gave Thanks to jayan_jay For This Post:
# 5  
Old 10-20-2011
MySQL

Thank you very much jayan_jay. It worked great Smilie
# 6  
Old 10-20-2011
I have to do opposite of what you want https://www.unix.com/shell-programmin...ow-column.html
# 7  
Old 10-20-2011
@gvj
No need to advertise in other threads, closing thread.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

[Solved] My script executes poorly as a text file.

Hello again, I have put together a shell script using sed and some shell commands, and it runs pretty well when I am in terminal, but when I save it as a text file and invoke it through the terminal by typing its path, all I get are errors. Can some one give me some hints as to what I am doing... (13 Replies)
Discussion started by: Paul Walker
13 Replies

3. Shell Programming and Scripting

[Solved] Replacing line of text while file is closed

Is it possible to replace a line of text within a file while it's closed with a single command or a script? Please show me an example or point me to a webpage that shows an example. The file has this line of text: LoginGraceTime 100 I want to replace it with the following: ... (2 Replies)
Discussion started by: wdg74
2 Replies

4. Shell Programming and Scripting

[Solved] How to print specific text from a file?

Hi All, I have the below text file from which I have to cut particular section starting from PTR_Security_Rpeorting.cpf to PTR_Security_Reporting_Env93_export. Report Model............: "D:\Cognos_Publishing\tmp.a2R94KLQec"\PTR_Security_Reporting.cpf Report Output Script....:... (4 Replies)
Discussion started by: Vikram_Tanwar12
4 Replies

5. Shell Programming and Scripting

[Solved] Formatting the text file

Hi All, I ahve requirement where I want to put the text file in into proper format. I am wondering how can i achieve that:- Host/Alias Name IP Address Resolved sinuiy01.infra.go2uti.com 10.240.8.158 N sinuid20.devtst.go2uti.com 10.240.8.230 N sinuid21.devtst.go2uti.com... (6 Replies)
Discussion started by: sharsour
6 Replies

6. UNIX for Dummies Questions & Answers

[Solved] Text manipulation help

Hello Unix.com How can I sort from a large email list only the emails that finish with .ca domain? cat <list> | grep "\.ca\b" >> <new list> isnt working perfectly. Any tips? Best regards, Galford D. Weller (2 Replies)
Discussion started by: galford
2 Replies

7. Shell Programming and Scripting

[SOLVED] Extract IP from text

Dear friends, I have a file with the text: href="/archive/ip=x.x.x.x">M</a></td> I want to extract just the IP number. I tried sed, awk but i didn't succeed. Any idea? Thank you Please view this code tag video for how to use code tags when posting code and data. (3 Replies)
Discussion started by: tts0
3 Replies

8. Shell Programming and Scripting

Solved: Text formatting

What's the best way to format the text file shown below the way in which COlA ColB ColC ColD ---- ---- ---- ---- 70 923 AAA 5864 70 testing AAA 13062 PPPPPP test AAA 142 (0 Replies)
Discussion started by: joe228
0 Replies

9. Shell Programming and Scripting

[solved] merging two files and writing to another file- solved

i have two files as file1: 1 2 3 file2: a b c and the output should be: file3: 1~a 2~b 3~c (1 Reply)
Discussion started by: mlpathir
1 Replies

10. UNIX for Dummies Questions & Answers

SOLVED: Text file compare using perl

I have two text file.... One text file contain in this format...... keyvalue.txt \SUM\SUM_cam.c \SUM\SUM_cam.h \SUM\SUM_command.c \SUM\SUM_command.h \SUM\SUM_dab.c \SUM\SUM_dmb.c \SUM\SUM_eventHandler.h \SUM\SUM_eventHandler_dab.c \SUM\SUM_eventHandler_dmb.c ... (6 Replies)
Discussion started by: suvenduperl
6 Replies
Login or Register to Ask a Question