Score:0

I am trying to get this file to show Author before the CSV which is the "," only the first name shows

cn flag
#!/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

david avatar
cn flag
thanks for the edit.
Raffa avatar
jp flag
Please read: https://askubuntu.com/help/someone-answers
Score:2
hr flag

The bash shell's set command is not the one you want in this context - it is used for setting the value of shell options and positional parameters.

Specifically, the command set INPUT=0 sets the value of the shell's first positional parameter, $1, to INPUT=0. Then set IFS=, replaces it with IFS=,. The correct assignments would simply be INPUT=0 and IFS=, respectively.

HOWEVER, the only place that the value of IFS appears to be significant is in your read command - and there you can set it locally i.e.

while IFS=, read -r author title isbn

so you don't need to set IFS=, elsewhere. You should also get into the habit of quoting variable expansions. So

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

done < Proj2.sorted
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.