Using input from one file to define scope of other file in Linux


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using input from one file to define scope of other file in Linux
# 8  
Old 03-13-2015
Yes, Just because a value isn't a min value doesn't mean its max, don't know what I was thinking there.
# 9  
Old 03-13-2015
Quote:
Originally Posted by Chubler_XL
Yes, Just because a value isn't a min value doesn't mean its max, don't know what I was thinking there.
If the input file had been sorted with:
sort -o B.txt -k1,1 -k2,2n B.txt
which could be true with the sample input shown, your code would work just fine. In fact, the three lines in your code:
Code:
  if(!($1 in M)) M[$1]=X[$1]=$2
  else if($2<M[$1]) M[$1]=$2
  else X[$1]=$2

could be simplified even more to just:
Code:
  if(!($1 in M)) M[$1]=$2
  X[$1]=$2

This User Gave Thanks to Don Cragun For This Post:
# 10  
Old 03-13-2015
Thanks guys. Just got a couple of follow-ups:

1. Let's say I want to add one more column counting the number of pings per address. That would only be to add c[i], right?

2. Would it be possible to describe briefly what happens here?

Code:
if(!($1 in c)) m[$1] = M[$1] = $2
	else	if($2 > M[$1]) M[$1] = $2
	else	if($2 < m[$1]) m[$1] = $2

Then I can edit the file myself so it fits other types of files I'm working on.

Cheers!
Z
# 11  
Old 03-13-2015
Quote:
Originally Posted by Zooma
Thanks guys. Just got a couple of follow-ups:

1. Let's say I want to add one more column counting the number of pings per address. That would only be to add c[i], right?

2. Would it be possible to describe briefly what happens here?

Code:
if(!($1 in c)) m[$1] = M[$1] = $2
	else	if($2 > M[$1]) M[$1] = $2
	else	if($2 < m[$1]) m[$1] = $2

Then I can edit the file myself so it fits other types of files I'm working on.

Cheers!
Z
  1. Yes, c[i] in the END clause in this awk program is the number of times IP address i has been seen in the input file(s) that have been processed.
  2. Do the following comments sufficiently explain what this code does?
Code:
	if(!($1 in c)) m[$1] = M[$1] = $2	# 1st line with this IP? Set min & Max for this IP
	else	if($2 > M[$1]) M[$1] = $2	# else, if new Max for this IP, save it
	else	if($2 < m[$1]) m[$1] = $2	# else, if new min for this IP, save it

This User Gave Thanks to Don Cragun For This Post:
# 12  
Old 03-13-2015
Thanks again Don. Just put this code in a bash script and I would like to be able to specify the input file as an argument. So I would like to call:

Code:
./stats.sh B.txt

