【问题标题】:CSV file manipulationCSV 文件操作
【发布时间】:2019-08-02 07:55:43
【问题描述】:

下面的行代表我需要操作的 CVS 文件的简单行。

....column-n    column-m
---------------------------------
....0x65AFB4C6, 0x27D47C0

从上面我必须产生下一个输出:

0、101、175、180、198、0、39、212、124、0

换句话说,我必须将每个十六进制值拆分为位,并将每个位转换为十进制数,逗号分隔如下:

0x65AFB4C6 becomes
0x  65     AF     B4    C6 which is converted to decimal
0  ,101  , 175  ,180  , 198

我知道如何 grep 整个列,但是如何拆分每两个字符并将它们转换为十进制?有什么想法吗?

【问题讨论】:

  • 您能发布包含.....------column-n 的输出吗? .... 真的是输入的一部分吗?
  • 0x27D47C0应该如何拆分? 27d47c0?

标签: bash csv export-to-csv


【解决方案1】:

以下带有 cmets 的脚本:

# pipe our input
cat <<EOF |
....column-n    column-m
---------------------------------
....0x65AFB4C6, 0x27D47C0
EOF
# first extract the numbers from input
# print the numbers on separate newlines appended with two 00
sed -E 's/0x([[:xdigit:]]+)/\n00\1\n/g' |
# filter lines starting with two 00
grep '^00' |
# make each number have a separate line
sed -E 's/../&\n/g;' |
# remove empty lines, there will be some
sed '/^$/d' |
# convert hex lines into decimal lines
xargs -I{} printf "%d\n" "0x{}" |
# print the lines separated by comma
paste -sd,

产生以下输出:

0,101,175,180,198,0,39,212,124,0

paiza.io测试

【讨论】:

    猜你喜欢
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多