Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
7 replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
source | link

I was in the process of writing the following answer to the other, related question herehere when it was deleted. I don't know if you'll be able to follow that link, but (an abbreviation of) the example data referred to below is this:

I was in the process of writing the following answer to the other, related question here when it was deleted. I don't know if you'll be able to follow that link, but (an abbreviation of) the example data referred to below is this:

I was in the process of writing the following answer to the other, related question here when it was deleted. I don't know if you'll be able to follow that link, but (an abbreviation of) the example data referred to below is this:

6 added 5 characters in body
source | link

I was in the process of writing the following answer to the other, related question here when it was deleted. I dunnodon't know if you'll be able to follow that link, but (an abbreviation of) the example data referred to below is this:

I was in the process of writing the following answer to the other, related question here when it was deleted. I dunno if you'll be able to follow that link, but (an abbreviation of) the example data referred to below is this:

I was in the process of writing the following answer to the other, related question here when it was deleted. I don't know if you'll be able to follow that link, but (an abbreviation of) the example data referred to below is this:

5 added 254 characters in body
source | link
mosquitto ... 2>&1 | 
sed -u "/.*PUBLISH[^'"']*\*PUBLISH[^']*\([^,]*\]*,\).*/!d 
       s//\1/;$!N;s;N;s/'\''[\%]/&\\&&&&/g
       sg;s/.\n'/",[$(date +%s),]"'\''&\\\\&&/  g
       s/.*/printf "%s\\n"'" '&'/";s/\n/'&date |'+[%s,]/"|  
sh -s

...which is, I think, what you want. It works by ensuring all of the input is safely quoted for both the shell and for date and printf's format strings, and then constructing shell commands out of each line pair.

For each line pair printf first prints the contents of the first line w/out a trailing \newline, then date prints the [timestamp] in seconds since the epoch followed by the contents of the second line and a trailing \newline. sed's output looks something like this:

printf "%s\n" ''\''m/gf/TMX6BP/075/d/SVlts'\'"SVlts'\'',[$('
date +%s)'+[%s,]"'86]86,1224830,27610 27869 17565'

What's moreNone of the [date] part is only run whencommands printed at sh receives the, of course, are executed until -ssedtreamed input command prints them at it. So if sed were reading mosquitto's output and one line came in later than a previous one then the date line would reflect that.

(set -f; IFS='
';  for l in $(xsel -bo)
    do  printf %s\\n "$l"
        sleep 1
    done) |
sed -u "/.*PUBLISH[^'"']*\*PUBLISH[^']*\([^,]*\]*,\).*/!d 
       s//\1/;$!N;s;N;s/'\''[\%]/&\\&&&&/g
       sg;s/.\n'/",[$(date +%s),]"'\''&\\\\&&/  g
       s/.*/printf "%s\\n"'" '&'/";s/\n/'&date |'+[%s,]/"|  
sh -s
mosquitto ... 2>&1 | 
sed -u "/.*PUBLISH[^'"']*\([^,]*\).*/!d 
       s//\1/;$!N;s/'\''/&\\&&/g
       s/.\n/",[$(date +%s),]"'\''/  
       s/.*/printf "%s\\n"'" '&'/" |
sh -s

...which is, I think, what you want. It works by ensuring all of the input is safely quoted, and then constructing shell commands out of each line pair. sed's output looks something like this:

printf "%s\n" ''\''m/gf/TMX6BP/075/d/SVlts'\'",[$(date +%s),]"'86,1224830,27610 27869 17565'

What's more the [date] part is only run when sh receives the -streamed input command. So if sed were reading mosquitto's output and one line came in later than a previous one then the date line would reflect that.

(set -f; IFS='
';  for l in $(xsel -bo)
    do  printf %s\\n "$l"
        sleep 1
    done) |
sed -u "/.*PUBLISH[^'"']*\([^,]*\).*/!d 
       s//\1/;$!N;s/'\''/&\\&&/g
       s/.\n/",[$(date +%s),]"'\''/  
       s/.*/printf "%s\\n"'" '&'/" |
sh -s
mosquitto ... 2>&1 | 
sed -u "/.*PUBLISH[^']*\([^,]*,\).*/!d 
       s//\1/;N;s/[\%]/&&/g;s/'/&\\\\&&/g
       s/.*/printf '&'/;s/\n/'&date '+[%s,]/"|  
sh -s

...which is, I think, what you want. It works by ensuring all of the input is safely quoted for both the shell and for date and printf's format strings, and then constructing shell commands out of each line pair.

For each line pair printf first prints the contents of the first line w/out a trailing \newline, then date prints the [timestamp] in seconds since the epoch followed by the contents of the second line and a trailing \newline. sed's output looks something like this:

printf ''\''m/gf/TMX6BP/075/d/SVlts'\'','
date '+[%s,]86,1224830,27610 27869 17565'

None of the commands printed at sh, of course, are executed until sed prints them at it. So if sed were reading mosquitto's output and one line came in later than a previous one then the date line would reflect that.

(set -f; IFS='
';  for l in $(xsel -bo)
    do  printf %s\\n "$l"
        sleep 1
    done) |
sed -u "/.*PUBLISH[^']*\([^,]*,\).*/!d 
       s//\1/;N;s/[\%]/&&/g;s/'/&\\\\&&/g
       s/.*/printf '&'/;s/\n/'&date '+[%s,]/"|  
sh -s
4 added 5 characters in body
source | link
3 added 7 characters in body
source | link
2 added 7 characters in body
source | link
1
source | link