![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread |
Rating:
|
Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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:]]' Last edited by gus2000; 11-13-2007 at 01:15 AM.. |
|
||||
|
Quote:
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 |
|
||||
|
Quote:
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 |
|
||||
|
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?
|
![]() |
| Bookmarks |
| Tags |
| grep, grep or, regex |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|