grep display word only


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep display word only
# 1  
Old 11-09-2008
grep display word only

Folks, is it possible to display only words with grep (or any built-in ultility)?

I have more than 1 pattern to search, say apple & orange

The text goes like this:

Quote:
tasty apple-bite orange-juice fruit
orange-skin fruit apple-red
So I need to display all the words starting with apple or orange

The output should be:

Quote:
apple-bite
orange-juice
orange-skin
apple-red
Any idea?
# 2  
Old 11-09-2008
You can use awk
Code:
awk '{for(i=1;i++<=NF;)if($i~/^apple|^orange/) print $i}' file

..and please read the forum rules.

Last edited by danmero; 11-09-2008 at 11:46 AM.. Reason: fix logic
# 3  
Old 11-09-2008
With grep:

Code:
tr ' ' '\n' < file | grep -E "^apple|^orange"

or egrep:

Code:
tr ' ' '\n' < file |egrep "^apple|^orange"

# 4  
Old 11-09-2008
Quote:
Originally Posted by Franklin52
With grep:

Code:
tr ' ' '\n' < file | grep -E "^apple|^orange"

or egrep:

Code:
tr ' ' '\n' < file |egrep "^apple|^orange"

You are legend! Smilie

danmero: Thanks, but i should start at 0, and if any word start with a or o, it will also appear in the output, too.
# 5  
Old 11-09-2008
Quote:
Originally Posted by bsddaemon
danmero: Thanks, but i should start at 0, and if any word start with a or o, it will also appear in the output, too.
Yep, I correct the line
Code:
# time awk '{for(i=1;i<=NF;i++)if($i~/^apple|^orange/) print $i}' file > newfile1

real    0m0.715s
user    0m0.550s
sys     0m0.128s
# time tr ' ' '\n' < file | grep -E "^apple|^orange" > newfile2

real    0m0.864s
user    0m0.550s
sys     0m0.238s
# diff newfile1 newfile2
#

# 6  
Old 11-09-2008
Quote:
Originally Posted by bsddaemon
[...]
is it possible to display only words with grep (or any built-in ultility)?
[...]
If you have GNU grep:

Code:
grep -oE '\b(apple|orange)\S*'

Otherwise with Perl:

Code:
perl -nle'print join"\n",/\b((?:apple|orange)\S*)/g'


Last edited by radoulov; 11-09-2008 at 01:01 PM..
# 7  
Old 11-09-2008
Code:
undef $/;
open FH,"<filename";
$str=<FH>;
$str=~s/\n/ /g;
close FH;
print "$_\n" foreach(grep {m/apple|orange/;} (split(" ",$str)));

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to grep for a word and display last transection?

Hi, When we "grep" for a word in a file, it returns the last lines containing the word that we searched for. Is there a way to display last line to grep. Thanks Ex log. Ex. logname.log 2015-07-29 06:43:07.023|BETA |2015-07-29... (5 Replies)
Discussion started by: ooilinlove
5 Replies

2. Shell Programming and Scripting

Grep word after last occurance of string and display next few lines

Hi, I wanted to grep string "ERROR" and "WORNING" after last occurrence of String "Starting" only and wanted to display two lines after searched ERROR and WORNING string and one line before. I have following cronjob log file "errorlog" file and I have written the code for same in Unix as below... (17 Replies)
Discussion started by: nes
17 Replies

3. Shell Programming and Scripting

Need a word which just comes next to after grep of a specific word

Hi, Below is an example : ST1 PREF: int1 AVAIL: int2 ST2 PREF :int1 AVAIL: int2 I need int1 to come in preferred variable while programming and int2 in available variable Please help me doing so Best regards, Vishal (10 Replies)
Discussion started by: Vishal_dba
10 Replies

4. Shell Programming and Scripting

grep part of word or Another word from a string

Hi all, FileOne family balance >>>>> 0 0 0 0 java.io.FileNotFoundException: Settings.xml (No such file or directory) at java.io.FileInputStream.open(Native Method) .. .... ..... ..... java.lang.NullPointerException ... ..... ...... Stacktrace: at... (2 Replies)
Discussion started by: linuxadmin
2 Replies

5. Shell Programming and Scripting

Grep out specific word and only that word

ok, so this is proving to be kind of difficult even though it should not be. say for instance I want to grep out ONLY the word fkafal from the below output, how do I do it? echo ajfjf fjfjf iafjga fkafal foeref afoafahfia | grep -w "fkafal" If i run the above command, i get back all the... (4 Replies)
Discussion started by: SkySmart
4 Replies

6. Shell Programming and Scripting

Finding a "word" through grep but display line above?

Hi guys. I am trying to perform a search using grep. I get my grep to work, but need to "awk" a Process Number that is 2 lines above... Example: I run a query on my TSM server for Processes that are "Waiting" for something...it returns this: Process Number: 32,881 Process... (14 Replies)
Discussion started by: Stephan
14 Replies

7. UNIX for Dummies Questions & Answers

how to grep the word and display only the second word from it

hi, consider the below line in a text file, 'Y',getdate(),'N','V',NULL ..... 'N',getdate(),'Y','D',NULL ..... 'Y','N','Y',getdate(),'Y','D',NULL .... as u see above, i want only the second word after the getdate() word... getdate() will not come 2nd word alwys it may be any position but i... (11 Replies)
Discussion started by: prsam
11 Replies

8. Shell Programming and Scripting

How to Grep for particular word and display..

Hi Guru's.... I've one log file in all my systems which writes the backup information.. I'have written a command like this: ssh -l ora${sid} ${primaryhost} "tail -50 /oracle/$ORACLE_SID/newbackup/END_BACKUP.log" |grep 'insert' |tail -1| awk '{print $7}' We have nearly 50 systems in our... (2 Replies)
Discussion started by: suri.tyson
2 Replies

9. UNIX for Dummies Questions & Answers

how to grep for a word and display only the word

Hi, When we "grep" for a word in a file, it returns the lines containing the word that we searched for. Is there a way to display only the words and not the entire line containing them. Thanks Ananth (6 Replies)
Discussion started by: ananthmm
6 Replies

10. UNIX for Dummies Questions & Answers

grep a word and display its column

Hi, I need idea about this, say I have this line: 05 21 * * 0,6 /user/clean.desktop.sh > /tmp/desktop_rpt 2>&1 I would need to grep the word desktop and display the /user/clean.desktop.sh and not the whole line. And if I have some more lines say, 05 21 * * 0,6 /user/clean.desktop.sh >... (1 Reply)
Discussion started by: Orbix
1 Replies
Login or Register to Ask a Question