Help with simple bash script involving a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with simple bash script involving a loop
# 1  
Old 05-26-2016
Help with simple bash script involving a loop

Dear unix wizards,

I'd be very grateful for your help with the following.

I have a hypothetical file (file.txt) with three columns:

Code:
111 4 0.01 
112 3 0.02
113 2 0.03
114 1 0.04
115 1 0.06
116 2 0.02
117 3 0.01
118 4 0.05

Column 2 consists of pairs of integers from 1 to 4 (each number only occurs twice). I want to:
- find the two lines with matching values for column 2
- then of the two, pick out the line that has the greatest value for column 3
- then finally print column 1 of that line.
I want to use a looped bash script to do this, as in reality, the values in column 2 go from 1 to about 10,000.

I have tried:

Code:
#! bin/bash
for i in {1..4}
do
cat file.txt | awk '{print $2}' | grep -w "$i" | sort -k 3 | head -2 | tail -1 | awk '{print $1}'
done

In the hope that it would give me an output that looks like:

Code:
115
113
112
118

However, I'm getting nothing at all, and have clearly gravely misunderstood something here.

Please help!

Many thanks.

---------- Post updated at 12:53 PM ---------- Previous update was at 12:43 PM ----------

I'd omitted "$" before "i".
Silly mistake - sorry.

It sort of works now, but because I used awk '{print $2}' to search in that column, the final value that is printed is not from the original column 1 in the file, but from column 2 (as that is the only remaining column).

Is there a way around this?
And also, a more elegant way to script this?

Thanks.

Last edited by Don Cragun; 05-26-2016 at 04:39 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 05-26-2016
Hello aberg,

If you are not worried about the order of the printing of 1st field then following may help you in same.
Please let me know how it goes then.
Code:
awk 'FNR==NR{A[$2]=A[$2]>$3?A[$2]:$3;next} ($2 in A){if($3==A[$2]){print $1;delete A[$2]}}'  Input_file  Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 05-26-2016
Code:
sort -n -k2 -k3 infile | awk '!a[$2]++ && l {print l} {l=$1}; END {print l}'

This User Gave Thanks to rdrtx1 For This Post:
# 4  
Old 05-26-2016
Code:
awk '{if(p[$2]<$3){p[$2]=$3;c[$2]=$1;}}END{for(i in c){print c[i]}}' example.data

Output:

Code:
113
112
118
115

This User Gave Thanks to Aia For This Post:
# 5  
Old 05-26-2016
Thanks RavinderSingh13 and rdrtx1. Much appreciated.
# 6  
Old 05-27-2016
This one prints at the first occasion in the main loop, i.e. does not need an explicit loop in the END section
Code:
awk '($2 in A3) {print ($3>A3[$2] ? $1 : A1[$2]); next} {A1[$2]=$1; A3[$2]=$3}' file.txt

Also I have the habit to check for existence first ($2 in A3) so I don't need to think about unitialized fields, negative numbers, etc.

Last edited by MadeInGermany; 05-27-2016 at 05:33 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Simple script not working- for loop

Hi all, Please guide me writing this script Follwing is the file which I have created, which contains the files to be copied. cat system1-dr.txt /etc/passwd /etc/shadow /etc/group /etc/vfstab /etc/profile /etc/default/init /etc/shells /etc/dfs/dfstab /etc/dfs/sharetab... (11 Replies)
Discussion started by: manalisharmabe
11 Replies

2. Shell Programming and Scripting

simple while loop script to check a connection

Hi, I am completely new to shell scripting. Basically I am wanting to create a simple while loop script to check if a network connection is available from 'netstat -a' and if its present then output a character to the serial port. I think i am nearly there.. but need some assistance.. this is... (9 Replies)
Discussion started by: zippyzip
9 Replies

3. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

4. Shell Programming and Scripting

a simple for loop in bash and zsh

Hi! I am just starting to learn scripting. I am trying a simple script in bash and zsh I have two questions: First: Why zsh does not expand the var M? What I am doing wrong? localhost galanom # bash -c 'M="m1 m2 m3 m4 m5"; for x in $M; do echo "<$x>"; done' <m1> <m2> <m3> <m4> <m5>... (1 Reply)
Discussion started by: galanom
1 Replies

5. Shell Programming and Scripting

Simple bash script help

Hi to everyone here, I'm a new user and relatively-new linuxer. I'm trying to write a script that checks if every file from a directory is present in a given list and if not, delete it. should be simple. But I think I've done half the work only: this is to create the reference list: for c... (2 Replies)
Discussion started by: dentex
2 Replies

6. UNIX for Dummies Questions & Answers

simple script with while loop getting problem

Hello forum memebers. can you correct the simple while program. #! /bin/ksh count=10 while do echo $count count='expr$count-1' done I think it will print 10 to 1 numbers but it running for indefinite times. (2 Replies)
Discussion started by: rajkumar_g
2 Replies

7. Shell Programming and Scripting

Simple LOOP script help

I am trying to create a simple ksh loop script. What I've pasted below is not working. Any help is appreciated. typeset -i count typeset -i tcount count=0 tcount=0 while $tcount >= 11 do print "\$count is $count" pwd ls -l sleep 30 $(( $tcount = $count + 1 )) ... (5 Replies)
Discussion started by: musikman65
5 Replies

8. Shell Programming and Scripting

Simple loop in Bash

Hi, I want to do a simple loop where I have one column of text in a file and I want the loop to read each line of the file and do a simple command. The text file will be something like this: hostname1 hostname2 hostname3 hostname4 I am using Bash and have already come up with this to... (1 Reply)
Discussion started by: BrewDudeBob
1 Replies

9. Shell Programming and Scripting

Simple bash for loop problem

I'm just trying to make a script that runs in command line to echo each line in a text file. Everything i found on google is telling me to do it like this but when I run it it just echos removethese.txt and thats it. Anyone know what im doing wrong? for i in removethese.txt; do echo $i; done ... (4 Replies)
Discussion started by: kingdbag
4 Replies

10. Shell Programming and Scripting

Simple script loop question

Hey all. Thanks in advance for any help you can give, hopefully this is an easy one. I want to create a loop to run a simple performance monitor like vmstat and record it to a file, but have very limited scripting skills (obviously). Starting with this... date >> /var/log/perfmon.log vmstat... (2 Replies)
Discussion started by: mattlock73
2 Replies
Login or Register to Ask a Question