bash awk 和 md5:替换一列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23696722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:27:39  来源:igfitidea点击:

awk and md5: replace a column

bashawkmd5

提问by Florin Ghita

Starting from Awk replace a column with its hash value, I tried to hash(md5) a list of numbers:

Awk开始用它的哈希值替换一列,我试图对一个数字列表进行哈希(md5):

$ cat -n file
 1  40755462755
 2  40751685373
 3  40730094339
 4  40722740446
 5  40722740446
 6  40743802204
 7  40730094339
 8  40745188886
 9  40740593352
10  40745561530

If I run:

如果我运行:

cat file | awk '{cmd="echo -n "  " | md5sum|cut -d\" \" -f1"; cmd|getline md5; =md5;print;}' | cat -n
 1  29ece26ce4633b6e9480255db194cc40
 2  120148eca0891d0fc645413d0f26b66b
 3  cafc48d392a004f75b669f9d1d7bf894
 4  7b4367e8f58835c0827dd6a2f61b7258
 5  7b4367e8f58835c0827dd6a2f61b7258
 6  49b12d1f3305ab93b33b330e8b1d3165
 7  49b12d1f3305ab93b33b330e8b1d3165
 8  bee44c89ac9d4e8e4e1f1c5c63088c71
 9  f07262ac8f53755232c5abbf062364d0
10  2ac7c22170c00a3527eb99a2bfde2c2c

I don't know why the line 7 get the same md5 as line 6 because if I run them separately they are different:

我不知道为什么第 7 行与第 6 行得到相同的 md5,因为如果我单独运行它们,它们是不同的:

$ echo -n 40743802204 | md5sum|cut -d" " -f1
49b12d1f3305ab93b33b330e8b1d3165
$ echo -n 40730094339 | md5sum|cut -d" " -f1
cafc48d392a004f75b669f9d1d7bf894

I tried some prints:

我尝试了一些打印:

cat file| awk '{print 
while read -r num; do 
    echo -n $num | md5sum | cut -d ' ' -f1; 
done < file
29ece26ce4633b6e9480255db194cc40
120148eca0891d0fc645413d0f26b66b
cafc48d392a004f75b669f9d1d7bf894
7b4367e8f58835c0827dd6a2f61b7258
7b4367e8f58835c0827dd6a2f61b7258
49b12d1f3305ab93b33b330e8b1d3165
cafc48d392a004f75b669f9d1d7bf894
bee44c89ac9d4e8e4e1f1c5c63088c71
f07262ac8f53755232c5abbf062364d0
2ac7c22170c00a3527eb99a2bfde2c2c
,NF,NR;cmd="echo -n " " | md5sum|cut -d\" \" -f1"; cmd|getline md5; =md5"---"cmd"---";print;}' | cat -n

but with no success to find what's going wrong.

但没有成功找到问题所在。

EDIT:As the title says, I try to replace a column in a file(a file with hundred fields). So, $1 would be $24 and NF would be 120 for a file and 233 for another file.

编辑:正如标题所说,我尝试替换文件中的一列(具有一百个字段的文件)。因此,$1 为 24 美元,一个文件的 NF 为 120,另一个文件为 233。

回答by Florin Ghita

Ok, I found the issue. The pipes in awk should be closed. So, I needed a close(cmd);

好的,我发现了问题。awk 中的管道应该关闭。所以,我需要一个close(cmd);

I found the solution here

我在这里找到了解决方案

回答by jaypal singh

I wouldn't use getlinein awklike that. You can do:

我不会用getlineawk这样的。你可以做:

< tmp | while read num ; do echo -n $num | md5sum | cut -f1 -d' '; done | cat -n

回答by Ed Morton

I would GUESS, but can't tell since you aren't testing it's return code, that it's because your getlineis failing at line 7 so md5has the same value it did for the previous line. Use of getlineis fraught with caveats and not for use by beginners, see http://awk.info/?tip/getline.

我猜,但不能说,因为你没有测试它的返回码,那是因为你getline在第 7 行失败,所以md5与前一行的值相同。的使用getline充满了警告,不适合初学者使用,请参阅http://awk.info/?tip/getline

What value are you getting out of using awk for this anyway as opposed to just staying in shell?

无论如何,与仅仅停留在 shell 中相比,您为此使用 awk 有什么价值?

回答by viraptor

It's a bit awkward with all the quoting - I'm not sure why would it fail to be honest. But here's something that uses less awk and works just fine:

所有的引用都有些尴尬 - 我不知道为什么它会不诚实。但这里有一些使用较少 awk 并且工作正常的东西:

##代码##