Embed cmd into sed replacement to have dynamic substitution

Hello,
Is it possible to embed the command output to the replacement of sed? Say I need to replace all the blank lines with random strings:
input:

ACCTTCGTCTTCTGG

GCTTGAGATGGTCCA

GCAGGGCTAGTGACG

GACGAGTCTCTTGAC

ACCAAATCAAAGATC

and output is:

>26aa36d934d44f06d15b3aab4645a602 $(date | md5sum)
ACCTTCGTCTTCTGG
>0b23ffccf2d708b8b3f0eaed1fbaeae7 
GCTTGAGATGGTCCA
>f7d0caf30e50bbf55f69678854b610cd
GCAGGGCTAGTGACG
>06a28f5fcc2e8c5d984cf2dd8bbd3a61
GACGAGTCTCTTGAC
>f47cd7fa172ab69142f76c6de7ea1c4f
ACCAAATCAAAGATC

something like:

sed 's/^$/'">$(date | md5sum)"'/g'

but above script print out single same string for each substitution, not as unique different string each time. i.e.

date | md5sum

is not dynamic. Any help? Thanks a lot!
Yifang

while read l ; do [[ -z "$l" ]] && l=">`echo $RANDOM|md5sum|sed 's/ .*//'`"; echo "$l" ; done < infile

Worked like a chime!
The script is twisty to me. Could you explain the do part?

do [[ -z "$l" ]] && l=">`echo $RANDOM|md5sum|sed 's/ .*//'`"; 

Wait a minute, the replacement is not unique. Believe it is related to $RANDOM|md5sum. Any clue about this?
Thanks a lot!

do [[ -z "$l" ]] && l=">`echo $RANDOM|md5sum|sed 's/ .*//'`";

if line ("$l") is empty replace with ">" plus random generated string (trailing space and other charachters removed).

Thanks! Now I understand the replacement part. Can I ask what this part does?

 [[ -z "$l" ]] 

and How come you used && condition to check empty line. Still could not figure it out. Thanks again.

&& : execute following statement if condition is true

1 Like

-z means "if string is zero length".

1 Like

Thanks rdrtx1 and Corona688! Now I think I get the whole picture of the script.
If go back to my original question, the command ($RANDOM|md5sum) was not embedded in the sed substitution part. And my code

sed 's/^$/'">$(date | md5sum)"'/g'

is not dynamic which does not count either. I read some of the post about embedding cmd in AWK script. Is that possible for SED too, say embeding a perl script in it to make it work? Thanks again!

You cannot embed commands inside a sed expression and expect sed to execute them itself. If you manage to get them executed at all, it will be the shell that executes them, and only once, before sed runs.

I could probably cook up what you wanted in awk, if I knew what you wanted. Perl is overkill in most cases.

Thanks Corona!
What I want is to replace all the blank rows with different unique strings to match the different sequences of every other row. The one rdrtx1 posted worked well except the replacement is not unique to each other, which is believed from the part:

echo $RANDOM|md5sum

Please post your awk script, if you can. Thanks a lot!