LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-08-2009, 09:02 PM   #1
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Rep: Reputation: 17
grep command


Hi,

just wanna know something related to grep.

The contents of my file is

Quote:
cat file.txt
john bravo
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.
Old 12-08-2009, 09:10 PM   #2
GooseYArd
Member
 
Registered: Jul 2009
Location: Reston, VA
Distribution: Slackware, Ubuntu, RHEL
Posts: 183

Rep: Reputation: 46
Quote:
Originally Posted by john83reuben View Post
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.
 
Old 12-08-2009, 09:20 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Not with grep alone, not with your search string, but:
Code:
echo "john bravo"| egrep -ow "[a-z]+o[a-z]+"
john
 
Old 12-08-2009, 09:48 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Tinkster View Post
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.
Old 12-08-2009, 09:50 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by john83reuben View Post
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.
Old 12-08-2009, 11:07 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by ghostdog74 View Post
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 :}
 
Old 12-08-2009, 11:15 PM   #7
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
Originally Posted by Tinkster View Post
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
 
Old 12-09-2009, 03:46 AM   #8
john83reuben
Member
 
Registered: Oct 2007
Location: Kuala Lumpur,Malaysia
Distribution: Debian Etch, OpenSuse
Posts: 132

Original Poster
Rep: Reputation: 17
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
 
Old 12-09-2009, 04:07 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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
 
Old 12-09-2009, 11:21 AM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by john83reuben View Post
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 11:24 AM.
 
  


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
How to pass the result of a command to another command (like grep) desb01 Programming 4 06-25-2009 12:09 PM
Help me in Grep Command + cd command in single line JeiPrakash Linux - Newbie 3 05-27-2008 04:16 AM
grep command sgarci Linux - General 3 08-28-2006 07:13 AM
how to use grep command sharonyiisl Linux - Newbie 7 05-28-2006 03:46 PM
Help Use Grep Command, Thanks! zzytech Linux - Newbie 2 04-05-2006 11:37 PM


All times are GMT -5. The time now is 10:02 PM.

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