modify a datastream on the fly

后端 未结 5 546
[愿得一人]
[愿得一人] 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:16

    This should do it, its a one liner

    perl -pe '/^STX\s\w+\s\d+#\s\w+,(\w+)\sETX$/;$len=length($1);s/$1/" " x $len/e'
    

    I tested using.

    echo "STX NAM 100# LASTNAME,FIRSTNAME ETX" | perl -pe '/^STX\s\w+\s\d+#\s\w+,(\w+)\sETX$/;$len=length($1);s/$1/" " x $len/e'
    

    and it returns

    STX NAM 100# LASTNAME,          ETX
    

    To ensure the strings are the same length i tested.

    echo "STX NAM 100# LASTNAME,FIRSTNAME ETX" | perl -pe '/^STX\s\w+\s\d+#\s\w+,(\w+)\sETX$/;$len=length($1);s/$1/" " x $len/e'| perl -pe 'print length($_);'
    

    Gives 36.

    echo "STX NAM 100# LASTNAME,FIRSTNAME ETX"|perl -pe 'print length($_);'
    

    Gives 36.

提交回复
热议问题