0

I have a huge text file with contents similar to this:

<!-- $var = aaa -->
<!-- $var = aaa -->
<!-- $var = aaa -->
<!-- $var = aaa -->
<!-- $var = aaa -->
.
<!-- $var = bbb -->
<!-- $var = bbb -->
<!-- $var = bbb -->
<!-- $var = bbb -->
<!-- $var = bbb -->

I want to achieve that the $var will be replaced like this:

<!-- $address = aaa -->
<!-- $city    = aaa -->
<!-- $zip     = aaa -->
<!-- $phone   = aaa -->
<!-- $geo     = aaa -->
.
.
<!-- $address = bbb -->
<!-- $city    = bbb -->
<!-- $zip     = bbb -->
<!-- $phone   = bbb -->
<!-- $geo     = bbb -->

The sequence is always the same. I have researched for about 3 hours, but I can't get past this brain hurdle. My idea was to realize this with regex, but it seems I need a script to wrestle with this.

Can you give me a hint into which direction to go and is this possible with Regex at all? I'm a beginner, so please be gentle :)

Kind regards Oliver

3 Answers 3

1

You could write a regular expression to do this in one foul swoop, but it would be way easier to use something like sed, which will target lines individually.

#!/bin/sh
sed '
  s/\$var/\$address/  # replace $var with $address
  N                   # next line
  s/\$var/\$city   /  # replace $var with $city
  N                   # next line
  s/\$var/\$zip    /  # replace $var with $zip
  N                   # next line
  s/\$var/\$phone  /  # replace $var with $phone
  N                   # next line
  s/\$var/\$geo    /  # replace $var with $geo
' $1

You can then run this script against your file.

Sign up to request clarification or add additional context in comments.

4 Comments

Ooh. I need to try that out. Sed is very tempting!
Thanks. Sed is very powerful and it solved this problem. Additionally I can reuse the code for more files that need processing. Thanks a lot!
Is there a simple way to loop this script until the end of the file is reached?
It should do that automatically: mine did, when I tested this. Is yours only targeting the first five lines? I'm not a sed expert, so I'm afraid I don't know: Take a look at the documentation, or ask another SO question, tagged with sed, so someone who knows it better can respond... Sorry I can't be more help :P
1

You need to handle one row at a time, applying a different regexp depending on which row it is, as in (I do not know which language you need so see this as pseudocode, and it can be optimized if needed)

var replacements = new[]{"address","city","zip","phone","geo"};
var replacement = replacements[row % 5];
var r = new Regex("(^<!-- \$)var(.*$)";
var newline = r.Replace(oldline, "$1"+replacement+"$2");

1 Comment

Thanks for that. I'm sure I can use it again sometime!
1

This should do it. I have tested this using Progammer's Notepad. If you are using something different then you may need to tweek it.

Find:
(<!-- \$var = (\w+) -->\r\n){5}

Replace:
<!-- $address = \2 -->\r\n<!-- $city    = \2 -->\r\n<!-- $zip     = \2 -->\r\n<!-- $phone   = \2 -->\r\n<!-- $geo     = \2 -->\r\n

The key to understanding this is the \2. This references the second group found by the regex. A group is something trapped in parenthsis ().

\0 would match the entire string.
\1 would match the first set of backets. (<!-- \$var = (\w+) -->\r\n)
\2 would match the second set of backets. (\w+) This is your aaa or bbb

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.