Linux - NewbieThis 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.
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.
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
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
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.
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
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
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?
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.
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 [ ]?
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".
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!!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.