Score:0

Appending string or character to terminal output

cn flag

I need to run a command in a terminal to continuously receive incoming messages from elsewhere on the network. I have no control over the formulation of the length of the message. The command, however, is able to receive and print one full message at a time (and I also have no control over this command).

My task is to forward this stream of messages to a local port. An app listening to that port will further process the messages. Here is the problem: I have to set a receiving buffer at the app (say 1024 bytes) and as TCP protocol follows (as heavily discussed here), the messages just go through not in its their original chunk. And the length of the message may not be constant (as my example below)

I want to append one special character at the beginning and the end of the message that the command spits out in order to process them on the other side. Here is a more specific eg.:

On terminal, I run this command;

mycommand | tee | netcat localhost 2003    

This command receives the messages in their original chunks and forwards to port 2003:

action_1_timestamp_2021_6_09_10_34_23_abort
action_2_timestamp_2021_6_09_10_34_34_success
...
action_193848_timestamp_2021_6_23_10_34_23_hold

How can I wrap each line above like this :

<action_1_timestamp_2021_6_09_10_34_23_abort>

before it is forwarded by netcat localhost 2003. I am not very familiar with bash scripting.

hr flag
Well you could pipe it through something like `sed 's/.*/<&>/'` or `sed -e 's/^/</' -e 's/$/>/'` - but I suspect this is an XY problem?
Tristan Tran avatar
cn flag
@steeldriver Thanks for the bash code. I am going to try solve this in a different way. We can close this thread.
hr flag
You can delete the question yourself if you believe it is no longer useful / relevant
Tristan Tran avatar
cn flag
I want to keep reference to your code, still. So, will leave the question here.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.