views:

81

answers:

4

Looking at this:

MENU_ITEM_BACK#0="Back";
MENU_ITEM_BLOCK_CHANNEL#0="Block";
MENU_ITEM_CLOSE#0="Close";
MENU_ITEM_DETAILS#0="Details";
MENU_ITEM_DIAGNOSE#0="Diagnose";
MENU_ITEM_DOWNLOAD#0="Download";

...and so on (over 500 lines). What would be the best way to automate copying the label name into the label itself. For example,

MENU_ITEM_BACK#0="Back";

would become

MENU_ITEM_BACK#0="MENU_ITEM_BACK";

I am most familiar with Java but have no objections to any other programming languages, although I am not very familiar with regular expressions (coded ones at least).

EDIT: It have come to my attention that the '#0=' are not always constant. Sometimes they can be replaced by a very arbitrary number such as '#6548135='. The answers given were good but since I am not familiar with regular expressions, how would you accommodate this? Note that I do not want to copy the number, just the label.

+2  A: 

I think a regular expression is a good choice, assuming the label can't have any # characters. Simply replace

^([^#]+#0)=".*";$

with

 \1="\1";

If you want to do this in a program or script, you need to use your language of choice's regex facilities.

If you want to do this for just once for this particular file, you can do it with sed:

$ sed -e "s/^([^#]+#0)=".*";$/\1=\"\1\"/" file
Martinho Fernandes
+1  A: 

A regular expression, e.g. using sed:

sed -r 's/^([^#]+)([^=]+)=.*$/\1\2="\1"/' yourfile.txt

The same thing in Python:

import sys
import re

for line in sys.stdin.readlines():
    print re.sub('^([^#]+)([^=]+)=.*$', r'\1\2="\1"', line).rstrip()

Usage:

python program.py < yourfile.txt
Mark Byers
+1  A: 

Use a regular expression search and replace, such as with Perl, sed, awk or as supported in many recent text editors.

In Perl, this would do the trick:

my $text = <<END_YOUR_TEXT;
PUT YOUR TEXT HERE
END_YOUR_TEXT
$text =~ s/(MENU_ITEM_[^#]+)#0="[^"]+";/$1#0="$1";/g
print $text;

The "$1" is a reference to the parenthesized match, e.g. MENU_ITEM_BACK.

To read the text from a file in the command line, slurp it in:

my $text = <>;

Paul Chernoch
+1  A: 

You can do this without regular expressions using Microsoft Word. Copy the section of the code into Word. Then use a column selection (hold the alt key down and select with the mouse), then copy it to the right, and clean up with search and replace.

xpda
How will a column selection work with columns of different sizes?
Martinho Fernandes
Here's one way: Add a space to the right of each row. In the top row, add enough spaces so it's the longest line. Select a column of all the lines, wide enough to include each name and trailing space. Paste the column far enough to the right so it doesn't overlap any text. The spaces on the first line will allow this. It will work, even though the rows are different length.
xpda
I'm an idiot. Took 2 minutes in excel.
Jay