csvfile=$1
ymd=$(date '+%Y%m%d%H%M%S')
lineprime=1
cp ${csvfile} ${csvfile}_bk
fileline=$(grep -c "." ${csvfile}_bk)
outputfile=${ymd}_shaping.txt
while read line; do
currentline=$lineprime
currentlinekanma=$(sed -n ''${currentline}p'' ${csvfile}_bk | awk -F ',' '{print NF}')
tmpprime=0
for prime in $(seq 1 $currentlinekanma); do
currentarray_index=$(sed -n ''${currentline}p'' ${csvfile}_bk | awk -F [,] '{print $'$prime'}')
if [[ $(echo $currentarray_index | grep "." | wc -l) -eq 1 ]]; then
tmpprime=$prime
fi
done
tmpprime2=$(sed -n ''${currentline}p'' ${csvfile}_bk | cut -d ',' -f -${tmpprime})
sed -i ''${currentline}''d ${csvfile}_bk
if [[ $currentline -eq $fileline ]]; then
sed -i '$a '${tmpprime2}'' ${csvfile}_bk
else
sed -i ''${currentline}'i '${tmpprime2}'' ${csvfile}_bk
fi
lineprime=$((++lineprime))
done <${csvfile}_bk
lineprime=1
while read line; do
lineprime=$((++lineprime))
currentline=$lineprime
if [[ $currentline -gt $fileline ]]; then
break
fi
beforeline=$(expr $currentline - 1)
currentlinekanma=$(sed -n ''${currentline}p'' ${csvfile}_bk | awk -F ',' '{print NF}')
currentlinekanmaprime=$(sed -n ''${currentline}p'' ${csvfile}_bk | awk -F [,] '{print $'$currentlinekanma'}')
beforelinekanma=$(sed -n ''${beforeline}p'' ${csvfile}_bk | awk -F ',' '{print NF}')
beforelinekanmaprime=$(sed -n ''${beforeline}p'' ${csvfile}_bk | awk -F [,] '{print $'$beforelinekanma'}')
if [[ $currentlinekanma == 1 ]]; then
continue
fi
currentarray=()
if [[ $beforelinekanma -ge 1 ]]; then
beforearray=$(sed -n ''${beforeline}p'' ${csvfile}_bk | awk -F [,] '{print $0}' | sed 's/,/"" /g')
count=0
for t in ${beforearray[@]}; do
currentarray+=($t)
count=$((++count))
if [[ ${count} -eq $(expr ${currentlinekanma} - 1) ]]; then
currentarray+=($currentlinekanmaprime)
break
fi
done
else
currentarray+=($currentlinekanmaprime)
fi
printnum=$(echo ${currentarray[*]} | sed 's/""//g' | sed 's/ /,/g')
sed -i ''${currentline}''d ${csvfile}_bk
if [[ $currentline -eq $fileline ]]; then
sed -i '$a '${printnum}'' ${csvfile}_bk
else
sed -i ''${currentline}'i '${printnum}'' ${csvfile}_bk
fi
done <${csvfile}_bk
cat ${csvfile}_bk
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
csvfile=$1
ymd=$(date '+%Y%m%d%H%M%S')
lineprime=1
cp ${csvfile} ${csvfile}_bk
fileline=$(grep -c "." ${csvfile}_bk)
outputfile=${ymd}_shaping.txt
while read line; do
currentline=$lineprime
currentlinekanma=$(sed -n ''${currentline}p'' ${csvfile}_bk | awk -F ',' '{print NF}')
tmpprime=0
for prime in $(seq 1 $currentlinekanma); do
currentarray_index=$(sed -n ''${currentline}p'' ${csvfile}_bk | awk -F [,] '{print $'$prime'}')
if [[ $(echo $currentarray_index | grep "." | wc -l) -eq 1 ]]; then
tmpprime=$prime
fi
done
tmpprime2=$(sed -n ''${currentline}p'' ${csvfile}_bk | cut -d ',' -f -${tmpprime})
sed -i ''${currentline}''d ${csvfile}_bk
if [[ $currentline -eq $fileline ]]; then
sed -i '$a '${tmpprime2}'' ${csvfile}_bk
else
sed -i ''${currentline}'i '${tmpprime2}'' ${csvfile}_bk
fi
lineprime=$((++lineprime))
done <${csvfile}_bk
$ cat test.txt
1.java
,method_1_1
,method_1_2
2.java
,method_2_1
,method_2_2
3.java
,method_3_1
,method_3_2
4.java
,method_4_1
,method_4_2
$ bash substring_hierarchy_csv.sh test.txt csv
1.java
1.java,method_1_1
1.java,method_1_2
2.java
2.java,method_2_1
2.java,method_2_2
3.java
3.java,method_3_1
3.java,method_3_2
4.java
4.java,method_4_1
4.java,method_4_2
while read line
do
delimiter_num=$(echo ${line}|awk -F ',' '{print NF}')
#末尾のデータを取る
suffixdata=$(echo ${line}|awk '{print $NF}')
if [[ ${delimiter_num} = 1 ]]; then
echo ${suffixdata}
else
suffixdata=$(expr ${delimiter_num} - 1)
cat <(seq 1 ${suffixdata}|sed -E "s/[0-9]+/,/g"|tr "\n" " "|sed -E "s/ | $//g") <(echo ${line}|awk -F [,] '{print $NF}')
fi
done < ${1}
$ cat hoge
1.java
1.java,method_1_1
1.java,method_1_2
2.java
2.java,method_2_1
2.java,method_2_2
3.java
3.java,method_3_1
3.java,method_3_2
4.java
4.java,method_4_1
4.java,method_4_2
$ bash substring_hierarchy_modoshi_csv.sh hoge
1.java
,method_1_1
,method_1_2
2.java
,method_2_1
,method_2_2
3.java
,method_3_1
,method_3_2
4.java
,method_4_1
,method_4_2