LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-10-2011, 09:10 PM   #1
linux_evangelist
LQ Newbie
 
Registered: Oct 2010
Posts: 14

Rep: Reputation: 0
Exclamation stupid simple for loop to match usernames in /etc/features


So I've got these two files, see. One is a list of usernames I gathered from:

Code:
ls -AlHr --sort=time /var/mail > users
I cleaned up my "users" file (from the command above) and now it contains only usernames of people who haven't checked their emails in forever.

The other file is /etc/features, which has a list of all usernames and a "description" field containing the user's real name. It is formatted like so:

Code:
office:mail=10,ftp=-1,description=Main Office
jbeam:mail=40,ftp=-1,description=Jim Beam
johnson:mail=25,ftp=-1,description=Michael Johnson
photo1:mail=25,ftp=-1,description=Michael Johnson
harleyhog:mail=10,ftp=-1,description=Guy George
dana:mail=50,ftp=-1,description=Dana Harvey
doug:mail=10,ftp=0,description=CD - Doug Adams
I want to compare those two files and pick out only the lines containing names which appear in "users." For example, photo1 may be the only name which appears in both places. Here's the script I wrote to do that:

Code:
#!/bin/bash
for i in `cat ~/users`;
do grep $i /etc/features;
done
When I run this, it sets the environment variable correctly, but it never matches the data. If I pass "echo $i" it contains the value found last in "users."

Now, here's the weird part. I wanted to see if it was searching literally for "$i" so I passed:

Code:
echo "$i testing..." >> /etc/features
And when I ran my script again, it displayed:

Code:
 testing...
^space
How is grep matching the data and how do I write it properly to do what I need?

Thanks in advance!
 
Old 11-10-2011, 11:37 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,499

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Please show what the content of the users file looks like?
 
Old 11-11-2011, 10:23 AM   #3
berbae
Member
 
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
Code:
grep --fixed-strings "$(<~/users)" /etc/features
If the users file contains only the names, one per line.
The grep option has the -F short option form.

Last edited by berbae; 11-11-2011 at 10:36 AM.
 
Old 11-12-2011, 12:10 AM   #4
jthill
Member
 
Registered: Mar 2010
Distribution: Arch
Posts: 211

Rep: Reputation: 67
The grep-them-all approach starts running out of steam as your search sets get large, you'll eventually need `join`. I just wrote a script an hour ago that uses it, I think it'll give you the idea. Run it on any plain-text file with a .txt suffix to see what's going on, the default is oanc.txt.
Code:
DSN=${1:-oanc}
COUNT=${2:-3000}
START=${3:-10000}
 sed -r 's/[^A-Za-z]+/\n/g' $DSN.txt | tr A-Z a-z > $DSN.words
 sort $DSN.words | uniq -c | sort -nrk1 > $DSN.counts
 awk '{print NR,$0}' $DSN.words | sort -k2 >$DSN.indexed 
 awk '$DSN >= '$START' { start=NR } start<NR && NR <= start+'$COUNT' { print $2 }' $DSN.counts |sort >$DSN.midrange
 join -2 2 $DSN.midrange $DSN.indexed | sort -nk2 | sed 's/ .*//' > $DSN.payload
 
Old 11-12-2011, 02:11 AM   #5
linux_evangelist
LQ Newbie
 
Registered: Oct 2010
Posts: 14

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
Please show what the content of the users file looks like?
A friend found it. The "users" file had been opened in excel and edited, then uploaded back into the server for use in the script.

iconv -f utf-16 -t utf-8 ./users | fromdos > users.new put it in the format we needed.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Stupid g++ compiler does not even support foreach loop, similarly for Visual C++ xuancong Programming 12 01-09-2011 10:58 PM
[SOLVED] Simple regex match without replace? nrg Programming 1 11-21-2009 03:04 AM
Simple mobiles, without advanced features. Is it possible? pslacerda Linux - Mobile 2 04-29-2009 02:29 AM
Bash: proceeding only if a string from a for loop does *not* match some value kylebarbour Programming 11 03-12-2009 04:25 AM
simple pattern match with awk, sed alenD Linux - Newbie 10 03-10-2008 02:31 PM


All times are GMT -5. The time now is 08:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration