The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep help flood Shell Programming and Scripting 3 06-06-2008 01:14 AM
Grep Aejaz UNIX for Advanced & Expert Users 3 04-30-2008 07:10 AM
grep dineshr85 Shell Programming and Scripting 1 10-10-2007 04:52 AM
how to exclude the GREP command from GREP yamsin789 UNIX for Advanced & Expert Users 2 10-05-2007 02:59 AM
Make grep -c display like grep -n? Jerrad Shell Programming and Scripting 2 08-25-2006 12:20 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 11-12-2007
jazz8146 jazz8146 is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 20
Grep or Sed

Hi All,
I have created a bourne script that basically wants to split a file up in to different parts. I have this working if the file has all the information on different lines but if it doesn't then it doesn't work.
i.e.
If this is the file
hello
12345
good bye
6789
I could grep all the letters and out and put them in a file as the same with the numbers.
But if they were all on the same line when i use grep it copies the whole line.
i.e.
hello 12345 good bye 6789

So when i do grep '[a-z*]' file > letters
it sends the whole line.
I thought i could do like a sed command that substitutes everything except letters to nothing leaving just the letters then save that to a file.
i.e
sed 's/[a-z]// file > numbers

I hope that makes sense. What i'm trying to say is which is the best way to pull out specific text from a file without getting the whole line??

Thanks
Jason
  #2 (permalink)  
Old 11-13-2007
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
to pull out lines that contain numbers, you may use:
Code:
grep -v '[a-z*]' somefile > num_file
similarly, to pull out lines containing alphabets, use:
Code:
grep -v '[0-9*]' somefile > letters_file
but in these cases, if a line contains both numbers and alphabets, it is left out. if you want to extract numeric or alphabetic part from such lines, then this approach is not good. you need to use something else then (grep alone can't help)
  #3 (permalink)  
Old 11-13-2007
gus2000 gus2000 is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 157
As you have seen, "grep" is a tool for identifying whole lines. You, however, need to identify words.

A cheap way to do this is to put each word on its own line before using grep:

Code:
tr ' '  '\n' somefile | grep '^[[:alpha:]]'
But then all your data ends up on individual lines. If you needed each line intact (minus the unwanted data of course) then your sed solution would be better.

Last edited by gus2000; 11-13-2007 at 01:15 AM..
  #4 (permalink)  
Old 11-13-2007
jazz8146 jazz8146 is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 20
Quote:
Originally Posted by gus2000 View Post
As you have seen, "grep" is a tool for identifying whole lines. You, however, need to identify words.

A cheap way to do this is to put each word on its own line before using grep:

Code:
tr ' '  '\n' somefile | grep '^[[:alpha:]]'
But then all your data ends up on individual lines. If you needed each line intact (minus the unwanted data of course) then your sed solution would be better.
I used this command and the result was blank. Here is the data i am using from a file.
13/11/07 $50 new shoes
Here is the command i have used
tr ' ' '\n' < a file | grep '^[[:alpha:]]' > temp
tr

Last edited by jazz8146; 11-13-2007 at 07:32 AM.. Reason: Solved previous issue
  #5 (permalink)  
Old 11-13-2007
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
Quote:
Originally Posted by jazz8146 View Post
I thought i could do like a sed command that substitutes everything except letters to nothing leaving just the letters then save that to a file.
i.e
sed 's/[a-z]// file > numbers
If this is what you really want to do (see the points Yogesh and gus have raised) you can do that. In fact you have almost already done it, there is just a minor detail missing:

s/something/other/

will change only the first occurrence of "something" in every line to "other". If you want to change any occurrence you have to use the "global"-operator:

s/something/other/g

Hence your script would work this way:

sed 's/[a-z]//g' file > numbers

This will replace every (smallcap) character with "nothing", effectively deleting it. If you want to delete *every* character use: "[a-zA-Z]" or, better, "[^0-9.+-]". The "^" will invert the character mask, meaning "everything NOT mentioned here". The given expression will mean "everything, save for numbers 0-9, literal points *) and pluses and minuses **)".

So, your script should read:

sed 's/[^0-9.+-]//g' file > numbers

*) if you could have decimal numbers
**) if you could have signs

bakunin
  #6 (permalink)  
Old 11-13-2007
jazz8146 jazz8146 is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 20
Thanks a lot for the answers they really helped and was easy to understand. What about if the file had dates, money and text? As in if you wanted to extract them three seperately, but all the information was on the same line? Would i have to take the approach of putting one a new line after every word?
Closed Thread

Bookmarks

Tags
grep, grep or, regex

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 12:13 PM.


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