Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 11-10-2011, 10:10 PM   #1
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:

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:

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:

for i in `cat ~/users`;
do grep $i /etc/features;
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:

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

How is grep matching the data and how do I write it properly to do what I need?

Thanks in advance!
Old 11-11-2011, 12:37 AM   #2
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,627

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

Rep: Reputation: Disabled
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 11:36 AM.
Old 11-12-2011, 01:10 AM   #4
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.
 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, 03:11 AM   #5
LQ Newbie
Registered: Oct 2010
Posts: 14

Original Poster
Rep: Reputation: 0
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 > put it in the format we needed.


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 11:58 PM
[SOLVED] Simple regex match without replace? nrg Programming 1 11-21-2009 04:04 AM
Simple mobiles, without advanced features. Is it possible? pslacerda Linux - Mobile 2 04-29-2009 03:29 AM
Bash: proceeding only if a string from a for loop does *not* match some value kylebarbour Programming 11 03-12-2009 05:25 AM
simple pattern match with awk, sed alenD Linux - Newbie 10 03-10-2008 03:31 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:58 AM.

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