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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
12-08-2009, 10:02 PM
|
#1
|
Member
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132
Rep:
|
grep command
Hi,
just wanna know something related to grep.
The contents of my file is
when i do a
Quote:
grep [a-z]o file.txt
john bravo
|
Is there a way to get only the string john and not the whole line.
John
|
|
|
Click here to see the post LQ members have rated as the most helpful post in this thread.
|
12-08-2009, 10:10 PM
|
#2
|
Member
Registered: Jul 2009
Location: Reston, VA
Distribution: Slackware, Ubuntu, RHEL
Posts: 183
Rep:
|
Quote:
Originally Posted by john83reuben
Hi,
Is there a way to get only the string john and not the whole line.
John
|
If you just want the part of the line that matches the pattern you provided to grep, just use grep -o
If you always want the nth column, try:
grep whatever file.txt | awk '{ print $1 }'
$1 gets the first column, $2 the second, and so on.
|
|
|
12-08-2009, 10:20 PM
|
#3
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Not with grep alone, not with your search string, but:
Code:
echo "john bravo"| egrep -ow "[a-z]+o[a-z]+"
john
|
|
|
12-08-2009, 10:48 PM
|
#4
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Quote:
Originally Posted by Tinkster
Not with grep alone, not with your search string, but:
Code:
echo "john bravo"| egrep -ow "[a-z]+o[a-z]+"
john
|
here's from the man page
Quote:
In addition, three variant programs egrep, fgrep and rgrep are
available. egrep is the same as grep -E. fgrep is the same as
grep -F. rgrep is the same as grep -r. Direct invocation as either
egrep or fgrep is deprecated, but is provided to allow historical
applications that rely on them to run unmodified.
|
|
|
2 members found this post helpful.
|
12-08-2009, 10:50 PM
|
#5
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Quote:
Originally Posted by john83reuben
Hi,
just wanna know something related to grep.
The contents of my file is
when i do a
Is there a way to get only the string john and not the whole line.
John
|
you should define clearly what your search criteria is. why is "bravo" not included ? is it because you want to omit it whenever nothing follows "o" ?
|
|
1 members found this post helpful.
|
12-09-2009, 12:07 AM
|
#6
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Quote:
Originally Posted by ghostdog74
here's from the man page
|
Goes to show that one needs to keep reading man-pages.
I had no idea it was deprecated. Thank you :}
|
|
|
12-09-2009, 12:15 AM
|
#7
|
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
Quote:
Originally Posted by Tinkster
Goes to show that one needs to keep reading man-pages.
I had no idea it was deprecated. Thank you :}
|
+1 from me too -- thanks (though I look at the grep man page often enough, I guess I don't read it very intently because I didn't know that)
Sasha
|
|
|
12-09-2009, 04:46 AM
|
#8
|
Member
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132
Original Poster
Rep:
|
Thanks for all the replies.
Actually, I was trying to learn to grab only one preffered string from a line of text. Thats y I asked.
My actual intention is,
my file will consist something like this
Quote:
{+12643834} apple orange {+melon}
6434 {+test} [+jsdhfshdf]
|
So I would like to grab only the string that starts with {+
Im trying to learn regular expression, but gosshh....<complicating>
One more advice from experts, can I perform the task without using much regex, or can it be done just by awk?
Thanks
|
|
|
12-09-2009, 05:07 AM
|
#9
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
the regex to get start of string, is ^, so if you use grep or awk
Code:
grep "^{+" file
awk '/^{+/' file
if you don't want to use too much regex, you can get string index, then compare with "{+"
eg shell
Code:
while read -r line
do
# get first 2 characters
[ "${line:0:2}" == "{+" ] && echo "$line"
done <"file"
or gawk
Code:
gawk 'substr($0,1,2) == "{+"' file
another alternative language, which have clean syntax and strong string parsing capabilities (almost never the need to use regex) , is Python
Code:
for line in open("file"):
if line[0:2] == "{+": # or line.lstrip().startswith("{+")
print line
|
|
|
12-09-2009, 12:21 PM
|
#10
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Quote:
Originally Posted by john83reuben
Thanks for all the replies.
Actually, I was trying to learn to grab only one preffered string from a line of text. Thats y I asked.
My actual intention is,
my file will consist something like this
Code:
{+12643834} apple orange {+melon}
6434 {+test} [+jsdhfshdf]
So I would like to grab only the string that starts with {+
Im trying to learn regular expression, but gosshh....<complicating>
One more advice from experts, can I perform the task without using much regex, or can it be done just by awk?
Thanks
|
Code:
echo "{+12643834} apple orange {+melon}
6434 {+test} [+jsdhfshdf]"| grep -Eo '\{\+\w+\}'
{+12643834}
{+melon}
{+test}
Last edited by Tinkster; 12-09-2009 at 12:24 PM.
|
|
|
All times are GMT -5. The time now is 04:47 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|