Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
01-07-2008, 12:55 PM
|
#1
|
LQ Newbie
Registered: Jan 2008
Location: Austin, TX
Distribution: Suse
Posts: 6
Rep:
|
using sed to parse dir output
Hello Linux Professionals:
I am trying to parse the output of a windows dir command so it looks like to the below 'After' statement. I just to remove the extra stuff even the recursive directories.
Before:
Volume in drive \\Scandocs_vs\scandocs is SCANDOCS
Volume Serial Number is C0A8-579C
Directory of \\Scandocs_vs\scandocs\archives_webfiles\arcmaps\pdfs
03/04/2004 12:39p <DIR> .
03/04/2004 12:39p <DIR> ..
03/19/2004 01:15p 24,364,073 10315.pdf
After:
03/19/2004 01:15p 24,364,073 10315.pdf
Any help appreciated!
Keith
|
|
|
01-07-2008, 01:09 PM
|
#2
|
Member
Registered: Jun 2004
Posts: 307
Rep:
|
Remove the first 7 lines (?).
dir | sed '1,7d'
Just a stab in the dark...
|
|
|
01-07-2008, 02:42 PM
|
#3
|
LQ Newbie
Registered: Jan 2008
Location: Austin, TX
Distribution: Suse
Posts: 6
Original Poster
Rep:
|
I think that would work if I did not have to dir recursively. [dir /s]
I was thinking that if I could remove all lines that did not match '.pdf' in the string it would work.
-Keith
********************
03/19/2004 01:15p 24,364,073 10315.pdf (keep)
Directory of \\Scandocs_vs\scandocs\archives_webfiles\arcmaps\pdfs (discard)
********************
|
|
|
01-07-2008, 02:44 PM
|
#4
|
Senior Member
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,181
Rep:
|
Why don't you just use grep? You can search for ".pdf" and only include those lines that have a .pdf file on them (if so named). There are a variety of ways to do this, all equally valid, but I leave their discovery as an exercise for the reader.
|
|
|
01-08-2008, 09:10 AM
|
#5
|
LQ Newbie
Registered: Jan 2008
Location: Austin, TX
Distribution: Suse
Posts: 6
Original Poster
Rep:
|
grep did it.
I was using it like this: (cygwin by the way)
c:> grep -i '.pdf' dir_pdfs
But the result was not returning what I expected (directories were still listed) so I was luckily able to modify the statement to:
c:> grep -i '[0-9].pdf' dir_pdfs
and it returned the results I wanted. So problem solved!
As a matter of curiosity, do you know why it did not seem to apply the '.' in the string example.
Thanks,
Keith
|
|
|
01-08-2008, 01:35 PM
|
#6
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
The dot '.' as a special meaning in a regular expression: it matches any single character, not just the dot itself. When you use a dot or any other special character in the pattern, grep interprets it as a regular expression and you can obtain an unexpected result.
On the other hand, to match a dot literally you have to enclose it in square brackets, e.g
Code:
grep [.]pdf dir_pdfs
Last edited by colucix; 01-08-2008 at 01:37 PM.
|
|
|
01-08-2008, 11:32 PM
|
#7
|
LQ Newbie
Registered: Jan 2008
Location: Austin, TX
Distribution: Suse
Posts: 6
Original Poster
Rep:
|
That explains it. Thanks much!
|
|
|
01-08-2008, 11:45 PM
|
#8
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
Quote:
Originally Posted by colucix
The dot '.' as a special meaning in a regular expression: it matches any single character, not just the dot itself. When you use a dot or any other special character in the pattern, grep interprets it as a regular expression and you can obtain an unexpected result.
On the other hand, to match a dot literally you have to enclose it in square brackets, e.g
Code:
grep [.]pdf dir_pdfs
|
Every day I learn that you learn something new every day!!
I had learned that the "normal" way to change the meaning of certain characters was the "escape"---as in:
grep "\." filename
The square bracket I never saw before---does it also work in SED?
Yes...
|
|
|
01-09-2008, 12:09 AM
|
#9
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Quote:
Originally Posted by pixellany
The square bracket I never saw before---does it also work in SED?
Yes...
|
its used in regexp to specify range or single character. eg [a-z] , [abc].
From wiki
Quote:
\[ \] A bracket expression. Matches a single character that is contained within the brackets. For example, \[abc\] matches "a", "b", or "c". \[a-z\] specifies a range which matches any lowercase letter from "a" to "z". These forms can be mixed: \[abcx-z\] matches "a", "b", "c", "x", "y", and "z", as does \[a-cx-z\].
The - character is treated as a literal character if it is the last or the first character within the brackets, or if it is escaped with a backslash: \[abc-\], \[-abc\], or \[a\-bc\].
|
If the sed you are using supports this syntax, then yes, it can be used in sed.
|
|
|
01-09-2008, 06:58 AM
|
#10
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
Light goes on....
I knew bracket expressions, but had never considered that a "special" character would cease to be special inside one. The books typically don't talk about the use of brackets in lieu of escaping---but it obviously works.
So, is there a way to pass in as a variable the string to go inside [ ]?
|
|
|
01-09-2008, 07:49 AM
|
#11
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by pixellany
The books typically don't talk about the use of brackets in lieu of escaping---but it obviously works.
|
Yes... not really used as an escape, but as a way to match single characters, as ghostdog reported. Anyway, very useful for "escaping" in some cases!
Quote:
So, is there a way to pass in as a variable the string to go inside [ ]?
|
I think this can be done in the common way. For example consider a text file with these two lines
Code:
$ cat testfile
line with a dot . inside
line with a dot at the end.
You can do
Code:
$ my_var=.$
$ grep [$my_var] testfile
line with a dot . inside
line with a dot at the end.
whereas if you want to retain the special meaning of $ you have to add it outside the brackets.
Code:
$ grep [$my_var]$ testfile
line with a dot at the end.
Cheers! 
|
|
|
01-10-2008, 07:37 AM
|
#12
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
OK---special meaning as you use it means "at the end of the line". But, inside the [ ], the "$" clearly has its more general special meaning--i.e. "the value of". so you would have to use [\$] to look for a literal "$".
What other characters are special by default inside of [ ]? e.g. "r[^ab]" means "r, not followed by a or b".
|
|
|
01-10-2008, 01:17 PM
|
#13
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
CORRECT. Except when you put $ at the end of the character list, that is if it's not followed by any other character it cannot expand any variable. How many nuances the shell has!!
|
|
|
All times are GMT -5. The time now is 10:07 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
|
|