问题
I have a file of having multiple rows. Using that original file I am creating one more files but in that new file i am taking only few columns of the original files.
What I have to do is, instead of just picking few columns and pasting in new files, I need to create sha2 of first column and paste in new file as plain value also as well as sha2 value.
Hope I am clear.
This is the awk command I am using to do the same.
awk -F '|' -v OFS='|' -v var="10|" '(NR - 1) != 0 {$2=var$2; print $2,$3,$4,$5} (NR - 1) == 0 {print} ' $line > $subName$i$output
INPUT
2|0001001010000026316|531849|1150|101|01111991|00919323739251 |VIJAYPANDEY1191@GMAIL.COM |VIJAY PANDEY |PART OF GROUND FLOOR & BASEMENT |SHOPPER STOP SV ROAD ANDHERI WEST |LANDMARK-ERSTWHILE CRASSWORD BOOK STORE |MUMBAI |400058
EXPECTED OUTPUT REQUIRES SHA2 VALUE OF 2ND COLUMN AND TO APPEND AS THE LAST COLUMN IN THE SAME ROW
10|0001001010000026316|531849|1150|101|2e16abd9f3e3e368210b11faa5bfebdb6e001034b58cc9ad1c689dfd1f7eeacd
回答1:
Try this :
awk -F"|" -v var="10" '
NR==1;
NR>1{
"echo "$2"|sha256sum" | getline shaoutput;
split(shaoutput, sha, " ");
print var, $2, $3, $4, $5, sha[1]
}' OFS="|" file
Output :
10|0001001010000026316|531849|1150|101|2e16abd9f3e3e368210b11faa5bfebdb6e001034b58cc9ad1c689dfd1f7eeacd
I prefer to use NR==1 and NR>1 as it is more readable.
NR==1; is ok, no need to add {print}
For NR>1, I use sha256sum to generate the sha as awk does not have any function to do that (to my knowledge). I save the output in shaoutput variable, clean the output using split, then print what is needed.
I prefer not to store the output delimiter inside the var variable.
Using commas inside print will make awk use the OFS variable as delimiter.
Edited
As suggested by Ed Morton, an improved solution :
awk -v var="10" '
BEGIN{
FS=OFS="|"
}
NR==1;
NR>1{
shaoutput="";
cmd="echo \047" $2 "\047 | sha256sum" ;
if ( (cmd | getline line) > 0 ){
shaoutput=line
close(cmd)
}
split(shaoutput, sha, " ");
print var, $2, $3, $4, $5, sha[1];
}' file
回答2:
With GNU awk you can get away with setting NF although Ed is not going to like it:
awk '{ NF=5; "echo " $2" | sha256sum | cut -d\\\ -f1" | getline $(NF+1) } 1' FS='|' OFS='|'
You can accomplish the same with fewer portability issues with paste and cut:
paste -d'|' <(cut -d'|' -f1-5 infile) <(cut -d'|' -f2 infile | sha256sum | cut -d' ' -f1)
来源:https://stackoverflow.com/questions/53760800/awk-command-to-create-sha2-of-individual-column-and-paste-into-new-file