LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 10-19-2009, 04:27 PM   #1
wakatana
Member
 
Registered: Jul 2009
Location: Slovakia
Posts: 141

Rep: Reputation: 16
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
 
Old 10-19-2009, 04:47 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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.
 
Old 10-19-2009, 04:47 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
 
Old 10-19-2009, 05:07 PM   #4
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hi wakatana,

Code:
egrep '[Hh]akin09\.0[0-7](\.|-0[0-8]\.)2009' vystup
works for me.

Markus
 
Old 10-20-2009, 04:48 AM   #5
wakatana
Member
 
Registered: Jul 2009
Location: Slovakia
Posts: 141

Original Poster
Rep: Reputation: 16
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
 
Old 10-20-2009, 05:20 AM   #6
wakatana
Member
 
Registered: Jul 2009
Location: Slovakia
Posts: 141

Original Poster
Rep: Reputation: 16
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
 
Old 10-20-2009, 05:45 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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"
 
Old 10-20-2009, 06:03 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by wakatana View Post
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
 
Old 10-20-2009, 02:31 PM   #9
wakatana
Member
 
Registered: Jul 2009
Location: Slovakia
Posts: 141

Original Poster
Rep: Reputation: 16
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
 
Old 10-20-2009, 07:21 PM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
 
Old 10-21-2009, 01:10 AM   #11
wakatana
Member
 
Registered: Jul 2009
Location: Slovakia
Posts: 141

Original Poster
Rep: Reputation: 16
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
 
Old 10-21-2009, 08:46 PM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
The best book: http://regex.info/
 
Old 10-22-2009, 01:27 AM   #13
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hello together,

Quote:
Originally Posted by chrism01 View Post
The best book: http://regex.info/
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
 
  


Reply

Tags
expressions, grep, regexp, regular, sed



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expressions ziggy25 Linux - Newbie 7 11-05-2007 06:57 AM
Regular Expressions markjuggles Programming 2 05-05-2005 11:39 AM
Regular Expressions overbored Linux - Software 3 06-24-2004 02:34 PM
help with REGULAR EXPRESSIONS ner Linux - General 23 10-31-2003 11:09 PM
regular expressions? alaios Linux - General 2 06-11-2003 03:51 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration