Stephen McKain
Stephen McKain

Reputation: 315

How to generate a UUID for each line in a file using AWK or SED?

I need to append a UUID ( newly generated unique for each line) to each line of a file. I would prefer to use SED or AWK for this activity and take advantage of UUIDGEN executable on my linux box. I cannot figure out how to generate the the UUID for each line and append it.

I have tried:

awk '{print system(uuidgen) $1} myfile.csv
sed -i -- 's/^/$(uuidgen)/g' myfile.csv

And many other variations that didn't work. Can this be done with SED or AWK, or should I be investigating another solution that is not shell script based?

Sincerely, Stephen.

Upvotes: 2

Views: 6852

Answers (6)

nroose
nroose

Reputation: 1792

These all seem pretty slow. Ruby can do it right quick.

ruby -r securerandom -ne 'print SecureRandom.uuid + " " + $_' file

Upvotes: 1

Ed Morton
Ed Morton

Reputation: 203324

Just tweaking the syntax on your attempt, something like this should work:

awk '("uuidgen" | getline uuid) > 0 {print uuid, $0} {close("uuidgen")}' myfile.csv

For example:

$ cat file
a
b
c

$ awk '("uuidgen" | getline uuid) > 0 {print uuid, $0} {close("uuidgen")}' file
52a75bc9-e632-4258-bbc6-c944ff51727a a
24c97c41-d0f4-4cc6-b0c9-81b6d89c5b77 b
76de9987-a60f-4e3b-ba5e-ae976ab53c7b c

The right solution is to use other shell commands though since the awk isn't buying you anything:

$ xargs -n 1 printf "%s %s\n" $(uuidgen) < file
763ed28c-453f-47f4-9b1b-b2f972b2cc7d a
763ed28c-453f-47f4-9b1b-b2f972b2cc7d b
763ed28c-453f-47f4-9b1b-b2f972b2cc7d c

Upvotes: 4

user4401178
user4401178

Reputation:

You can use paste and GNU sed:

paste <(sed 's/.*/uuidgen/e' file) file

This uses the GNU execute extension e to generate a UUID per line, then paste pastes the text back together. Use the -d paste flag to change the delimiter from the default tab, to whatever you want.

Upvotes: 0

tale852150
tale852150

Reputation: 1628

Using bash, this will create a file outfile.txt with a concatenated uuid:

NOTE: Please run which bash to verify the location of your copy of bash on your system. It may not be located in the same location used in the script below.

#!/usr/local/bin/bash

while IFS= read -r line
do
    uuid=$(uuidgen)
    echo "$line $uuid" >> outfile.txt
done < myfile.txt

myfile.txt:

john,doe
mary,jane
albert,ellis
bob,glob
fig,newton

outfile.txt

john,doe 46fb31a2-6bc5-4303-9783-85844a4a6583
mary,jane a14bb565-eea0-47cd-a999-90f84cc8e1e5
albert,ellis cfab6e8b-00e7-420b-8fe9-f7655801c91c
bob,glob 63a32fd1-3092-4a72-8c24-7b01c400820c
fig,newton 63d38ad9-5553-46a4-9f24-2e19035cc40d

Upvotes: 5

anubhava
anubhava

Reputation: 785058

Using xargs is simpler here:

paste -d " " myfile.csv <(xargs -I {} uuidgen {} < myfile.csv)

This will call uuidgen for each line of myfile.csv

Upvotes: 2

Diego Torres Milano
Diego Torres Milano

Reputation: 69198

Try this

awk '{ "uuidgen" |& getline u; print u, $1}' myfile.csv

if you want to append instead of prepend change the order of print.

Upvotes: 1

Related Questions