hash each line in text file

前端 未结 2 1953
陌清茗
陌清茗 2020-12-17 17:34

I\'m trying to write a little script which will open a text file and give me an md5 hash for each line of text. For example I have a file with:

123
213
312
<         


        
2条回答
  •  囚心锁ツ
    2020-12-17 18:29

    Almost there, try this:

    while read -r line; do printf %s "$line" | md5sum | cut -f1 -d' '; done < 123.txt
    

    Unless you also want to hash the newline character in every line you should use printf or echo -n instead of echo option.

    In a script:

    #! /bin/bash
    cat "$@" | while read -r line; do
        printf %s "$line" | md5sum | cut -f1 -d' '
    done
    

    The script can be called with multiple files as parameters.

提交回复
热议问题