sed assistance

Hello everyone.

I am trying to replace

sprintf(buffer, "{\"id\":1,\"method\":\"mining.update_block\",\"params\":[\"tu8tu5\",%d,\"%s\"]}\n", coinid, blockhash);

with

sprintf(buffer, "{\"id\":1,\"method\":\"mining.update_block\",\"params\":[\"'$blckntifypass'\",%d,\"%s\"]}\n", coinid, blockhash);

this is the code I was trying but is not working

sudo sed -i 's/sprintf(buffer, "{\"id\":1,\"method\":\"mining.update_block\",\"params\":[\"tu8tu5\",%d,\"%s\"]}\n", coinid, blockhash);/sprintf(buffer, "{\"id\":1,\"method\":\"mining.update_block\",\"params\":[\"'$blckntifypass'\",%d,\"%s\"]}\n", coinid, blockhash);/g' blocknotify.cpp

any help is appreciated!

Welcome to the forum.

Your request is not quite clear. Please exercise due care when phrasing your specification. Do you want to replace tu8tu5 with the string constant $blckntifypass or the respective variable's contents? Are the single quotes around it required in your result, or are they to enable shell expansion of the variable? Is that the only line in the file or has it to be selected, are there more occurrences of tu8tu5 ?

Im sorry if I wasnt more clear. Sometimes I dont get out everything I am thinking.

But yes I am trying to replace just the one line removing the

with the variable

which that variable is a randomly generated password.

And that is the only line in the code that needs to be changed and contains tu8tu5

a bit hairy, but...
Might not be too specific as it anchors around %d

sed "s#\[.*%d#[\\\\\"\'\$blckntifypass\'\\\\\",%d#" myFile

Wouldn't

sed 's/tu8tu5/'$blckntifypass'/' file

work, then?

bah im getting old in my old age! thank you

A little bit safer (in case the password contains white space characters) and a couple of characters shorter:

sed "s/tu8tu5/$blckntifypass/" file

but, of course, neither of these will work if the expansion of the variable contains a slash character ( / ).

I am using

cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1

to generate the password, so far no issues with the

sed 's/tu8tu5/'$blckntifypass'/' file

Thanks for all the help!

How about

</dev/urandom tr -dc 'a-zA-Z0-9' | dd bs=1 count=32

to generate the password?

Hi.

For password generation, see also:

apg     generates several random passwords (man)
Path    : /usr/bin/apg
Length  : 15 lines
Type    : Bourne-Again shell script, ASCII text executable
Shebang : #!/bin/bash
Repo    : Debian 8.8 (jessie) 

pwgen   generate pronounceable passwords (man)
Path    : /usr/bin/pwgen
Type    : ELF 64-bit LSB executable, x86-64, version 1 (SYSV ...)
Repo    : Debian 8.8 (jessie) 

For a system like:

OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.8 (jessie) 

as well as some others, e.g. Fedora, Debian-based systems.

Best wishes ... cheers, drl