0

I am reading a CSV file that contains 1 column,

  1. 220657
  2. 202512
  3. 156736

Here is the code to read and generate hash,

#! /bin/bash
while IFS="," read -r rec_column1
do
    echo "Displaying Record for-$rec_column1"
    echo -n "$rec_column1" | sha256sum
    echo ""
done < <(tail -n +1 users.csv)

The correct sha256 hash for 220657 a2983a41141e5197d3cf99732288d92e852027751c6a1e844d4dcdbf19549012

but generates this instead, 394170bd2537d3634697e00ce8de3d02464a4158a16aeb7b30b5acdb27d286b0

any idea why?

Atish
  • 135
  • 2
  • 2
  • 10
  • Your hash is from `printf '220657\r' | sha256sum` because you're including the carriage return from the csv file – that other guy Nov 18 '21 at 22:43
  • How do I make it right? – Atish Nov 18 '21 at 23:28
  • Please look at the answer for [the duplicate](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings), under "DOS/Windows line endings in input data". It lists a number of ways you can strip carriage returns – that other guy Nov 18 '21 at 23:44
  • Thanks! IFS=$'\r' solved it – Atish Nov 18 '21 at 23:49
  • Also, if the CSV file actually has more than one column, `IFS="," read -r rec_column1` will store the entire line -- not just the first column -- in `rec_column1` (because it always puts any additional fields into the last variable). To avoid this, add another variable to hold the rest of the fields, like `IFS="," read -r rec_column1 extra_columns` – Gordon Davisson Nov 19 '21 at 00:13

0 Answers0