Siebird
Siebird

Reputation: 638

Regex find & replace all spaces with underscore between tags

I'm trying to format an xml document before I import it into a database. I have several hundred rows with <Image #1 File Name>Random string to find and replace</Image #1 File Name> I have no clue for the life of me on the correct find & replace pattern in regex to do what I need. I was able to match all instances with this:

<Image #1 File Name>(.*?)</Image #1 File Name>

But I'm unsure on the pattern I need to then replace the whitespace with underscores between the open/close tags <Image #1 File Name>Random_string_to_find_and_replace</Image #1 File Name>

Upvotes: 2

Views: 1392

Answers (2)

aanton
aanton

Reputation: 5572

I suggest the next trick:

  1. Replace <Image #1 File Name> and </Image #1 File Name> with two differentspecial words without spaces (example MY_START_TAG and MY_END_TAG).
  2. Replace all spaces with underscores.
  3. Revert the first step.

Divide and rule :)

Upvotes: 0

Stephane Rouberol
Stephane Rouberol

Reputation: 4384

awk -F'<|>' '{gsub(/ /,"_",$3);print"<"$2">"$3"<"$4">"}' yourxmlfile

OK, you can then check with ~ match operator, before filtering:

awk -F'<|>' '{if ($0 ~ /Image #1 File Name/) {gsub(/ /,"_",$3);print"<"$2">"$3"<"$4">"} else {print;}}' yourxmlfile

Upvotes: 1

Related Questions