hash each line in text file

前端 未结 2 1954
陌清茗
陌清茗 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:17

    You can just call md5sum directly in the script:

    #!/bin/bash
    #read.file.line.by.line.sh
    
    while read line
    do
    echo $line | md5sum | awk '{print $1}'
    done
    

    That way the script spits out directly what you want: the md5 hash of each line.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题