1

I'm connected to a MQTT stream, and I filter out the data by entering :

mosquitto_sub -d -t +/# 2>&1 |
sed -n "/PUBLISH/{s|.*\('.*',\).*|\1$(date),|;N;s/\n//;p}"

into the Ubuntu terminal. This prints the only things that I am interested from the MQTT stream.

Now that I can do this I would like to dump this information into a qwerty.TXT file. I've tried

mosquitto_sub -d -t +/# 2>&1 |
sed -n "/PUBLISH/{s|.*\('.*',\).*|\1$(date),|;N;s/\n//;w qwerty.txt}"

and got the following error:

sed: -e expression #1, char 0: unmatched '{'

I've looked into sed regex, and the examples are that w file.type writes into the file file.type where type can be .txt for example.

I've also tried

mosquitto_sub -d -t +/# 2>&1 |
sed -n "/PUBLISH/{s|.*\('.*',\).*|\1$(date),|;N;s/\n//;p >> qwerty.txt}"

and

mosquitto_sub -d -t +/# 2>&1 |
sed -n "/PUBLISH/{s|.*\('.*',\).*|\1$(date),|;N;s/\n//;1a qwerty.txt}"

How can I print my filtered MQTT stream into a file named qwerty.txt?

  • mosquitto_sub -d -t +/# 2>&1 | sed -n "/PUBLISH/{s|.*\('.*',\).*|\1$(date),|;N;s/\n//;p}" > qwerty.TXT – heemayl Feb 28 '15 at 2:15
  • @heemayl if i write mosquitto_sub -d -t +/# 2>&1 | sed -n "/PUBLISH/{s|.*\('.*',\).*|\1$(date),|;N;s/\n//;p}" >> qwerty.TXT will be appending the filtered MQTT stream into the qwerty.txt? – 3kstc Feb 28 '15 at 2:35
  • @heemay, thanks, and if I wanted to make this command run in the background with an & would it just be mosquitto_sub -d -t +/# 2>&1 | sed -n "/PUBLISH/{s|.*\('.*',\).*|\1$(date),|;N;s/\n//;p}" > qwerty.TXT & ? – 3kstc Feb 28 '15 at 2:44
  • @heemayl, I've checked into the qwerty.txt, the file is empty. Even though mosquitto_sub -d -t +/# 2>&1 | sed -n "/PUBLISH/{s|.*\('.*',\).*|\1$(date),|;N;s/\n//;p}" prints data on a parallel terminal. Is there a way of printing the datum inside of the sed command i.e. inside mosquitto_sub -d -t +/# 2>&1 | sed -n "/PUBLISH/{s|.*\('.*',\).*|\1$(date),|;N;s/\n//;p}"? – 3kstc Feb 28 '15 at 2:50
  • @don_crissti, I owe you more then a thanks! just writing the file didn't work, writing the full directory (...>>/home/ed/file.txt) did the trick though! And to make this run 24/7 in the background I would just need to add an & at the end of the command right? from reading various sites/forums $ some_command & will run the some_command in the background until its stopped. – 3kstc Feb 28 '15 at 8:56
1

To terminate a w command in sed, you need an end of line or an end of string.

You can either embed a literal newline in your sed command (which is rather irritating for interactive use) or you can use multiple -e commands.

So instead of:

sed -n "/PUBLISH/{s|.*\('.*',\).*|\1$(date),|;N;s/\n//;w qwerty.txt}"

You would use:

sed -n -e "/PUBLISH/{s|.*\('.*',\).*|\1$(date),|;N;s/\n//;w qwerty.txt" -e "}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.