Fedora Linux Support Community & Resources Center

Go Back   FedoraForum.org > Fedora 17/18 > Using Fedora
FedoraForum Search

Forgot Password? Join Us!

Using Fedora General support for current versions. Ask questions about Fedora and it's software that do not belong in any other forum.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 8th January 2010, 05:19 PM
qsub Offline
Registered User
 
Join Date: Nov 2008
Posts: 9
windows_7firefox
Question Need help using grep, need to grep something from a txt file.

Hi,

Basically I have a file which displays a users UID/GID/Username.

I want to basically take out the username and disgard the rest.

So lets pretend first line in a output.txt file is
Code:
uid=500(Bobby) gid=500(Bobby)
uid=501(Jim) gid=500(Jim)
I want to to basically read the first line in the text file and get the name Bobby to output. disgard the rest.

Can anyone help me with this? Preferably with the command grep in as short amount of characters as possible, just to keep it less complicated. This should be fairly simple id imagine.

Thanks,
Reply With Quote
  #2  
Old 8th January 2010, 05:33 PM
notageek's Avatar
notageek Offline
Registered User
 
Join Date: Jan 2008
Location: New Delhi, India
Posts: 2,068
linuxgentoofirefox
Code:
awk ' {print $1 }' filename | sed 's/uid=[0-9][0-9][0-9](//'|sed 's/)//'
This probably can be cleaned up.
Reply With Quote
  #3  
Old 8th January 2010, 06:41 PM
weitjong's Avatar
weitjong Online
Registered User
 
Join Date: Oct 2006
Location: Singapore, 新加坡
Posts: 735
linuxfedorafirefox
The grep command is not suitable for this task as it outputs the matching lines. To cut the line and output its part I usually use the cut command.

$ head -1 output.txt | cut -f2 -d'(' | cut -f1 -d')'
__________________
YaoWT - Leave no window unbroken ^_^
Reply With Quote
  #4  
Old 8th January 2010, 06:58 PM
PabloTwo's Avatar
PabloTwo Offline
"Registered User" T-Shirt Winner
 
Join Date: Mar 2007
Location: Seville, FL
Posts: 5,118
linuxfedorafirefox
With the grep command, you have to know the pattern you are searching for. So, if you already know you are searching for the name Bobby in a file named grepfile.....
Code:
BASH:~/-> cat grepfile
uid=500(Bobby) gid=500(Bobby)
uid=501(Jim) gid=500(Jim)
Code:
BASH:~/-> grep -o Bobby grepfile
Bobby
Bobby
To grep for names Bobby and Jim
Code:
BASH:~/-> grep -o -e Bobby -e Jim grepfile
Bobby
Bobby
Jim
Jim
If you don't know the names you're searching for, then grep isn't the command you want to use.
Reply With Quote
  #5  
Old 8th January 2010, 07:26 PM
brunson Offline
Registered User
 
Join Date: Jun 2005
Location: Westminster, Colorado
Posts: 2,304
linuxsafari
I can't tell from the original question if you only want to print the username from the first line, or all lines.

If it's the former:
Code:
ebrunsonlx(~)$ sed -ne '1{s/.*(\([^)]*\)).*/\1/p}' grepfile 
Bobby
if the latter:
Code:
ebrunsonlx(~)$ sed -ne 's/.*(\([^)]*\)).*/\1/p' grepfile 
Bobby
Jim
__________________
Registered Linux User #4837
411th in line to get sued by Micro$oft
Quote:
Basically, to learn Unix you learn to understand and apply a small set of key ideas and achieve expertise by expanding both the set of ideas and your ability to apply them - Paul Murphy
Reply With Quote
  #6  
Old 9th January 2010, 06:32 AM
weitjong's Avatar
weitjong Online
Registered User
 
Join Date: Oct 2006
Location: Singapore, 新加坡
Posts: 735
linuxfedorafirefox
PabloTwo just gives me an idea of using grep to solve the problem. For me, grep is still not suitable for this task but it is not impossible.

$ grep -m 1 -o -P '(?<=\().*(?=\)\sgid)' output.txt

I am sure I will scratch my head to explain how it works myself after a while

Better alternative:

$ awk 'BEGIN {FS="[()]"}{print $2;exit}' output.txt
__________________
YaoWT - Leave no window unbroken ^_^

Last edited by weitjong; 9th January 2010 at 06:56 AM.
Reply With Quote
  #7  
Old 9th January 2010, 10:28 PM
PabloTwo's Avatar
PabloTwo Offline
"Registered User" T-Shirt Winner
 
Join Date: Mar 2007
Location: Seville, FL
Posts: 5,118
linuxfedorafirefox
Code:
BASH:~/-> grep -m 1 -o -P '(?<=\().*(?=\)\sgid)' grepfile
Bobby
Having read most of the Bash Programming book that I got for xmas from my wife, I actually understand most of that line

---------- Post added at 05:28 PM CST ---------- Previous post was at 10:44 AM CST ----------

I played around with this using only the printf external command, letting bash do all the rest.
Code:
BASH:~/-> read var < grepfile && var="${var##*(}" && printf "%s\n" ${var%%)*}
Bobby
Reply With Quote
Reply

Tags
file, grep, txt

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
grep help Wiles Using Fedora 6 25th August 2006 07:50 PM
"grep" username in samba or grep file owner? overlord Servers & Networking 0 22nd July 2005 02:12 PM


Current GMT-time: 14:09 (Sunday, 19-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat