The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
searching one pattern in file and also counting ksr.test UNIX for Advanced & Expert Users 8 10-20-2008 01:03 PM
I am trying to find pattern between two words but unable to get that pattern.. ksr.test UNIX for Advanced & Expert Users 7 09-09-2008 10:34 AM
searching pattern in another file and print cdfd123 Shell Programming and Scripting 2 10-26-2007 03:30 AM
searching for a pattern in a file and forwarding result to another ocelot UNIX for Advanced & Expert Users 7 01-22-2007 12:39 PM
getting file words as pattern matching arunkumar_mca High Level Programming 5 05-31-2005 04:28 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-06-2009
sree_123 sree_123 is offline
Registered User
  
 

Join Date: May 2009
Posts: 2
Question Searching words in a file containing a pattern

Hi all,
I would like to print words in a file seperated by whitespaces containing a specific pattern like "="

e.g. I have a file1 containing strings like
%cat file1
The= some= in
wish= born

<eof> .I want to display only those words containing = i.e The= , some=,wish=

when i grep for pattern "=" in file1 , I get the entire line..
So I decided to loop like
for words in $( grep -x "=" file1)
do
# How do I print only those words containing = here
done

or is there a better way?
  #2 (permalink)  
Old 05-06-2009
abhisek.says abhisek.says is offline
Registered User
  
 

Join Date: May 2008
Posts: 9
Hi,

I guess if you use regular expression avialable in perl it would be good..

But still if you insist to do it using Unix, then in that case you might have to use awk command which would be a little complex..

Regards,
Abhisek
  #3 (permalink)  
Old 05-06-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,559
awk

Code:
# awk '{for(i=1;i<=NF;i++){if ($i ~/=$/) {print $i}}}' file
The=
some=
wish=

alternative solution with Python

Code:
for line in open("file"):
    line=line.strip().split()
    for items in line:
        if items.endswith("="):
            print items,

  #4 (permalink)  
Old 05-06-2009
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,928
With Perl:


Code:
perl -nle'print$1while/(\w+=)/g' infile

With GNU grep:


Code:
grep -Eo \\w+= infile

  #5 (permalink)  
Old 05-06-2009
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink Would this work for you?


Code:
> cat file15
The= some= in
wish= born

> tr " " "\n" <file15
The=
some=
in
wish=
born

> tr " " "\n" <file15 | grep "="
The=
some=
wish=

  #6 (permalink)  
Old 05-07-2009
sree_123 sree_123 is offline
Registered User
  
 

Join Date: May 2009
Posts: 2
Thanks a lot for the ideas
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:48 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0