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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)