[1. What you want to do](#What you want to do) [2. Code introduction](# Code introduction) [3. Execution example](# Execution example)
The contents of the file are acquired and processed in order from the top. In this article, I will introduce a shell script of a loop statement that acquires the contents of the name data (name.txt) in the file from above, assigns a serial number, and converts it to csv (name.csv).
** Current folder status **
State before execution
$ cat name.txt
Taro Suzuki
Jiro Suzuki
Noriko Yamada
Junko Nishida
main.sh
#!/bin/sh
cnt=1
cat name.txt | while read data
do
echo ${cnt},${data} >> name.csv
#Increment of cnt variable
cnt=`expr ${cnt} + 1`
done
main.run sh
sh main.sh
Post-execution status
$ cat name.csv
1,Taro Suzuki
2,Jiro Suzuki
3,Noriko Yamada
4,Junko Nishida
Recommended Posts