There is no guarantee that your questions here will ever be answered. You can be published anonymously - just let us know!
From Paul Wilts
Answered By Ben Okopnik
Hello there, I am hoping you can help me out. I am writing a script. I have a file that has two columns. one column with numbers and one column with names. This file stores users disk-usage/user-name ie: 50000 paul. I would like to run a script/command that would look into the file and if a user is over a certain number , that number/numbers along with the user/users name is copied from that file and put into a different file. I have tried almost everything I know, which is limited, but have not had any success. Thank you for your help
[Ben] Well, you don't say what it is that you tried, or what language the script is in, but I'll take a flyer in a "bash" one-liner. If we have a file called "quotas" that looks like this:
5 joe 7 jim 12 jack 10 jeff 20 jose 1 jerry 3 jenny 8 jamal 6 jude
and we want only those users whose numbers exceed, say, 7, then we might do something like this:
while read a b; do [ $a -gt 7 ] && echo $a $b; done < quotas
What we've done here is read in each line and load the strings into two variables, $a and $b. We then check to see if $a is greater than our target number, and echo both of them if it is.
Note that the whitespace between the names and the numbers is ignored by 'read'; I only put it in to demonstrate how clever "bash" is about stuff like that.
You could also do it in Perl -
perl -wane 'print if $F[0] > 7' quotas
- split each line into an array, print if the 1st member of the array (arrays are indexed starting from 0) is greater than the target.
That should save lots of wear and tear on your fingers.
Thanks very much for your help. Yes I was using Bash. I tried using the test and expr. What would you suggest for a good web site that would also be a good reference for information on scripts. Once again thanks.
[Ben] Heh. I might have a suggestion.
A while ago, I wrote a 6-part series right here in Linux Gazette called "Introduction to Shell Scripting". It's been translated into 7 languages, and is used in at least two college courses. It was intended as a basic text - don't expect to be introduced into The Deepest Mysteries - but I believe that it's a very good start for anyone trying to learn shell scripting, and should get you up to basic competence in short order.
Take a look at LG issues 52-55 and 57-58 or <a href="http://www.linuxgazette.com/search.html">search for my last name (Okopnik)</a>, since one of the articles got misnamed in the e-mail shuffle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |