simple regex question
hi everyone.
i am very new to linux, so i think this is the best forum to post such a question. I'm trying to construct regex to work with sed, i need it to match words with every third character z. i don't understand why this doesn't work: ls | sed -n '/..z*$/p' Dot should stand for any single character, but this match just everything. Also if you can point me to any good tutorial for beginners in regex it will be very helpful. THank you in advance. |
Start here http://www.grymoire.com/Unix/Sed.html#uh-0.
Watch that use of '*', it matches anything. |
Quote:
Code:
ls | sed -n '/..z.*$/p' |
Most (all ?) regex questions are simple. It's the resolution that gets complex ... :eek:
|
Quote:
Quote:
|
In which case you should provide such so we can evaluate what is happening based on your data.
I was about to comment that last I looked the grymoire site was somewhat dated, but I see a recent attribution. Goodness. |
Quote:
I thought it should be pretty easy but stuck unexpectedly. :) |
Quote:
The regex actually works, in my system it lists .tar.gz, .zip, and a couple of files ending in 'z', in a directory containing several types of files. |
You shouldn't be parsing ls for filenames anyway. For simple name pattern matching you can almost always use simple globbing patterns.
Code:
printf '%s\n' ??z* |
Quote:
Here's example: [root@lab2 bin]# cd /usr/bin [root@lab2 bin]# ls | sed -n '/..z.*$/p' abrt-action-analyze-backtrace abrt-action-analyze-c abrt-action-analyze-core abrt-action-analyze-oops abrt-action-analyze-python bluetooth-wizard bunzip2 compiz compiz-gtk egroupwarewizard eu-size funzip gettextize gpg-zip groupwarewizard groupwisewizard gunzip hg-viz htfuzzy --omitted-- ***************** And same with grep. As you can see, it lists everything in the directory. My question is how can i filter output, based on definition of every second or third or whatever character. |
It's simple. You use shell globbing, as I said before. or find.
Tools like grep/sed/awk are designed for text processing, not filename matching. Do not try to filter the output of ls for names or metadata. One thing to remember about regex, by the way, is that it's unanchored by default. You do not need to use ^/$ unless you specifically need the match those positions exactly, and you don't need to give any more than is necessary to uniquely match the string. (e.g. '^..z' will return any string with 'z' as the third character.) Globbing is more limited though, in that the pattern must match the entire string, usually with the use of "*" wildcards. |
Quote:
with grep: Code:
ls |egrep '^..z.*$' Code:
ls |sed -n '/^..z.*$/p' |
Quote:
Code:
$ ls | sed -n '/^..z/p' Code:
bzz |
I can't help with giving you the regex, but for a reference, try Regular-Expressions.info .
|
This is the book on regex http://regex.info/book.html :)
|
All times are GMT -5. The time now is 08:15 AM. |