Results 1 to 10 of 11
I'm still learning this and I'm very new to Linux in general, so I could use some help please. I created a text file in kwrite that looks just like ...
- 03-19-2007 #1Just Joined!
- Join Date
- Mar 2007
- Posts
- 3
Need some help writing a script in bash
I'm still learning this and I'm very new to Linux in general, so I could use some help please. I created a text file in kwrite that looks just like this:
1 50.00 2 Linux
2 200.00 3 Windows
3 99.99 2 Mac_OS
Now what I want to do with this file is read it into an array and output the the contents to look like this (with the total price of the inventory calculated on the last line):
ID PRICE QTY OS
1 50.00 2 Linux
2 200.00 3 Windows
3 99.99 2 Mac_OS
Total Price of Inventory: $
I don't think the columns will line up properly here, but you get the idea (column headings with the data below). I want to be able to print the output. The only thing I've done in scripting so far is the simple "Hello World" script. Help please?
- 03-19-2007 #2
Arrays kind of suck in bash, and read kind of sucks, and expr kind of sucks. But here is my solution:
Feel Free to question me. BTW, I think python is much more suited to tasks like this. I am just starting out python right now, but FWIU this type of stuff is what it excels at. Bash only supports one dimensional arrays, and loading them up is kind of a ***** with read. To get the whole file to load, I had to disable newline from ending the input, with the -d switch. However, as a by product of using -d, the array was populated with one element. The whole contents of the file were the one element. That's why I had to store that in a temporary array, and echo its contents to assign to the working array.Code:#!/bin/bash if [ $# == 0 ]; then printf "Usage: `basename $0` inventory-file\n" >&2 exit 1; fi read -d -a TMPARR < $1 INVARR=( `echo ${TMPARR}` ) COUNT=1 SUM=0 while [ $COUNT -lt ${#INVARR[*]} ]; do SUM=`python -c "print $SUM + ${INVARR[$COUNT]}"` COUNT=`expr $COUNT + 4` done printf "ID PRICE QTY OS:\n" COUNT=0 while [ $COUNT -lt ${#INVARR[*]} ]; do printf "${INVARR[$COUNT]} ${INVARR[`expr $COUNT + 1`]} ${INVARR[`expr $COUNT + 2`]} ${INVARR[`expr $COUNT + 3`]}\n" COUNT=`expr $COUNT + 4` done printf "Total Price of Inventory: $SUM\n"
- 03-20-2007 #3Linux User
- Join Date
- Aug 2006
- Posts
- 458
just one way to do it
output:Code:#!/usr/bin/bash total=`tr ',' '\n' < file | awk 'BEGIN {c = 0} { c=c + $2} END { print c}'` echo "ID PRICE QTY OS" > newfile tr ',' '\n' < file >> newfile echo "total price: $total" >> newfile
or using awk (there are better ways than this)Code:ID PRICE QTY OS 1 50.00 2 Linux 2 200.00 3 Windows 3 99.99 2 Mac_OS total price: 349.99
Code:#!/bin/bash awk -F ', ' 'BEGIN {c=0; print "ID PRICE QTY OS";} { printf ("%s\n%s\n%s\n",$1,$2,$3) split($1,one, " ") split($2,two, " ") split($3 , three ," ") total= one[2] +two[2] + three[2] print "total price: " , total } END { print "finsh" }' file
- 03-20-2007 #4
Yea mine doesn't work if it's in the first format, I assumed the second format for the input file.
I have a question for ghostdog. So you're cutting off the ,s and replacing with newlines with tr, but can you explain what that awk line does? I've always kind of avoided learning awk but it seems pretty powerful for this kind of thing.
- 03-20-2007 #5Linux User
- Join Date
- Aug 2006
- Posts
- 458
you mean this line: ?
Originally Posted by likwid
this awk line first "initialize" a counter 'c' to 0 using BEGIN. 'c' is used to add the values of the second field (which are the amount). { c= c + $2 } will add the second field of every line and assign each subtotal to 'c'. The find END will print the total value of 'c'.Code:awk 'BEGIN {c = 0} { c=c + $2} END { print c}'
My english is bad, hope you understand.
- 03-20-2007 #6
That's what I figured. I wasn't sure if it worked on a line at a time or not.
- 03-20-2007 #7Just Joined!
- Join Date
- Mar 2007
- Posts
- 3
thanks for the input
wow, thanks for help guys! I really needed the solution using arrays though, maybe you did use them and I just don't get it. I'm in the "Using Arrays in Bash" portion of my beginners guide to Linux.
- 03-20-2007 #8Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
The starter of the thread is still learning and a newbee in Linux.
If he/she is trying to learn scripting, he/she is not helped with these answers, right?
It's preferable to read some stuff first about shell scripting:
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
http://tldp.org/LDP/abs/html
Regards
- 03-21-2007 #9
Observing code is one of the best ways to learn. In my bloated script up there I make sure of arrays in a few different ways, which aren't always explained thoroughly in text. Tutorials and descriptions can only help out so much.
And the other script shows how you can condense all of that, without using arrays. I think this thread is pretty helpful to the OP, and me too of course
- 03-21-2007 #10Just Joined!
- Join Date
- Mar 2007
- Posts
- 3
Filenames?
In Likwid's code below, where would the filenames go? The Input file is called OS and the output file is called Inventory.
Code:#!/bin/bash if [ $# == 0 ]; then printf "Usage: `basename $0` inventory-file\n" >&2 exit 1; fi read -d -a TMPARR < $1 INVARR=( `echo ${TMPARR}` ) COUNT=1 SUM=0 while [ $COUNT -lt ${#INVARR[*]} ]; do SUM=`python -c "print $SUM + ${INVARR[$COUNT]}"` COUNT=`expr $COUNT + 4` done printf "ID PRICE QTY OS:\n" COUNT=0 while [ $COUNT -lt ${#INVARR[*]} ]; do printf "${INVARR[$COUNT]} ${INVARR[`expr $COUNT + 1`]} ${INVARR[`expr $COUNT + 2`]} ${INVARR[`expr $COUNT + 3`]}\n" COUNT=`expr $COUNT + 4` done printf "Total Price of Inventory: $SUM\n"


Reply With Quote