Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-22-2008, 04:12 PM
|
#1
|
|
LQ Newbie
Registered: Jul 2008
Posts: 7
Rep:
|
Sed: how to delete all lines except those matching........
Hello Everyone,
I'm hoping i can get some assistance using Sed to do the following.;
"Remove all lines that do NOT contain a period followed by 3 characters (either numbers or letters or spaces...)Basicly, trying to cleanup a windows directory listing so that only files ending with .*** are
left.
I have been able to successfully delete all lines that do NOT contain
a period with the following syntax,
sed '/\./'!d
but I'm unable to insert the necessary wildcards to keep lines that
contain periods immediately followed by three characters..
Thanks for any help you can provide,
Regards,
Last edited by DX398; 07-22-2008 at 04:38 PM.
|
|
|
|
07-22-2008, 04:20 PM
|
#2
|
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
You can use the -n option to suppress printing unless explicitly done with the 'p' command, and then make a rule which matches the desired line and upon matching calls 'p'.
|
|
|
|
07-22-2008, 04:36 PM
|
#3
|
|
LQ Newbie
Registered: Jul 2008
Posts: 7
Original Poster
Rep:
|
Thanks for the quick response Matthew.. Yes that is another option but what I'm confused about is what the rule should look like. The only known character is the period. The 3 trailing characters can be anything...
For example;
\directory\subdir\file1.txt - Want to keep this line
\directory\subdir\file1.xls - Want to keep this line
\directory\subdir\subdir.1 - Want to delete this line.
\directory\subdir\subdir - Want to delete this line.
Last edited by DX398; 07-22-2008 at 04:44 PM.
|
|
|
|
07-22-2008, 04:58 PM
|
#4
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,899
|
Following the suggestion by matthewg42 you are near to the solution. To build the regular expression you are looking for, take in mind the special meaning of . (dot): it matches any single character. Three consecutive dots match any sequence of three characters.
|
|
|
|
07-22-2008, 05:09 PM
|
#5
|
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 11,288
|
What about "\directory\subdir\file1.his.txtt" - gotta watch out for "corner" cases.
Ensure the data you are checking is appropriate - say, only at the end of the input.
|
|
|
|
07-22-2008, 10:27 PM
|
#6
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
sed -n '/\....$/p' filename > newfilename
literal "." + any 3 characters--all at the end of the line
|
|
|
|
07-22-2008, 10:31 PM
|
#7
|
|
LQ Newbie
Registered: Jul 2008
Posts: 7
Original Poster
Rep:
|
Quote:
Originally Posted by colucix
Following the suggestion by matthewg42 you are near to the solution. To build the regular expression you are looking for, take in mind the special meaning of . (dot): it matches any single character. Three consecutive dots match any sequence of three characters.
|
Hi Colucix.. I didn't know that about the dot. Thanks, but in my case
I had to escape it with a backslash to get the literal dot... how do you "unescape" and add the remaining dots.
Regards,
|
|
|
|
07-22-2008, 10:34 PM
|
#8
|
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 11,288
|
Bad, bad pix- we were trying to get the OP to answer his own q.
As pix showed, the escape is for only the one character.
|
|
|
|
07-22-2008, 10:44 PM
|
#9
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
Quote:
Originally Posted by syg00
Bad, bad pix- we were trying to get the OP to answer his own q.
As pix showed, the escape is for only the one character.
|
Well, I'm sorry. My homework detector was silent, and it seemed that OP was a real person--one who might go on to learn greater things.
I'll be better next time......unless I'm not.. 
|
|
|
|
07-23-2008, 02:39 AM
|
#10
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,899
|
Quote:
Originally Posted by DX398
Hi Colucix.. I didn't know that about the dot. Thanks, but in my case I had to escape it with a backslash to get the literal dot... how do you "unescape" and add the remaining dots.
|
Just leave them unescaped! You want to match a sequence of 4 characters: a literal dot followed by three other characters and by an end of line (as syg00 pointed out). You have to build a regular expression with 4 + 1 items: a literal (escaped) dot immediately followed by three (unescaped) dots plus the special character which means "end-of-line". Pixellany already posted the solution (just a final twist of the knife...  ).
Also note that another method to match literal characters is to embed them in a "character list" using square brackets. So the regular expression could be written
A character list can be used to match any of the listed characters. For example [agk] match any single occurrence of a, g or k. You can also use "intervals" like [0-5] to match any single digit from 0 to 5, or even [a-zA-Z] to match one single alphabetic character, be it upper- or lower-case. Hope this helps.
|
|
|
|
| Thread Tools |
Search 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
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:17 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|