10 Replies - 1698 Views - Last Post: 17 March 2011 - 10:02 AM Rate Topic: -----

#1 Pandamonium  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 26-October 10

Bourne Shell script, Sorting and input.

Posted 16 March 2011 - 09:15 PM

Hi, I am trying to work on a script in bourne that takes an input file of peoples grades and sorts it based on the total.

I want to take the following

Name	Exam1	Exam2	Exam3
Greg	 63	     72	     70
Scott	 90	     95	     98
Emily	 81	     65	     89


and have the script add a new column with the total scores and sort the people by total:
like so:
Name	Exam1	Exam2	Exam3  total
Scott	  90	  95	   95      280
Emily	  81	  65	   89      235
Greg	  63	  72	   70      205


the input file is grades.txt
I dont know how to get the input file in bourne, or how to grab the values in the individual columns.
Any help would be greatly appreciated. I think I just need a few tips and push in the right direction :D.

Is This A Good Question/Topic? 0
  • +

Replies To: Bourne Shell script, Sorting and input.

#2 Pandamonium  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 26-October 10

Re: Bourne Shell script, Sorting and input.

Posted 16 March 2011 - 09:32 PM

I know I need to create an array using the input file. How do i assign each line to a different variable in an array?
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,465
  • Joined: 27-December 08

Re: Bourne Shell script, Sorting and input.

Posted 16 March 2011 - 09:37 PM

Did you even do a quick search for a tutorial? I did, and came up with this.
Was This Post Helpful? 0
  • +
  • -

#4 Pandamonium  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 26-October 10

Re: Bourne Shell script, Sorting and input.

Posted 16 March 2011 - 09:45 PM

I have actually been using that tutorial sporadically, however it does not explain how to import a line from a file into an array.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: Bourne Shell script, Sorting and input.

Posted 17 March 2011 - 05:31 AM

For even the most trivial things, you usually reach out to the rest of the utilities in the environment. Math, for example, isn't really supported in standard Bourne; you need to call something external.

Here's a quick and dirty:
#!/bin/sh

while read LINE; do
	echo $LINE
	ELE_NUM=0
	for WORD in $LINE; do
		echo $ELE_NUM"\t*"$WORD"*"
		ELE_NUM=`expr $ELE_NUM + 1`
	done
done < test.txt



Result:
Name Exam1 Exam2 Exam3
0	*Name*
1	*Exam1*
2	*Exam2*
3	*Exam3*
Greg 63 72 70
0	*Greg*
1	*63*
2	*72*
3	*70*
Scott 90 95 98
0	*Scott*
1	*90*
2	*95*
3	*98*
Emily 81 65 89
0	*Emily*
1	*81*
2	*65*
3	*89*



The shell doesn't really have arrays, but it does tokenize by white space.

I'd probably make a function for the line. Then, you get tokens as params. e.g.
#!/bin/sh

processLine() {
	echo "*"$2"*\t*"$3
}

while read LINE; do
	echo $LINE
	processLine $LINE
done < test.txt



Name Exam1 Exam2 Exam3
*Exam1*	*Exam2
Greg 63 72 70
*63*	*72
Scott 90 95 98
*90*	*95
Emily 81 65 89
*81*	*65



Shell scripting is a game where you figure out which base elements exist in the OS that you can leverage. You can make it a game about using only shell elements, but you'll be fooling yourself; they sneak in all over the place. So, first, you have to define your tool set. Then you can get to work.
Was This Post Helpful? 0
  • +
  • -

#6 Pandamonium  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 26-October 10

Re: Bourne Shell script, Sorting and input.

Posted 17 March 2011 - 06:02 AM

Thanks for the help, I was getting myself more and more confused.
few questions though. In the code, how do I tell it to read the names from grades.txt?
also how do I do addition and create a new column?
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: Bourne Shell script, Sorting and input.

Posted 17 March 2011 - 06:51 AM

Well, name is your first column, so you just print the first.

The rest are values. You can use `expr ` as already shown to add them up.

processLine() {
	echo "Name: "$1
	echo "Exam1: "$2
	echo "Exam2: "$3
	echo "Exam3: "$4
	echo ""
}


Was This Post Helpful? 0
  • +
  • -

#8 Pandamonium  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 26-October 10

Re: Bourne Shell script, Sorting and input.

Posted 17 March 2011 - 07:44 AM

cool, I understand how to do the adding and columns now. I am still a little confused as to how the script knows to pull the information from the grades.txt file.
also when trying to run the first part I get this error:
sh-3.2$ chmod +x test.sh
sh-3.2$ ./test.sh
'/test.sh: line 5: syntax error near unexpected token `do
'/test.sh: line 5: ` for WORD in $LINE; do
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: Bourne Shell script, Sorting and input.

Posted 17 March 2011 - 09:05 AM

Hmm... runs fine here.

Three possibilities I can think of:
  • The shebang, #!/bin/sh, isn't pointing to your shell.
  • What is at #!/bin/sh isn't actually a Bourne shell. ( Bash would be fine, it's a superset. )
  • Your OS doesn't have a "read" command. Recall I said shell scripts use OS elements, even if you don't notice.


Any normal *nix should come ready to go with a Bourne shell or compatible. If you have something like cygwin, make sure you have installed everything you need.
Was This Post Helpful? 0
  • +
  • -

#10 Pandamonium  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 26-October 10

Re: Bourne Shell script, Sorting and input.

Posted 17 March 2011 - 09:49 AM

Ok, it is nolonger giving errors, but when I use the script, nothing shows up in test.txt.
Was This Post Helpful? 0
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: Bourne Shell script, Sorting and input.

Posted 17 March 2011 - 10:02 AM

You're going the wrong direction. The file test.txt is being read. It's piping into the while block. Change it to your grades.txt or whatever.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1