sách gpt4 ai đã đi

Sử dụng awk để băm bằng sha1sum

In lại 作者:行者123 更新时间:2023-12-01 11:35:01 29 4
mua khóa gpt4 Nike

我有一个大约有 20 列的“管道分隔”文件。我只想使用 sha1sum 散列第一列,它是一个数字,如帐号,并按原样返回其余列。

使用 awk 或 sed 执行此操作的最佳方法是什么?

Accountid|Time|Category|.....
8238438|20140101021301|sub1|...
3432323|20140101041903|sub2|...
9342342|20140101050303|sub1|...

以上是仅显示 3 列的文本文件示例。只有第一列在其上实现了哈希函数。结果应该是:
Accountid|Time|Category|.....
104a1f34b26ae47a67273fe06456be1fe97f75ba|20140101021301|sub1|...
c84270c403adcd8aba9484807a9f1c2164d7f57b|20140101041903|sub2|...
4fa518d8b005e4f9a085d48a4b5f2c558c8402eb|20140101050303|sub1|...

1 Câu trả lời

Best Way™ 是什么有待讨论。用 awk 做到这一点的一种方法是

awk -F'|' 'BEGIN { OFS=FS } NR == 1 { print } NR != 1 { gsub(/'\''/, "'\'\\\\\'\''", $1); command = ("echo '\''" $1 "'\'' | sha1sum -b | cut -d\\ -f 1"); command | getline hash; close(command); $1 = hash; print }' filename

那是
BEGIN {
OFS = FS # set output field separator to field separator; we will use
# it because we meddle with the fields.
}
NR == 1 { # first line: just print headers.
in
}
NR != 1 { # from there on do the hash/replace
# this constructs a shell command (and runs it) that echoes the field
# (singly-quoted to prevent surprises) through sha1sum -b, cuts out the hash
# and gets it back into awk with getline (into the variable hash)
# the gsub bit is to prevent the shell from barfing if there's an apostrophe
# in one of the fields.
gsub(/'/, "'\\''", $1);
command = ("echo '" $1 "' | sha1sum -b | cut -d\\ -f 1")
command | getline hash
close(command)

# then replace the field and print the result.
$1 = hash
in
}

您会注意到顶部的 shell 命令和底部的 awk 代码之间的差异;这都是由于 shell 膨胀。因为我将 awk 代码放在 shell 命令中的单引号中(在这种情况下双引号不值得争论, $1 和所有内容),并且因为代码包含单引号,使其内联工作会导致噩梦的反斜杠。因此,我的建议是将 awk 代码放入一个文件中,比如 foo.awk ,并运行
awk -F'|' -f foo.awk filename

反而。

关于使用 awk 使用 sha1sum 进行散列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27930643/

29 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com