我试图让此文件在 CSV 之前显示作者,即“,”仅显示名字

我试图让此文件在 CSV 之前显示作者,即“,”仅显示名字
#!/bin/bash
# this is the input
#Clive Cussler,Ghost Ship,9780399167315
#Clive Cussler,Bootlegger,9780399167295
#James Patterson,Invisible,9780316405345
#James Patterson,Gone for Now,9781455515845
#James Rollins,Map of Bones,9780062017855
#Michael Connely,Lincoln Lawyer,9781455516345
#David Baldacci,The Escape,9781478984345
set INPUT=0

set IFS=,

    

echo 'how do you want to sort the file?'
echo '1 for Author'
echo '2 for Title'
echo '3 ISBN'
read -r INPUT

case $INPUT in

        1) sort -t "," -k 1 project2.input > Proj2.sorted;
           outfile=project2.author.out;;

        2) sort -t, -k  project2.input > Proj2.sorted;
           outfile=project2.title.out;;


        3) sort -t, -k  project2.input > Proj2.sorted;
           outfile=project2.isbn.out;;

        *) echo 'invaled not 1-3'; exit;;

esac

# Setting up echo and header information here.
echo "******************************************" > "$outfile"
echo "* CIS 129 Project 2                      *" >> "$outfile"
echo "*          6/19/2021                     *" >> "$outfile"
echo "******************************************" >> "$outfile"



while read -r author title isbn
do
       echo $author

done < Proj2.sorted

答案1

bash shell 的set命令不是你在这个上下文中想要的 - 它用于设置 shell 的值选项位置参数

具体来说,该命令set INPUT=0将 shell 的第一个位置参数 的值设置$1INPUT=0。然后set IFS=,将其替换为IFS=,。正确的赋值应该是INPUT=0IFS=,

然而,唯一一个看起来IFS重要的地方是在你的read命令中——你可以在本地设置它,即

while IFS=, read -r author title isbn

所以你不需要IFS=,在其他地方设置。你还应该养成引用变量扩展的习惯。所以

while IFS=, read -r author title isbn
do
       echo "$author"

done < Proj2.sorted

相关内容