Eggfoot
Eggfoot

Reputation: 49

Replacing segments in multiple lines with another segment in the same file, which is in multiple folders, using find and sed

I have a the following directory structure of multiple folders:

Applications
|
|____Salford_123
|   |_________SomeFile.png
|   |_________AnotherFile.nui
|   |_________MenuSettings.txt
|   |_________Data
|             |_____Settings.txt
|
|____Salford_546
|   |_________SomeFile.png
|   |_________AnotherFile.nui
|   |_________MenuSettings.txt
|   |_________Data
|             |_____Settings.txt
|
|____Salford_789
|   |_________SomeFile.png
|   |_________AnotherFile.nui
|   |_________MenuSettings.txt
|   |_________Data
|             |_____Settings.txt

The text file MenuSettings.txt in each folder which has the following contents:

AppName: "http://www.bbc.co.uk”
DoubleWidthTile: "False"
TileColor: "0.76 0 0.24 1"
AnimationSize: "10 10"
AppName_xx_XX: “Venice Quays”
AppName_yy_YY: “Venice Quays”

This file and its parent folder is generated automatically, multiple times. The first line of the file has a different URL in it every time which could be at any length. Im trying to have the segment with the URL copied to replace the values in AppName_xx_XX:and AppName_yy_YY:so that it looks like this

AppName: "http://www.bbc.co.uk”
DoubleWidthTile: "False"
TileColor: "0.76 0 0.24 1"
AnimationSize: "10 10"
AppName_xx_XX: “http://www.bbc.co.uk”
AppName_yy_YY: “http://www.bbc.co.uk”

The entries for AppName_xx_XX: and AppName_yy_YY: would have any word generated in it at any length with spaces and punctuation. The xx and yy part of the string would be any 2 characters. So I used "find" to search recursively through the folders and a "sed" command to copy and replace the segments, which outputs to another file, which I would then mv -v back to the original.

find salford* -maxdepth 1 -type d \( ! -name . \) -exec sh -c '(cd {} && 

cat MenuSettings.txt | sed -e 's/AppName\_en\_[[A-Z]]\:[[:blank:]]\"[[:alpha:]]*.\"/AppName\:[[:space:]]\"[[:alpha:]]*.[[:alpha:]]*.[[:alpha:]]*.\"/g' > MenuSettings3.txt

)' ';'

The bash file runs with no errors but fails to work, as file it generates shows no changes. Where am i going wrong?

Upvotes: 0

Views: 111

Answers (1)

dan4thewin
dan4thewin

Reputation: 1184

$ sed '/^AppName: */{h;s///;x;}; /^\(AppName_.._..:\).*/{s//\1/;G;s/\n/ /;}' MenuSettings.txt
AppName: "http://www.bbc.co.uk”
DoubleWidthTile: "False"
TileColor: "0.76 0 0.24 1"
AnimationSize: "10 10"
AppName_xx_XX: "http://www.bbc.co.uk”
AppName_yy_YY: "http://www.bbc.co.uk”

I worked from your description to make the above sed command. I'm making use of the hold buffer to keep the URL, and for the xx/yy lines appending from the hold buffer after getting rid of the original value.

The first bracketed expression uses hold to make a copy of the line in the hold buffer. The s/// reuses the last pattern and replaces it with nothing, leaving just the quoted URL. The x exchanges the edited line for the copy, so it outputs the original line while keeping the URL in the hold buffer.

The next bracketed expression uses s//\1/ to reuse the last pattern and replace the match with the captured characters. G appends the hold buffer contents to the end of the pattern space, after the newline. The s/\n/ / replaces the newline with a space.

Upvotes: 0

Related Questions