Help with Regexp replace in vim/sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with Regexp replace in vim/sed
# 1  
Old 01-16-2011
Question Help with Regexp replace in vim/sed

Hi!

I have a file with multiple lines following this format:
Code:
<a href="xxx.aaa_bbb_ccc.yyy">xxx.aaa_bbb_ccc.yyy</a>

The goal is to replace the title (not modifying the href) so the new lines looks like this:
Code:
<a href="xxx.aaa_bbb_ccc.yyy">Aaa bbb ccc</a>

The number of underscores in the "aaa_bbb_ccc" part is unknown, and that's where I'm stuck.
So far I've managed to get this result:
Code:
<a href="xxx.aaa_bbb_ccc.yyy">Aaa_bbb_ccc</a>

using this regexp:
Code:
s/>[^.]*\.\([^.]*\)[^<]*/>\u\1/

Though one-liners are always welcome Smilie, the solution may contain multiple commands; but no matter how I try, I can't wrap my head around is how to replace the underscores with spaces, without also replacing the underscores in the href-part of the link. Is it even possible (with regexp)?

Can sed/vim execute a substitution in only part of the line (i.e. between '>' and '<'), or can I somehow run a 's/_/ /g' on the backreference '\1' before it is substituted?

There doesn't seem to be that many places where you can find information about advanced regexp substitutions (most places focus on the matching, not the replacing).
So if anyone could shed some light on this, it would be greatly appreciated.

Thanks in advance,
Eric
# 2  
Old 01-16-2011
If Perl is an option, then :

Code:
$
$
$ cat f2
<a href="xxx.aaa_bbb_ccc.yyy">xxx.aaa_bbb_ccc.yyy</a>
<a href="xxx.aaa_bbb_ccc.yyy">xxx.aaa_bbb_ccc.yyy</a>
<a href="xxx.aaa_bbb_ccc.yyy">xxx.aaa_bbb_ccc.yyy</a>
<a href="xxx.aaa_bbb_ccc.yyy">xxx.aaa_bbb_ccc.yyy</a>
<a href="xxx.aaa_bbb_ccc.yyy">xxx.aaa_bbb_ccc.yyy</a>
$
$
$ perl -lne '($x,$y,$z)=/^(.*">)xxx.(.*).yyy(.*)$/; $y=~s/_/ /g; $y=ucfirst $y; print "$x$y$z"' f2
<a href="xxx.aaa_bbb_ccc.yyy">Aaa bbb ccc</a>
<a href="xxx.aaa_bbb_ccc.yyy">Aaa bbb ccc</a>
<a href="xxx.aaa_bbb_ccc.yyy">Aaa bbb ccc</a>
<a href="xxx.aaa_bbb_ccc.yyy">Aaa bbb ccc</a>
<a href="xxx.aaa_bbb_ccc.yyy">Aaa bbb ccc</a>
$
$

tyler_durden

Last edited by durden_tyler; 01-16-2011 at 03:01 PM..
This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 01-16-2011
Thanks for the answer!

I know I might end up using Perl or some other scripting language; but for now, I'm determined to find a solution using regexp (without scripting).
Surely it has to be possible?

(I know, I'm stubborn, but I want to learn what is possible, and not always stay with what I know works.)

/Eric
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

find and Replace String in Perl - Regexp

Trying to find and replace one string with another string in a file #!/usr/bin/perl $csd_table_path = "/file.ntab"; $find_str = '--bundle_type=021'; $repl_str = '--bundle_type=021 --target=/dev/disk1s2'; if( system("/usr/bin/perl -p -i -e 's/$find_str/$repl_str/' $csd_table_path")... (2 Replies)
Discussion started by: cillmor
2 Replies

2. Shell Programming and Scripting

Need help with sed and regexp

Hi everyone, I would really appreciate any help I could get on the following topic. I am not very familiar with reg expressions nor with sed, I just know the basic uses. What I am trying to do is the following: I have a huge text file where I would like to replace all occurnces of a certain... (13 Replies)
Discussion started by: Boxtuna
13 Replies

3. Shell Programming and Scripting

replace awk with a perl one liner (REGEXP and FS)

hello, I want to replace awk with a perl one liner in unix. i use in awk REGEX and FS ( field separator) because awk syntaxes in different unix os versions have not the same behaviour. Awk, Nawk and GNU Awk Cheat Sheet - good coders code, great reuse i have a file named "file" and want... (5 Replies)
Discussion started by: bora99
5 Replies

4. Shell Programming and Scripting

Advanced AWK Regexp substring to int & Replace

Hi! I have a difficult problem, to step up a unknown version number in a text file, and save the file. It would be great to run script.sh and the version gets increased. Example the content of the textfile.txt hello version = x bye This include three steps 1. First find the char after... (2 Replies)
Discussion started by: Beachboy72
2 Replies

5. UNIX for Dummies Questions & Answers

sed before and after regexp

Dear all i have the code which print 1 line of context before and after regexp, with line number sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h the code work well but any one can tell me what each letter mean {=;x;1!p;g;$!N;p;D;} also how i can print 2 line before and onle line after ... (2 Replies)
Discussion started by: soly
2 Replies

6. Shell Programming and Scripting

sed regexp

Hi, I am not that good with reg exp and sed. But I was just looking at something the other day and came across a situation. When I ran the below command: echo "123 word" | sed 's/*/(&)/' the op was: (123) word But when I ran: echo "123 word" | sed 's/*/(&)/g' the o/p was: (123)... (4 Replies)
Discussion started by: King Nothing
4 Replies

7. UNIX for Dummies Questions & Answers

VIM: replace a character in the middle of line

exmaple, i need to replace the number "5" from all lines below with "X"? What is the useful vim command that i can apply for.. ddpadsgg506xghssuyj ddpadsgag546xghssuys ddsadsgaag596xghssuy_te ddsadsgag506xghssuy_pe ddsadsgagc526xghssuys ddsads506ighssuys ddsadsgag506pghssuyk (1 Reply)
Discussion started by: 793589
1 Replies

8. UNIX and Linux Applications

vi or vim replace ,$ (eol) with just a comma

I have lines in a file like this (140,000+ entries): value1, value2, value3, " " I want to concatenate the three (there are 22) lines with commas so it looks like this value1, value2, value3 " " I'm trying with :g/,$/s/,$/, /g but that is not flying. any ideas? (6 Replies)
Discussion started by: dbauhaus
6 Replies

9. Shell Programming and Scripting

Replace Entire line if any part matches regexp

Hey guys, I have a file that I've slowly been awking, seding, and greping for data entry. I am down to pull the addresses out to insert them into an excel file. Each address is a few lines, but i want to put a semicolon delimiter in between each address so I can export the text file into excel and... (6 Replies)
Discussion started by: Cocoabean
6 Replies

10. Shell Programming and Scripting

regexp with sed again!!!

please help: I want to add 1 space between string and numbers: input file: abcd12345 output file: abcd 1234 The following sed command does not work: sed 's/\(+\)\(+\)/\1 \2/' file Any ideas, please Andy (2 Replies)
Discussion started by: andy2000
2 Replies
Login or Register to Ask a Question