UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 17th, 2012, 05:45 PM
johnhisenburg johnhisenburg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 9 johnhisenburg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 6 m 47 sec
Reputation Power: 0
Grep search Help

Lets say I had a text file called "file" and it looked like this

1234567|HI|My name is Bob|Dave
8911123|NI|My name is Dave|Bob

And I wanted to search the file in the third bracket position (where it says "My name is Bob" and "My name is Dave") and I wanted it to search for "Bob". So only the "Bob" from the first line would show up, not the "Bob" from the second line.

How would I do that?

Reply With Quote
  #2  
Old September 17th, 2012, 06:29 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,698 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 4 h 53 m 2 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Actually awk would be easier.
Code:
$ awk -F '|' '$3 ~ /Bob/' file

Reply With Quote
  #3  
Old September 18th, 2012, 11:31 AM
johnhisenburg johnhisenburg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 9 johnhisenburg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 6 m 47 sec
Reputation Power: 0
Quote:
Originally Posted by requinix
Actually awk would be easier.
Code:
$ awk -F '|' '$3 ~ /Bob/' file


Thanks worked perfectly!

If I were to add another search to this search and I wanted to search for the word "Alf" but it was in the 5th position (after the 5th '|') How would I add that onto it?

Like this...?

Code:
awk -F '|' '$3 ~ /Bob/ $5 ~/Alf/' file

Reply With Quote
  #4  
Old September 18th, 2012, 11:39 AM
johnhisenburg johnhisenburg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 9 johnhisenburg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 6 m 47 sec
Reputation Power: 0
Also, lets say I have a file listing numbers on different lines from 1 - 100.

For example...

1
2
3
4
5
...
100

How would I search 56-95 (inclusive)?

Reply With Quote
  #5  
Old September 18th, 2012, 12:46 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,698 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 4 h 53 m 2 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Quote:
Originally Posted by johnhisenburg
If I were to add another search to this search and I wanted to search for the word "Alf" but it was in the 5th position (after the 5th '|') How would I add that onto it?

Like this...?

Code:
awk -F '|' '$3 ~ /Bob/ $5 ~/Alf/' file

Check the documentation for the awk program. There's a very simple way of and-ing two conditions together. Or cheat and look below.

Quote:
Originally Posted by johnhisenburg
Also, lets say I have a file listing numbers on different lines from 1 - 100.

For example...

1
2
3
4
5
...
100

How would I search 56-95 (inclusive)?

Any program supporting scripting would work fine (awk, Perl, PHP, bash...). Continuing with awk,
Code:
$ awk '$0 >= 56 && $0 <= 95' file

Reply With Quote
  #6  
Old September 18th, 2012, 03:47 PM
johnhisenburg johnhisenburg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 9 johnhisenburg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 6 m 47 sec
Reputation Power: 0
Quote:
Originally Posted by requinix
Check the documentation for the awk program. There's a very simple way of and-ing two conditions together. Or cheat and look below.


Any program supporting scripting would work fine (awk, Perl, PHP, bash...). Continuing with awk,
Code:
$ awk '$0 >= 56 && $0 <= 95' file


Got it! I just had to use the && symbol for and-ing the two conditions. Thanks so much for your help.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Grep search Help

Developer Shed Advertisers and Affiliates



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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap