如何读取一行来计算md5 [英] how to read one line to calculate the md5

查看:221
本文介绍了如何读取一行来计算md5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Linux bash版本4.1.2

I am using Linux bash version 4.1.2

我有一个制表符分隔的input_file,其中包含5个字段,我想计算每行的MD5,并将md5sum放在每行的末尾.

I have a tab-delimited input_file having 5 fields and I want to calculate the MD5 for each line and put the md5sum at the end of each line.

因此,预期的output_file每行应具有6个字段.

The expected output_file should therefore has 6 fields for each line.

这是我的编码:

cat input_file | while read ONELINE
do
  THEMD5=`echo "$ONELINE" | md5sum | awk '{print $1}'`
  echo -e "${ONELINE}\t${THEMD5}"
done > output_file

大多数情况下,编码效果很好.

The coding works well most of the time.

但是,如果ONELINE以单/双标签结尾,则尾随的标签会消失!

However, if ONELINE is ended with single/double tabs, the trailing tab(s) will disappear!

结果,由于缺少选项卡,因此output_file有时会包含4或5个字段的行.

As a result, the output_file will sometimes contain lines of 4 or 5 fields, due to the missing tab(s).

我试图在while语句中添加IFS=IFS=''IFS=$'\n'IFS-$'\012',但是仍然不能解决问题.

I have tried to add IFS= or IFS='' or IFS=$'\n' or IFS-$'\012' in the while statement, but still cannot solve the problem.

请帮助.

Alvin SIU

Alvin SIU

推荐答案

如果您想在md5sums中包含尾随换行符(如原始代码所示),则可以肯定以下是正确的:

The following is quite certainly correct, if you want trailing newlines included in your md5sums (as your original code has):

while IFS= read -r line; do
  read sum _ < <(printf '%s\n' "$line" | md5sum -)
  printf '%s\t%s\n' "$line" "$sum"
done <input_file

注意:

  • IFS中的字符被read剥离;设置IFS=足以防止这种情况.
  • 在没有-r参数的情况下,read也会解释反斜杠文字,并将其删除.
  • 使用echo -e是很危险的:它解释行中的转义序列,而不是将它们作为文字散发.
  • 使用全大写变量名是错误的形式.请注意相关规范(尤其是第四段),请牢记Shell变量和环境变量共享一个名称空间.
  • 在处理不受控制的数据(特别是包括可能包含反斜杠文字的数据)时,通常使用echo是不好的形式.请参阅相关的POSIX规范,尤其是应用程序用法"和理据"部分./li>
  • 如果要以隐藏字符可见的方式打印行,请考虑使用'%q\t%s\n'而不是'%s\t%s\n'作为格式字符串.
  • Characters inside IFS are stripped by read; setting IFS= is sufficient to prevent this effect.
  • Without the -r argument, read also interprets backslash literals, stripping them.
  • Using echo -e is dangerous: It interprets escape sequences inside your line, rather than emitting them as literals.
  • Using all-uppercase variable names is bad form. See the relevant spec (particularly the fourth paragraph), keeping in mind that shell variables and environment variables share a namespace.
  • Using echo in general is bad form when dealing with uncontrolled data (particularly including data which can contain backslash literals). See the relevant POSIX spec, particularly the APPLICATION USAGE and RATIONALE sections.
  • If you want to print the lines in a way that makes hidden characters visible, consider using '%q\t%s\n' instead of '%s\t%s\n' as a format string.

这篇关于如何读取一行来计算md5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