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.
10-19-2009, 04:27 PM
#1
Member
Registered: Jul 2009
Location: Slovakia
Posts: 98
Rep:
extended regular expressions
Hi all how can I write regexp for matching all lines of this file ?
Code:
marek@cepi:~$ cat vystup
Hakin9.01.2009.pdf
Hakin9.02.2009.pdf
Hakin9.03.2009.pdf
Hakin9.04.2009.pdf
Hakin9.05.2009.pdf
hakin9.06.2009.pdf
hakin9.07-08.2009.pdf
I tried:
Code:
grep -oE "Hakin9.([0-9][0-9])|([0-9][0-9]-[0-9][0-9]).2009.pdf" vystup
Hakin9.01
Hakin9.02
Hakin9.03
Hakin9.04
Hakin9.05
07-08.2009.pdf
Code:
grep -oE "Hakin9.(([0-9][0-9])|([0-9][0-9]-0-9]0-9])).2009.pdf" vystup
Hakin9.01.2009.pdf
Hakin9.02.2009.pdf
Hakin9.03.2009.pdf
Hakin9.04.2009.pdf
Hakin9.05.2009.pdf
but as you can see did not gives me what I expect.
also I want know if I am capable using extended regular expressions with SED (in man there is "-r" option, I tried it but then noticed I have wrong regexps)
Thanks a lot
10-19-2009, 04:47 PM
#2
Senior Member
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 4,397
If I'm not mistaken simple:
Code:
grep "[hH]akin9.*pdf" vystup
will do.
You should be able to make use of ERE with the -r flag.
edit: Can you post your SED example that you've got problems with?
Last edited by sycamorex; 10-19-2009 at 04:48 PM .
10-19-2009, 04:47 PM
#3
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: back to Arch
Posts: 16,664
For starters, your file has both "Hakin" and "hakin", so you need the first part of the regex to be like this:
[Hh]akin9
No time at the moment to dig further.
10-19-2009, 05:07 PM
#4
Senior Member
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 2,767
Hi wakatana,
Code:
egrep '[Hh]akin09\.0[0-7](\.|-0[0-8]\.)2009' vystup
works for me.
Markus
10-20-2009, 04:48 AM
#5
Member
Registered: Jul 2009
Location: Slovakia
Posts: 98
Original Poster
Rep:
Hi guys thanks for yours reply, I used wrong expression, missing '[' before '0' in
Code:
grep -oE "Hakin9.(([0-9][0-9])|([0-9][0-9]-0-9]0-9])).2009.pdf" vystup
and also [Hh] for upper or lowercase of course.
so it should be
Code:
grep -oE "[Hh]akin9.(([0-9][0-9])|([0-9][0-9]-[0-9][0-9])).2009.pdf" vstup
sorry I typed in very late night for me
, thank you
10-20-2009, 05:20 AM
#6
Member
Registered: Jul 2009
Location: Slovakia
Posts: 98
Original Poster
Rep:
I have one question about sed
Code:
cat zoznam
Hakin9.01.2009.pdf
Hakin9.02.2009.pdf
Hakin9.03.2009.pdf
Hakin9.04.2009.pdf
Hakin9.05.2009.pdf
hakin9.06.2009.pdf
hakin9.07-08.2009.pdf
Desired output
Code:
2009.01.pdf
...
2009.07-08.pdf
so I typed:
Code:
sed -rn 's/[Hh]akin9.\((([0-9][0-9])|([0-9][0-9]-[0-9][0-9]))\).\(2009\).pdf/\2.\1.pdf/gp' zoznam
but gives me nothing
Seems like regexp memory did not works, because following command gives that:
Code:
sed -rn 's/[Hh]akin9.(([0-9][0-9])|([0-9][0-9]-[0-9][0-9])).2009.pdf/pdf/gp' zoznam
pdf
pdf
pdf
pdf
pdf
pdf
pdf
what I am doing wrong ?
Thanks a lot
10-20-2009, 05:45 AM
#7
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: back to Arch
Posts: 16,664
If you are using extended regexes (-r flag), then you don't escape the parentheses when using a backreference.
e.g.:
sed -r 's/j(um)p/\1/'
matches "jump", and replaces it with "um"
10-20-2009, 06:03 AM
#8
Senior Member
Registered: Aug 2006
Posts: 2,695
Quote:
Originally Posted by
wakatana
I have one question about sed
Code:
cat zoznam
Hakin9.01.2009.pdf
Hakin9.02.2009.pdf
Hakin9.03.2009.pdf
Hakin9.04.2009.pdf
Hakin9.05.2009.pdf
hakin9.06.2009.pdf
hakin9.07-08.2009.pdf
Desired output
Code:
2009.01.pdf
...
2009.07-08.pdf
so I typed:
[CODE]
Code:
#!/bin/bash
while IFS="." read -r -a arr
do
a="${#arr[*]}"
unset arr[$a-2]
s="${arr[@]}"
s="${s// /.}"
echo $s
done < "file"
output
Code:
$ more file
Hakin9.01.2009.pdf
Hakin9.02.2009.pdf
Hakin9.03.2009.pdf
Hakin9.04.2009.pdf
Hakin9.05.2009.pdf
hakin9.06.2009.pdf
hakin9.07-08.2009.pdf
$ ./shell.sh
Hakin9.01.pdf
Hakin9.02.pdf
Hakin9.03.pdf
Hakin9.04.pdf
Hakin9.05.pdf
hakin9.06.pdf
10-20-2009, 02:31 PM
#9
Member
Registered: Jul 2009
Location: Slovakia
Posts: 98
Original Poster
Rep:
ghostdog74: thank you for your post but I have no idea how you did it, I have to study bash a little more
pixellany: I did not knew about that with extended regular exprssions, this is what i was looking foor, thank you
Code:
sed -rn 's/[Hh]akin9.(([0-9][0-9])|([0-9][0-9]-[0-9][0-9])).(2009).pdf/\4.\1.pdf/gp' subor
2009.01.pdf
2009.02.pdf
2009.03.pdf
2009.04.pdf
2009.05.pdf
2009.06.pdf
2009.07-08.pdf
10-20-2009, 07:21 PM
#10
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: back to Arch
Posts: 16,664
Glad to see things are working....
I don't know if I will ever figure out how to remember what works in extended regexes, and in which utilities.....I always have to test it.
10-21-2009, 01:10 AM
#11
Member
Registered: Jul 2009
Location: Slovakia
Posts: 98
Original Poster
Rep:
Heh
so am I, some cheat sheet would be nice, I have found one is in czech language but if you are familiar with regexp I think it should not be problem.
http://i.iinfo.cz/r/old/data/tabulka.pdf
10-21-2009, 08:46 PM
#12
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.2, Centos 5.8
Posts: 11,740
10-22-2009, 01:27 AM
#13
Senior Member
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 2,767
Hello together,
Quote:
Originally Posted by
chrism01
yes, it is. The book of Jeffrey E. F. Friedl is very exciting and it helps at once. While reading it I found me using more elaborate regular expressions and much of my scripting became easier and more efficient.
Markus
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 06:52 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 .
Latest Threads
LQ News