for example. Tried to change "B.txt" in the file to various things (like $1 but that's already in use in the file...). What would do the trick?

Last edited by Zooma; 03-13-2015 at 11:57 PM.. Reason: Added .sh
# 13  
Old 03-14-2015
Quote:
Originally Posted by Zooma
Thanks again Don. Just put this code in a bash script and I would like to be able to specify the input file as an argument. So I would like to call:

Code:
./stats.sh B.txt

for example. Tried to change "B.txt" in the file to various things (like $1 but that's already in use in the file...). What would do the trick?
What happened when you replaced the filename operand to awk in stats.sh with $1?

That should be OK. The $1 in an awk script expands to the contents on the 1st first field in the current input line, but the $1 in a shell script expands to the contents of the 1st command line argument passed to that shell script.
This User Gave Thanks to Don Cragun For This Post:
# 14  
Old 03-14-2015
You're right of course. The problem was that I used a script editor and got the line ending wrong. Fixed that with:

Code:
sed -i 's/\r//' stats.sh

Ok, the topic has derailed a bit, but here is the final one: if I also add a third column with the URL for each IP in the input file (B.txt), how can I print that in the final table? Tried to add:

name[$1] = $3

Assuming that this would set the variable 'name' for a certain IP address to the URL listed in column 3 in the infile. So if this is a row in the infile:

Code:
62.147.28.224  12  www.myserver.com

The output file would get one more column to the right with www.myserver.com. But it just prints zero (0) for me in that column.

Promise this is the last one in this thread ;-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash Variable scope - while loop while reading from a file

Cope sample1: test.sh i=0 echo " Outside loop i = $i " while do i=$(( $i + 1)) echo "Inside loop i = $i " done echo " Out of loop i is : $i " When run output : Outside loop i = 0 Inside loop i = 1 Inside loop i = 2 Inside loop i = 3 Inside loop i = 4 Inside loop i = 5 Inside... (8 Replies)
Discussion started by: Adarshreddy01
8 Replies

2. Shell Programming and Scripting

How to define a variable in a BASH script by using a JSON file online?

Hello, I would like to modify an existing script of mine that uses a manually defined "MCVERSION" variable and make it define that variable instead based on this JSON file stored online: https://s3.amazonaws.com/Minecraft.Download/versions/versions.json Within that JSON, I 'm looking for... (4 Replies)
Discussion started by: nbsparks
4 Replies

3. UNIX for Advanced & Expert Users

Passwd file define user with special character

Hi all , The FTP user defind in my passwd file has ! in the hash password field and i want to know way is that its usually either MD5(Unix) hash or * can anyone explain to me i'm new for unix and want to learn this how my passwd file looks : ... (2 Replies)
Discussion started by: dahash11
2 Replies

4. Shell Programming and Scripting

Define variable from file.

HI I have file A.txt _1A _2A _3A _4A I want define all as different variable. $1A=_1A $2B=_2A $3C=_3A $4D=_4A Now i can use any variable in my script. (3 Replies)
Discussion started by: pareshkp
3 Replies

5. Shell Programming and Scripting

The scope of the shell/perl script is to read the input text file. Validate the expiry date of each

The scope of the shell/perl script is to read the input text file. Validate the expiry date of each certificate and send the mail to the user. The user takes action to add the new certificate to the storage file and user owns the responsibility to update the input text file with the new certificate... (5 Replies)
Discussion started by: casmo
5 Replies

6. UNIX for Dummies Questions & Answers

How to define input files more than 9

awk -v nfiles="10" { ......................... ............ }' $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 up to9 inputs script is running fine but after that it is throwing error file not found though there is a file. I used {$10} but no use. (1 Reply)
Discussion started by: quincyjones
1 Replies

7. Shell Programming and Scripting

Need script to take input from file, match on it in file 2 and input data

All, I am trying to figure out a script to run in windows that will allow me to match on First column in file1 to 8th Column in File2 then Insert file1 column2 to file2 column4 then create a new file. File1: 12345 Sam 12346 Bob 12347 Bill File2:... (1 Reply)
Discussion started by: darkoth
1 Replies

8. Programming

which head file define '_IO_*'

Under Solaris 10,I compile following file, #include <sys/types.h> #include <sys/stat.h> #include <sys/termios.h> #include <sys/ioctl.h> #include <stdio.h> #include <stdlib.h> #include <stddef.h> #include <string.h> #include <unistd.h> #include <signal.h> #define _IO_UNBUFFERED __SNBF... (1 Reply)
Discussion started by: konvalo
1 Replies

9. Shell Programming and Scripting

Problem in define cpu in Rules file

i try to install solaris OS in M4000 using OK prompts by boot net . in my rules file i define the cpus like below probe cpus probe networks networks 2 && cpus 8-10 && disksize c0t0d0 100000-292000 && disksize c0t1d0 100000-292000 && memsize 16384 && model SUNW,SPARC-Enterprise... (0 Replies)
Discussion started by: neruppu
0 Replies

10. Solaris

define .rhost file for tape backup to remote host

howdy experts, i am using 2 server- Solaris 5.9 i have tape device attached with 1 of my solaris server. But others not. # modinfo|grep tape 152 13d43e4 1333c 33 1 st (SCSI tape Driver 1.231) now i want to Backup DATA file and System File in Tape Drive. How do I take data and... (3 Replies)
Discussion started by: thepurple
3 Replies
Login or Register to Ask a Question