1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CSVファイル内の特定列をsha256に変換する

Posted at

CSVの1列目をsha256ハッシュ値に変更する。

sample.csv

カラム1,カラム2,カラム3,カラム4
c4c2OCNF4F,aaaaa,bbbbb,ccccc
6CkAXkGmmu,aaaaa,bbbbb,ccccc
ugrjoJYbgz,aaaaa,bbbbb,ccccc
KO6U5DJwYB,aaaaa,bbbbb,ccccc
sha.sh

#!/bin/bash

if [ !-e $1 ]; then
    echo "$1 No such file."
    exit 1
fi

while COUNT=0 read line
do 
  # カウント
  COUNT=$(( COUNT + 1 ))
  # 切り出し
  COL1=$(echo $line| cut -d , -f 1)
  COL2_4=$(echo $line| cut -d , -f 2-4)
  # ハッシュ値生成
  SHA=$(echo -n $COL1 |openssl dgst -sha256)
  if [ $COUNT -gt 1 ];then 
    echo "$SHA","$COL2_4";
  # 1行目スキップ
  else 
    echo "$line";
  fi
done < $1

実行

bash sha.sh sample.csv

結果

カラム1,カラム2,カラム3,カラム4
b06ed88dcce303122ca92beb2af20c72aa3f34db64b20c94a70e6ecaeb7bf90e,aaaaa,bbbbb,ccccc
72a9253b280211fc1c1e62964797852b8d76cb7ecebf946ef82d4861a6866587,aaaaa,bbbbb,ccccc
d5a259a890b0dcf76ca8051914c2694c1db1fbbfdb983fd8fe547d4c58dfe3c6,aaaaa,bbbbb,ccccc
bcaaafae78828d81c2929f00d3dc8a02af2ca2c02abd9e5680fde64388f17e48,aaaaa,bbbbb,ccccc
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?