LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-03-2005, 05:37 PM   #1
ziox
Member
 
Registered: Apr 2004
Distribution: RedHat, Fedora, Suse, Debian, Gentoo, Mandrake
Posts: 132

Rep: Reputation: 15
grep option needed


HellO...

how can i grep a specific string ???
i want to grep something like this <whatIwant> i want this only So
i tried
grep -o "\<*\>" file > tofile Didn't work
grep -o "<*>" file > tofile Didn't work also
i want to grep just words between this '<' and this '>' symbols

grep,sed or awk all any ...

Last edited by ziox; 01-03-2005 at 05:42 PM.
 
Old 01-03-2005, 05:49 PM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
$ cat testfile
testing <this thing> out
testing this thing out

$ grep -o "<.*>" testfile
<this thing>

$ grep -o "<.*>" testfile | sed 's/<\(.*\)>/\1/'
this thing

That work?
 
Old 01-03-2005, 06:08 PM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Here's an example with sed
sed -n 's/\(.*\)\(<.*>\)\(.*\)/\2/p' file.txt

and here's what grep may look like.
cat file.txt | grep -o "[<].*[>]"
 
Old 01-03-2005, 06:18 PM   #4
ziox
Member
 
Registered: Apr 2004
Distribution: RedHat, Fedora, Suse, Debian, Gentoo, Mandrake
Posts: 132

Original Poster
Rep: Reputation: 15
neither this nor that worked ..
i just want string between something like
<anything> to be grepd not the whole line
when just i say grep "<this>" fromfile > tofile
then i have tofile contain somthing like that
<something>
<anythiung>
<------------->
those examples didn't do so at all

Last edited by ziox; 01-03-2005 at 06:20 PM.
 
Old 01-03-2005, 06:47 PM   #5
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Post up a couple-three lines of the actual text you want to parse and then the result you'd want from those lines.

Based on your description, either of our solutions do work (and homey's sed is way better ), so we must not be following your description. Or maybe you're using a weird shell or weird versions of commands.

-- Here's a copy'n'paste from my xterm:

Code:
~
1016>> cat testfile
testing <this thing> out
testing this thing out
testing <that thing> out

~
1017>> sed -n 's/\(.*\)\(<.*>\)\(.*\)/\2/p' testfile > results

~
1018>> cat results
<this thing>
<that thing>
-- edit2 -- are you using the brackets as examples or are they actually what you want to parse? And do you want them actually in the results or not? That may be part of it.

Last edited by slakmagik; 01-03-2005 at 06:58 PM.
 
Old 01-04-2005, 07:34 PM   #6
ziox
Member
 
Registered: Apr 2004
Distribution: RedHat, Fedora, Suse, Debian, Gentoo, Mandrake
Posts: 132

Original Poster
Rep: Reputation: 15
<this mean > , <the meaning>, this mean <the meaning>, this Mean <the meaning>, but this mean <the meaning>, and this mean <the meaning>, not like this <the meaning>, even this , don't like any of this this mean <the meaning>, this mean <the meaning>, "this mean "<the meaning>, Mine Iz Like this <the meaning>, NOT @ All ,this mean <the meaning>,

$sed -n 's/\(.*\)\(<.*>\)\(.*\)/\2/p' testfile > results
$cat results

<the meaning>,

just 1 line output

and Thanks for your reply
 
Old 01-04-2005, 10:15 PM   #7
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Maybe this is what you have in mind.
Code:
cat file.txt |grep -o '<[a-zA-Z]* [a-zA-Z]*>'
Edit: This is probably better in case you have numbers in the string.
Code:
cat file.txt |grep -o '<[[:alnum:][:space:]]*>'

Last edited by homey; 01-04-2005 at 10:37 PM.
 
Old 01-04-2005, 11:02 PM   #8
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
If there are hard returns in the file and a <bracketed
set> is split across lines, it'll miss them, though. This may fix that. (And I think the variant regex should match more stuff.)

cat file | tr -d \\n | grep -o '<[^>]*>'

Probably other corner cases, but I can't think of them offhand.
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
grep ?? can grep us variables? DaFrEQ Linux - Software 4 09-14-2005 12:22 PM
How to use "OR" option in a grep command. sumitarun Programming 3 04-18-2005 01:44 AM
"ld unrecognized option '--as-needed'" aoe2bug Slackware 5 08-17-2004 05:59 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM
Grep help needed Dirt Linux - Newbie 4 08-03-2003 05:49 PM


All times are GMT -5. The time now is 11:54 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration