Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Redis reply formatter

#!/bin/bash

# define the formatter for 'sed' script
# the formatter will arrange and combine data to a line 
FORMATTER=':a;N;$!ba'   # define the label for removing the new line "\n"
FORMATTER+=';/\n\./s/\.\n/\./g'
    # search the all indentation lines that start with "." and remove the new 
    # line "\n"
    # e.g:
    #
    #   ...                 ...2|
    #   2|             =>   (integer)
    #   (integer)           650
    #   650

FORMATTER+=';/\n\?(.\+\n/s/(integer)\n/(integer) /g'
    # search the all lines that that start with "(" and remove the new line "\n"
    # e.g:
    #
    #   ...                 ...
    #   2|                  2|
    #   (integer)       =>  (integer) 650
    #   650

FORMATTER+=';/^[0-9]\+|\n/s/|\n/) /g'
    # replace the all lines that that start with "1|", "2|",... "104|"... etc.
    # with "1) ", "2) ", "104) " and remove the new line "\n"
    # e.g:
    #
    #   ...                 ...
    #   2|                  2) (integer)
    #   (integer)       =>  650
    #   650


# the formatter will replace the indentation characters, leading dots,
# with spaces
INDENTATION=':b;s/^\(\.*\)\./\1 /;tb'


# the demo sample
INPUT=$(cat <<EOF
1|
1|
(integer)
600
...
2|
(integer)
650
2|
1|
(integer)
1
...
2|
(integer)
1
3|
1|
1|
"P190000000000002"
...
...
2|
"gold"
...
...
3|
(integer)
1
...
...
4|
"&demo/1551079022:GIFT"
...
2|
1|
"P190000000000003"
...
...
2|
"coin"
...
...
3|
(integer)
600
...
...
4|
"&demo/1551079022:GIFT"
4|
1|
"BronzeTicket.1"
...
2|
(integer)
2
5|
1|
1|
"sZt5Td/1550079000>1552089000-0"
...
...
2|
"{\"coin\":600,\"BronzeTicket.1\":1,\"gold\":1}"
EOF
)

# the excepted output
EXCEPT=$(cat <<EOF
1) 1) (integer) 600
   2) (integer) 650
2) 1) (integer) 1
   2) (integer) 1
3) 1) 1) "P190000000000002"
      2) "gold"
      3) (integer) 1
      4) "&demo/1551079022:GIFT"
   2) 1) "P190000000000003"
      2) "coin"
      3) (integer) 600
      4) "&demo/1551079022:GIFT"
4) 1) "BronzeTicket.1"
   2) (integer) 2
5) 1) 1) "sZt5Td/1550079000>1552089000-0"
      2) "{\"coin\":600,\"BronzeTicket.1\":1,\"gold\":1}"
EOF
)


# display the input data 
#printf "%s\n" "${INPUT}" | od -vtc -to1


# format the input data
OUTPUT=$(echo "${INPUT}" | sed "${FORMATTER}"  | sed "${INDENTATION}")

# display output
echo "${OUTPUT}"
echo

# check if the OUTPUT matches the EXCEPT
if [ "${OUTPUT}" = "${EXCEPT}" ];
then
  echo "ok"
else
  echo "mismatch"
fi

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.