more bash scripting (simple stuff)

Live forum: /viewtopic.php?t=83

Anonymous

16-12-2004 05:24:48

i'm trying to write a bash script (could be perl) to grep a txt file
for a certain string, and return the next string on the line.
txt file example

cbishop passwd123
wolfie passwd456
dan passwd789
colleen passwd0101
riot password

if i search for colleen, i want 'passwd0101' to be the returned value.

chad

16-12-2004 06:14:03

This should do the trick:

grep colleen file | gawk '{ print $2 }'

wolfie

17-12-2004 03:33:09

Perl would look like this:


open(INPUT,"<$ARGV[0]");
@line = <INPUT>;
close(INPUT);

foreach $line (@line) {
($user,$passwd) = split(/\s/, $line);
if (lc($user) eq lc($ARGV[1])) {
print "Username:" . $user . "\nPassword:" . $passwd . "\n";
}

then you would execute the command as perlscriptname /patch/to/file username

you might have to change the \s to a \t or a \s+ depending on the format of the file but that should get what you want.

wolfjb

17-12-2004 06:45:22

common lisp would look like this: (via clisp [http://clisp.cons.org])



(let ((fname (first ext:*ARGS*))
(search (first (last ext:*ARGS*))))
(with-open-file (s fname)
(do ((line (read-line s) (read-line s nil 'eof)))
((equal search (subseq line 0 (position #\Space line)))
(string-trim '(#\Space #\Tab #\Newline)
(subseq line (position #\Space line)))))))

wolfjb

17-12-2004 07:14:20

or a shorter perl version...


open(INPUT, "ARGV[0]");
($user, $passwd) = split(/\s/, (map(grep(/^ARGV[1]/, $_), <INPUT>))[0]);
close(INPUT);
print $passwd;

wolfjb

17-12-2004 07:59:13

or an even shorter ruby script,

ruby -e 'print File.open("file1").grep(/colleen/)[0].split(/\s/)[1]'

change "file1" and colleen as appropriate

or in a script:


#!/usr/bin/ruby
print File.open("#{ARGV[0]").grep(Regexp.new("#{ARGV[1]}"))[0].split(/\s/)[1]

Anonymous

20-12-2004 04:45:41

i've had some limited success with gawk, thanks chad for the tip.
i had initially wanted to use perl, but had given up as my knowledge
is generally limited to opening/closing/appending txt files.

since there seems to be some mild interest in helping this tard along,
i'll go ahead and post my scenario. maybe someone has a better idea
than the way i'm going about it.

objective .. mail new passwords to users
username/password file generated in the format of
username1 password
username2 password
username3 password etc..

a distribution list is parsed one line at a time.
it is in the format of ...
username1 user1@okccc.edu
username2 user2@okccc.edu
username3 user3@okccc.edu

username and email address are assigned to variables.
a grep is performed on the password file with the
username variable as it's arguement. the output is
piped to gawk which returns the password string.
grep user passwdfile | gawk '{ print $2 }' (thanks chad)

password string is sent via sendmail to the user's
email address.

chad

20-12-2004 05:14:52

I'd go ahead and add "chad.eddings@gmail.com" to the list of recipients for every outbound "password e-mail". :wink:

Anonymous

20-12-2004 05:23:55

i'll be sure and do that.
in fact, why don't i print up a list of all the
school's admin passwords for you!

in reality this would not be necessary. i work in
one of the most backward environments you've
ever seen. obtaining root on any system here
would be trivial.

these password changes (for TELNET login!) are to
be scheduled for MONTHLY distribution, and all
passwords are 8 char, NOT chosen by the user.
these will invariably end up taped to the monitors
of faculty/staff users all over campus.