modify a datastream on the fly

后端 未结 5 539
[愿得一人]
[愿得一人] 2021-01-27 09:21

I need to hijack and modify a datastream. The stream consists of fixed-width commands. Each command is a new line, and the documentation says that each command starts and ends

5条回答
  •  时光说笑
    2021-01-27 10:15

    In the Perl, with added check that the string starts with NAM and replacing really the ,FIRSTNAME:

    nc -kl 1100 | \
    perl -pe '/^STX NAM / && do { s/(,FIRSTNAME)/" " x length("$1")/ge }' | \
    nc <...>
    

    In Perl s///e as expected does substitution, but evaluates the replacement string as an expression. Operator x makes a new string, duplicating string on the left number of times given on the right. Non-matching strings are obviously not modified.

提交回复
热议问题