0

I am trying to copy a specific line which is in a file, into a specific folder name. And this folder must have the equal name than this line.

I have 3 folders :

  • /home/my_username/bin : which contains inside of it a file, which is my script file : switch_menu
  • /home/student : which contains inside of it, my 2 students files : Rick and Josh And inside of each of both files, there are all the subjects names followed by the respectives score they received for it.
  • /home/result : which contain inside of it, my 2 subjects lessons files : MATH and HISTORY And inside of each of both files, there are the students name followed by the score they received for this specific lesson subject.

Here are my codes for a better understanding :

1. My script file | Inside of /home/my_username/bin  

#! /bin/bash

title="Switch Menu"
prompt="Please pick an option :"

echo "$title"
PS3="$prompt"

select opt in "${options[@]}"
do
 case $REPLY in
              1) cat /home/result/MATH >>/home/student/Rick;;
              2) echo "Goodbye !"; break;;
              3) echo "The option $REPLY does not exit !";;
 esac
done

2. My student files | Inside of /home/student

-rw-r--r-- 1 username username 0 month date time Rick
-rw-r--r-- 1 username username 0 month date time Josh

3. My result files | Inside of /home/result

-rw-r--r-- 1 username username 0 month date time MATH
-rw-r--r-- 1 username username 0 month date time HISTORY

cat MATH | Gives me 

Rick 7
Josh 14

cat HISTORY| Gives me 

Rick 18
Josh 11

Right now, if I run my script : switch_menu,

And type the choice 1, it will simply copy all the content of my MATH file, inside my RICK file.

Though in my MATH file, there is also the score for Josh

It shouldn't be like it.

It should be this : once I click on "1", it copies the score for each of my respectives students.

So that for this example, it should copy :

  • Rick 7, inside of the Rick file.
  • Josh 14, inside of the Josh file.
0

If I understand you correctly, you need to add the grades from a result file (e.g. math.txt) to the corresponding students file (e.g. rick.txt, josh.txt).

The way, that you are doing it right now cat /home/result/MATH >>/home/student/Rick;; is incomplete. You need to find a way to filter and process the information in the lines read from the math file. I don't want to give you a complete solution, because it sounds like homework to me. But I will give you an examples:

sh$ cat math.txt Mike 12 Romeo 8 Peter 13 Following command

sh$ grep "Mike" math.txt Mike 12 would only return the one line.

Further you can split the lines into single columns.

sh$ grep "Mike" math.txt | cut -d' ' -f2 12 would only return the 2nd column with the grade.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.