我有一个问题,我有一个文件 test.txt,其内容如下:
objectClass: CPM1
objectClass: CPM2
objectClass: CPM3
objectClass: CPM4
objectClass: CPZ
objectClass: CP04s
objectClass: CP0A
objectClass: CP11
entryDS: 1
nodeId: 21
createTimestamp: 20211014155438Z
modifyTimestamp: 20220313041228Z
MSISDN: 353834079137
IMSI: 272050103105233
NAM: 0
CDC: 1423
IMEISV:: U1RQJgAkQiE=
AUTHINFO: 0
CSP: 11
SUBSCSPVERS: 20
RSA: 20
SUBSRSAVERS: 19
APNID1: 4
每次找到文本 IMEISV:: 时,我想用以下函数的结果替换该文件中 IMEISV:: (在本例中为 U1RQJgAkQiE=)之后的值:
base64 -d | od -t x8 -An | rev
如果我在 unix 中这样做,回显将是这样的:
echo U1RQJgAkQiE= | base64 -d | od -t x8 -An | rev
结果将是 3545056200422412
因此,在文件末尾,值 U1RQJgAkQiE= 应为 3545056200422412。如下所示:
objectClass: CPM1
objectClass: CPM2
objectClass: CPM3
objectClass: CPM4
objectClass: CPZ
objectClass: CP04s
objectClass: CP0A
objectClass: CP11
entryDS: 1
nodeId: 21
createTimestamp: 20211014155438Z
modifyTimestamp: 20220313041228Z
MSISDN: 353834079137
IMSI: 272050103105233
NAM: 0
CDC: 1423
IMEISV:: 3545056200422412
AUTHINFO: 0
CSP: 11
SUBSCSPVERS: 20
RSA: 20
SUBSRSAVERS: 19
APNID1: 4
有人可以让我知道如何使用 sed/perl 来做到这一点吗?我真的是一个unix新手
答案1
怎么样
unpack("h*", decode_base64($_))
其中decode_base64
是由 perlMIME
模块提供的,例如:
$ perl -MMIME::Base64 -pe 's/IMEISV:: \K(.*)$/unpack("h*", decode_base64($1))/e' test.txt
objectClass: CPM1
objectClass: CPM2
objectClass: CPM3
objectClass: CPM4
objectClass: CPZ
objectClass: CP04s
objectClass: CP0A
objectClass: CP11
entryDS: 1
nodeId: 21
createTimestamp: 20211014155438Z
modifyTimestamp: 20220313041228Z
MSISDN: 353834079137
IMSI: 272050103105233
NAM: 0
CDC: 1423
IMEISV:: 3545056200422412
AUTHINFO: 0
CSP: 11
SUBSCSPVERS: 20
RSA: 20
SUBSRSAVERS: 19
APNID1: 4
答案2
sed 's/\(IMEISV::\)\(.*\)/echo -n \1" ";echo \2\| base64 -d \| od -t x8 -An\|rev /ge ' input.txt
诀窍是 e 开关。输出:
objectClass: CPM1
objectClass: CPM2
objectClass: CPM3
objectClass: CPM4
objectClass: CPZ
objectClass: CP04s
objectClass: CP0A
objectClass: CP11
entryDS: 1
nodeId: 21
createTimestamp: 20211014155438Z
modifyTimestamp: 20220313041228Z
MSISDN: 353834079137
IMSI: 272050103105233
NAM: 0
CDC: 1423
IMEISV:: 3545056200422412
AUTHINFO: 0
CSP: 11
SUBSCSPVERS: 20
RSA: 20
SUBSRSAVERS: 19
APNID1: 4
答案3
一个想法 sed
是在替换中执行上面的命令,sed
使用printf
打印到第一个匹配的组而不换行。
$ sed "s/\(IMEISV::\)\(.*\)/printf '\1 ' ; echo \2 | base64 -d | od -t x8 -An | rev/e" input_file
objectClass: CPM1
objectClass: CPM2
objectClass: CPM3
objectClass: CPM4
objectClass: CPZ
objectClass: CP04s
objectClass: CP0A
objectClass: CP11
entryDS: 1
nodeId: 21
createTimestamp: 20211014155438Z
modifyTimestamp: 20220313041228Z
MSISDN: 353834079137
IMSI: 272050103105233
NAM: 0
CDC: 1423
IMEISV:: 3545056200422412
AUTHINFO: 0
CSP: 11
SUBSCSPVERS: 20
RSA: 20
SUBSRSAVERS: 19
APNID1: 4
答案4
#------------------
# base64 in sed
#------------------
set -u
#> format string printf
fmt='%s%s\n'
#> bit, sextet, & octet regex
bit='[01]'
sextet="${bit}{6}"
octet="${bit}{8}"; byte=$octet
#> base64 charset
b64='/[:alnum:]+'
declare -a b64_chars=({A..Z} {a..z} {0..9} + /)
#> user-defined helper function(s)
oneLine() {
# collapse stdin into one line
paste -sd'\0' -
}
esc_rhs() {
# make stdin pkuggable
# on the rhs of a s///
sed -e '
s:[\/&]:\\&:g
$!s:$:\\:
' -
}
dec2bin() {
# create an array of size 2^$1
# Usage: dec2bin 6
# creates array d2b as:
# underscore for clarity only
# $d2b[0] => "000_000"
# $d2b[1] => "000_001"
# ...
# $d2b[63] => "111_111"
eval "d2b=($(yes '{0,1}' | sed "$1q" | oneLine))"
}
#> build the encoding lookup table
encode_tbl=$(printf '%s\n' "$(
dec2bin 6
i=0
for c in "${b64_chars[@]}"
do
printf "$fmt" "$c" "${d2b[$i]}"
(( i++ ))
done
)" | esc_rhs)
#> build the decoding lookup table
decode_tbl=$(printf '%s\n' "$(
dec2bin 8
for dec in {0..255}
do
hex=$(printf '%02x' "$dec")
bin=${d2b[$dec]}
printf "$fmt" "$bin" "$hex"
done
)" | esc_rhs)
#######> main()
sed -E "
s/^IMEISV::\s*//;t base64
b
:base64
s/[^${b64}]//g
/^\$/d
H;z;x;G
s/\$/$encode_tbl/
:encode
/\n\n/!{
s/((\n)[${b64}])(.*\1(${sextet}))/\4\2\3/
b encode
}
s/\n.*//;:pad
/^(${octet})+\$/!{
s/\$/0/;b pad
}
# chunk it in 8-bit portions
s/(${octet})/& /g
G;s/\$/${decode_tbl}/;t decode
:decode
s/(${octet}) (.*\n\1([[:xdigit:]]{2}))/\3\2/
t decode
s/\n.*//
s/([[:xdigit:]])([[:xdigit:]])/\2\1/g
s/00?\$//
s/^/IMEISV:: /
" file