Home > Blockchain >  Can I use sed to generate UUIDs inline instead of echo?
Can I use sed to generate UUIDs inline instead of echo?

Time:04-07

This bash snippet works to add a UUID and a tab character (\t) at the start of each line of a file.

    while read; do
    echo "$(uuidgen | tr 'A-Z' 'a-z')\t$REPLY"
    done < sourcefile.tsv > temp_destination.tsv

(Note the reason for the pipe to TR is to convert them to lowercase in MacOS version of UUID-generation).

Although that performs well for smaller files, it doesn't seem efficient.

sed -i '' "s/^/$(uuidgen | tr 'A-Z' 'a-z')\t/" sourcefile.tsv

Again, using MacOS bash so the '' after the -i flag is required since I don't want a backup file.

I think sed would perform better, but I think I have to have the UUID generation in some sort of loop.

I'm just looking to make this faster and/or perform more efficiently. It's working, but it's pretty slow on a 20,000-line file, and all other attempts by me have stumped me.

CodePudding user response:

Did you try something like that:

{
  uuidgen | tr 'A-Z' 'a-z'
  echo -n "\t"
  cat 'sourcefile.tsv'
} > temp_destination.tsv

You may think it is not much different from your "read" version, but it is:

  • You don't capture the result of uuidgen
  • cat will probably perform faster than read $REPLY

CodePudding user response:

Try this out:

while read; do printf "%s\t%s\n" $(uuidgen) "$REPLY"; done < input.tsv > output.tsv

No monkeying around with building strings.

  • Related