Skip to content

Instantly share code, notes, and snippets.

@sipadan2003
Last active November 27, 2020 16:05
Show Gist options
  • Save sipadan2003/2ddc06a2901c1668e1a43b3a5cc97ccf to your computer and use it in GitHub Desktop.
Save sipadan2003/2ddc06a2901c1668e1a43b3a5cc97ccf to your computer and use it in GitHub Desktop.
Reem010
H He Li Be B C N O F Ne
H1 He1 Li1 Be1 B1 C1 N1 O1 F1 Ne1
H2 He2 Li2 Be2 B2 C2 N2 O2 F2 Ne2
H3 He3 Li3 Be3 B3 C3 N3 O3 F3 Ne3
Li He B C
Li4 He4 B4 C4
Li5 He5 B5 C5
Li6 He6 B6 C6
B H He Li Ne
B7 H7 He7 Li7 Ne7
B8 H8 He8 Li8 Ne8
B9 H9 He9 Li9 Ne9
BEGIN { FS="," }
# NR=1: 入力CSVの1行目のみ処理
NR==1 {
printf "---------------------\n", FILENAME
printf "---- Processing %s\n", FILENAME
printf "---------------------\n", FILENAME
# a.csvからヘッダ行のみ取り込む
readBaseCsv(headerNames)
# 入力CSVの各列の列番号を、入力CSVを元に決定
printf "---- Decide column numbers corresponding to original CSV\n"
for(i=1; i<=NF; i++){
for(j=1; j<=length(headerNames); j++){
if($i == headerNames[j]){
columnNumbers1[i] = j
printf "%s,", j
break
}
}
}
printf "\n"
# 入力CSVの各列をソートするために、列番号を振り直す
printf "---- Decide column numbers for sorting\n"
for(i=1; i<=length(columnNumbers1); i++){
# ソートするための列番号
count = 1
for(j=1; j<=length(columnNumbers1); j++){
if(columnNumbers1[i] > columnNumbers1[j]){
count++;
}
}
# 新しい配列にソート用の列番号を代入
columnNumbers2[i] = count
printf "%d,", columnNumbers2[i]
}
printf "\n"
# 別のAWKファイルを生成する
printf "---- Create new AWK file\n"
currentIndex = 1
str = "BEGIN{ FS=\",\" }\nNR>1 { print"
for(i=1; i<=length(columnNumbers2); i++){
for(j=1; j<=length(columnNumbers2); j++){
if(i == columnNumbers2[j]){
# スキップする分だけ「,」を追加
while((exists = columnExists(columnNumbers1, currentIndex)) == 0){
currentIndex = currentIndex + 1
str = str " " "\",\""
}
# ソート用列番号を追加
currentIndex = currentIndex + 1
str = str " $" j
# 「,」を追加(最終列は除く)
if(i < length(columnNumbers2)){
str = str " " "\",\""
}
}
}
}
# 残りの列を「,」で埋める
for(i=currentIndex; i<=length(headerNames); i++){
str = str " " "\",\""
}
str = str " }"
printf "%s\n", str
print str > "temp.awk"
}
############################################
# a.csv からヘッダを読み込み、配列を生成する
############################################
function readBaseCsv(result){
getline line < "a.csv"
split(line, headerNames, ",")
close("a.csv")
for(j=1; j<=length(headerNames); j++){
result[j] = headerNames[j]
}
}
##########################################################
# iで指定された列番号が配列colに存在するかどうかを判別する
##########################################################
function columnExists(cols, i){
for(z=1; z<=length(cols); z++){
if(i == cols[z]){
return 1
}
}
return 0
}
#!/bin/bash
awk -f reem010.awk b.csv
awk -f temp.awk b.csv > b1.csv
head -n1 a.csv > b2.csv
cat b1.csv >> b2.csv
awk -f reem010.awk c.csv
awk -f temp.awk c.csv > c1.csv
head -n1 a.csv > c2.csv
cat c1.csv >> c2.csv
head -n1 a.csv > last.csv
cat b1.csv c1.csv >> last.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment