3

file.txt has multiple occurrences of the word 'dummy' and the goal is to replace each occurrence of such word by a novel UUID.

I was wondering if there is a solution for this using a single-line command, but if not I would like the simplest possible bash script. For example, something in line with:

sed -i "s/dummy/$(uuidgen)/g" file.txt

unfortunately this command substitutes every word 'dummy' by the same uuid.

e.g.

Input:

{
{"myid":"dummy"},
{"myid":"ymmud"},
{"myid":"dummy"},
{"myid":"ymmud"},
{"myid":"dummy"},
{"myid":"ymmud"}
}

expected output:

{
{"myid":"79769E7B-BED5-4CB5-AEF9-CCE445D9212E"},
{"myid":"ymmud"},
{"myid":"F2FDDD4A-4800-4F0F-911A-FEDBC82915DD"},
{"myid":"ymmud"},
{"myid":"52D93565-81E9-479C-8BD9-457754581BBE"},
{"myid":"ymmud"}
}
João Matos
  • 6,102
  • 5
  • 41
  • 76
  • Please add sample input and expected sample output in your post and let us know then. – RavinderSingh13 May 25 '18 at 13:17
  • @RavinderSingh13 let me know if this example is not satisfactory – João Matos May 25 '18 at 13:22
  • @JoãoMatos: Is this a JSON text you are manipulating? – Inian May 25 '18 at 13:27
  • 1
    Check this answer: https://stackoverflow.com/a/29329216/745235. Based on the answer, you can do something like this: `sed '/dummy/ { h; s/.*/echo $(uuidgen)/e; x; G; s/dummy\n// }' yourfilename >> newfile` –  May 25 '18 at 13:30

1 Answers1

5

I was wondering if there is a solution for this using a single-line command, but if not I would like the simplest possible bash script

Here is a possible solution to your problem:

Assuming your input file is eg.

{
  {"myid":"dummy"},
  {"myid":"ymmud"},
  {"myid":"dummy"},
  {"myid":"ymmud"},
  {"myid":"dummy"},
  {"myid":"ymmud"}
}

then the following bash script

while IFS= read -r line; do
  echo $line | sed "s/dummy/`uuid`/g";
done < jsonfile

will output

{
  {"myid":"a1c3874c-601d-11e8-97bd-705ab6b2eca7"},
  {"myid":"ymmud"},
  {"myid":"a1c45e56-601d-11e8-922c-705ab6b2eca7"},
  {"myid":"ymmud"},
  {"myid":"a1c57390-601d-11e8-aa47-705ab6b2eca7"},
  {"myid":"ymmud"}
}

Originally your question talked about a textfile hence this specific solution which ignores the format. If you want to specifically manipulate JSON, I recommend eg. jq (apt-get install jq).

by a novel UUID

The UUID key space (depending on the UUID type) is HUGE. It is very unlikely to cause collisions given a low amount of data.

Bjoern Rennhak
  • 6,766
  • 1
  • 16
  • 21
  • I'm getting a 'command not found' message repeated in loop, what am I missing? – João Matos May 25 '18 at 13:44
  • 1
    UUID are *designed* to avoid collisions in *any* reasonable situation. – chepner May 25 '18 at 13:44
  • @JoãoMatos seems you are missing the `uuid` command. `apt-get install uuid` maybe? – Bjoern Rennhak May 25 '18 at 13:53
  • @chepner Exactly, but given enough (huge amount of) iterations, collisions CAN happen. – Bjoern Rennhak May 25 '18 at 13:55
  • "Huge enough" means "more iterations than you can complete before the heat death of the universe". It simply is not a practical concern. – chepner May 25 '18 at 13:56
  • 1
    @chepner The heat death of the universe will occur at a magnitude of 10^1000 if protons decay (probably). The UUID collisions will occur already at approx. 10^18 so your analogy is strictly speaking not correct. The spirit of your point is certainly correct. – Bjoern Rennhak May 25 '18 at 14:00
  • @BjoernRennhak you are correct I don't have the command available. I don't think it is available on osx, do you think your solution would work with uuidgen instead? – João Matos May 25 '18 at 14:04
  • @BjoernRennhak disregard my last comment, I was able to install the uuid command on osx with 'brew install ossp-uuid'. It works now – João Matos May 25 '18 at 14:06