Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just 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?

  2. #2
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    Arrays kind of suck in bash, and read kind of sucks, and expr kind of sucks. But here is my solution:

    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"
    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.

  3. #3
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    just one way to do it
    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
    output:
    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
    or using awk (there are better ways than this)
    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

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    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.

  6. #5
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by likwid
    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.
    you mean this line: ?
    Code:
    awk 'BEGIN {c = 0} { c=c + $2} END { print c}'
    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'.
    My english is bad, hope you understand.

  7. #6
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    That's what I figured. I wasn't sure if it worked on a line at a time or not.

  8. #7
    Just 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.

  9. #8
    Linux 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

  10. #9
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    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

  11. #10
    Just 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"

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •