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.
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.
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.
Actually, i don't need it for work task i just wonder how to make this kind of regular expression.
I thought it should be pretty easy but stuck unexpectedly.
Then samples of your data would be useful, as syg00 points out.
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.
I've no problems with first and last, i use ^ and $ operators respectively. But when i try to point to second symbol or third here is the problem.
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.
Last edited by asherbarasher; 05-19-2013 at 12:11 PM.
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.
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.
This should work definitely as you want
with grep:
Code:
ls |egrep '^..z.*$'
with sed:
Code:
ls |sed -n '/^..z.*$/p'
Last edited by divyashree; 05-19-2013 at 01:31 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.