regex challenge


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting regex challenge
# 1  
Old 10-01-2008
regex challenge

Here's a regex substitution operation that has stumped me with sed:

How do you convert lines like this:

Code:
first.key ?{x.y.z}
second.key ?{xa.ys.zz.s}
third.key ?{xa.k}

to:

Code:
first.key ?{x_y_z}
second.key ?{xa_ys_zz_s}
third.key ?{xa_k}


So i'm basically converting all the periods within the ?{...} structure to underscores, without changing the periods outside of this structure?
# 2  
Old 10-01-2008
if u don't mind, i can try this in awk instead of sed
Code:
awk -v FS='[}{]' '{gsub(/\./,"_",$2); print $1"{"$2"}"}' /path/of/file

# 3  
Old 10-01-2008
Code:
$ cat data
first.key ?{x.y.z}
second.key ?{xa.ys.zz.s}
third.key ?{xa.k}
$ sed -e :a -e's/\([^{][^{]*{[^.][^.]*\)\./\1_/;ta' < data
first.key ?{x_y_z}
second.key ?{xa_ys_zz_s}
third.key ?{xa_k}
$
$

This can probably be simplified; however, obvious simplifications did not seem to work. Smilie It's trickier than it looks.
# 4  
Old 10-01-2008
Quote:
Originally Posted by Perderabo
Code:
$ cat data
first.key ?{x.y.z}
second.key ?{xa.ys.zz.s}
third.key ?{xa.k}
$ sed -e :a -e's/\([^{][^{]*{[^.][^.]*\)\./\1_/;ta' < data
first.key ?{x_y_z}
second.key ?{xa_ys_zz_s}
third.key ?{xa_k}
$
$

This can probably be simplified; however, obvious simplifications did not seem to work. Smilie It's trickier than it looks.
Code:
sed -e :a -e's/\([^{]\+{[^.]\+\)\./\1_/;ta' < data

This simplification worked for me.... interesting, I did not know about the "t" operator.
# 5  
Old 10-01-2008
Quote:
Originally Posted by gnsxhj
if u don't mind, i can try this in awk instead of sed
Code:
awk -v FS='[}{]' '{gsub(/\./,"_",$2); print $1"{"$2"}"}' /path/of/file

Sweet thinking!
# 6  
Old 10-01-2008
Thanks Perderabo and gnsxhj... I'm still, however, interested in a pure (or purer?) regex solution.
# 7  
Old 10-02-2008
Code:
$ cat file
first.key ?{x.y.z}
second.key ?{xa.ys.zz.s}
third.key ?{xa.k}
$ perl -pe's/([^.]+)\.(?!\w+\s)/$1_/g' file
first.key ?{x_y_z}
second.key ?{xa_ys_zz_s}
third.key ?{xa_k}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

2. Shell Programming and Scripting

Perl, RegEx - Help me to understand the regex!

I am not a big expert in regex and have just little understanding of that language. Could you help me to understand the regular Perl expression: ^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{ ------ This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Discussion started by: alex_5161
2 Replies

3. Shell Programming and Scripting

Anyone like a challenge?

I have searched through google, and this forum to try and find the answer, but alas, nothing quite hits the whole answer. I am trying to read the last line (or lines) of some log files. I do this often. The files are named sequentially, using the date as part of the file name, and appending... (18 Replies)
Discussion started by: BatterBits
18 Replies

4. UNIX for Dummies Questions & Answers

Touch Challenge

I've been given a directory full of subdirectories full of logfiles of the same name: /logfiles/day1/file1/blockednodes.csv day1-14 file1-48 The above is the actual directory structure for 14 days worth of a logfile that is generated every 30 minutes. It's been done this way to preserve the... (15 Replies)
Discussion started by: Cludgie
15 Replies

5. AIX

openssh connectivity challenge

Running a Power 5 Blade on AIX, with remote connectivity issues via putty. AIX V 6.1.00-02 openssh V5.2.0.5300 openssl V0.9.8.1103 Intermittent remote connections. Seems to connect every other time I try via my putty client. Using hosts.allow and hosts.deny to filter IP Addrss... (2 Replies)
Discussion started by: FrankM
2 Replies

6. Shell Programming and Scripting

PS1 challenge

Ok then i Have a challenge for you : Give me PS1 so that it always display the least 2 levels of directory (except if i am above of course) I want it this way : so if i go to / /home/ /home/user /home/user/whatever /home/user/whatever1/whatever2 my PS1 should respectively... (12 Replies)
Discussion started by: ctsgnb
12 Replies

7. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

8. Shell Programming and Scripting

AWK Challenge

I have the following text Microsoft iSCSI Initiator version 2.0 Build 3497 Targets List: iqn.2001-05.com.equallogic:0-8a0906-daef43402-138000002a4477ba-grsrv12-extra iqn.2001-05.com.equallogic:0-8a0906-986f43402-520000002b447951-exchange ... (9 Replies)
Discussion started by: netmedic
9 Replies

9. UNIX for Advanced & Expert Users

safeword challenge

Hi, there are some servers here at work which issue a Safeword challenge after I login. Can anyone tell me exactly how the challenge/response system works? In particular, how are the valid keys decided? (2 Replies)
Discussion started by: blowtorch
2 Replies

10. UNIX for Advanced & Expert Users

X25 Address - A challenge...

Okay - I've been searching near and far for the answer to this seemly simple question..... how do I find the X25 address for a server. Is there some sort of dump or ping or even a config which would tell me the address. I can find nothing on the web and my colleagues can't help either. (1 Reply)
Discussion started by: peter.herlihy
1 Replies
Login or Register to Ask a Question