Reduce the number of lines by using Array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reduce the number of lines by using Array
# 8  
Old 01-10-2015
An attempt to stay close to your original script...
Code:
#!/bin/sh

declare -A AGES                         # associative name/ages array
NAMES=($(cat names.data | sort -u))     # unique names array
SIZE=0

function output {
echo
for x in ${NAMES[*]};
do
    echo "$x(${AGES[$x]})"
done
exit
}

function setname {
for x in ${NAMES[*]};
do
    AGES[$x]=$(grep -c $x names.data)
done
}

function listname {
num=0
for x in ${NAMES[*]};
do
    num=$(( $num + 1 ))
    printf "%d-%s(%s)" $num $x ${AGES[$x]}
    if [[ $(( $num % 4 )) -ne 0 ]]; then    # 4 column display
        printf "\t"
    else
        printf "\n"
    fi
    SIZE=$num
done
name;
}

function name {
echo
echo "Which name you want to change? "
read iname
[ $iname ] || output;    # No entry = exit
if [ $iname -gt $SIZE ] || [ $iname -lt 1 ]; then
    echo "Choice is out of range"
    listname;
fi
idx=$(( $iname - 1 ))   # array index count from zero
nam=${NAMES[$idx]}
echo $nam

read -p "Enter $nam: " age
AGES[$nam]=$age         # update AGES array
echo "$nam=(${AGES[$nam]})"

echo "Do you want another change?"
read anothchang
case $anothchang in
    [yY] | [yY][Ee][Ss] )
        listname;
        ;;
    [nN] | [nN][Oo] )
        output;         # display AGES array and exit
        ;;
    * )
        output;
        ;;
esac
}

echo "Hello"
setname;
listname;

exit

# eof #


Last edited by ongoto; 01-14-2015 at 03:10 PM.. Reason: catch out-of-range errors
This User Gave Thanks to ongoto For This Post:
# 9  
Old 01-12-2015
Hi ongoto
It does not likethese two lines:-

idx=$(( $iname - 1 )) # array index count from zero AGES[$nam]=$age # update AGES array
I am getting the following error :-
bad array subscript
# 10  
Old 01-12-2015
Quote:
Originally Posted by samsan
Hi ongoto
It does not likethese two lines:-

idx=$(( $iname - 1 )) # array index count from zero AGES[$nam]=$age # update AGES array
I am getting the following error :-
bad array subscript
You have to be careful not to enter a number that is out of range. (zero or greater than the size of the list)

I made a few changes to catch that error and another bug I discovered...
Code:
function name {
echo
echo "Which name you want to change? "
read iname
[ $iname ] || output;    # No entry = exit
if [ $iname -gt $SIZE ] || [ $iname -lt 1 ]; then
    echo "Choice is out of range"
    listname;
fi

Code:
function listname {
num=0
for x in ${NAMES[*]};
do
    num=$(( $num + 1 ))
    printf "%d-%s" $num $x
    if [[ $(( $num % 4 )) -ne 0 ]]; then    # 4 column display
        printf "\t"
    else
        printf "\n"
    fi  
    SIZE=$num
done

Code:
NAMES=($(cat names.data | sort -u))     # unique names array
SIZE=0

function output {
echo
for x in ${NAMES[*]};
do
    echo "$x(${AGES[$x]})"
done
exit
}


Last edited by ongoto; 01-12-2015 at 03:25 AM.. Reason: noted changes
This User Gave Thanks to ongoto For This Post:
# 11  
Old 01-14-2015
ongoto ,

I really appreciate your help , I am still getting the following error:-

Code:
$ ./test2
Hello
1-Jack(12)    2-Kim(22)    3-Nick(89)    4-Sam(78)
5-Smith(7)    
Which name you want to change? 
4
Enter : 21
./test2: line 49: AGES[$nam]: bad array subscript

Here is how the code like:-
Code:
#!/bin/sh

declare -A AGES                         # associative name/ages array
NAMES=($(cat list | sort -u))     # unique names array
SIZE=0
function output {
echo
for x in ${NAMES[*]};
do
    echo "$x(${AGES[$x]})"
done
exit
}

function setname {
for x in ${NAMES[*]};
do
    AGES[$x]=$(grep -c $x list)
done
}

function listname {
num=0
for x in ${NAMES[*]};
do
    num=$(( $num + 1 ))
    printf "%d-%s" $num $x
    if [[ $(( $num % 4 )) -ne 0 ]]; then    # 4 column display
        printf "\t"
    else
        printf "\n"
    fi
SIZE=$num
done
name;
}

function name {
echo
echo "Which name you want to change? "
read iname
[ $iname ] || output;    # No entry = exit
if [ $iname -gt $SIZE ] || [ $iname -lt 1 ]; then
    echo "Choice is out of range"
    listname;
fi

read -p "Enter $nam: " age
AGES[$nam]=$age         # update AGES array
echo "$nam=(${AGES[$nam]})"

# 12  
Old 01-14-2015
Nothing major.
It looks like part of your code (in red) is missing...
Code:
read iname
[ $iname ] || output;
if [ $iname -gt $SIZE ] || [ $iname -lt 1 ]; then
    echo "Choice is out of range"
    listname;
fi
idx=$(( $iname - 1 ))   # array index count from zero
nam=${NAMES[$idx]}
echo $nam

read -p "Enter $nam: " age
AGES[$nam]=$age         # update AGES array
echo "$nam=(${AGES[$nam]})"

Another difference I noticed is your name list is displaying ages in parentheses.
Code:
1-Jack(12)    2-Kim(22)    3-Nick(89)    4-Sam(78)
5-Smith(7)

I modified this line in my example to achieve that...
Code:
function listname {
num=0
for x in ${NAMES[*]};
do
    num=$(( $num + 1 ))
    printf "%d-%s(%s)" $num $x ${AGES[$x]}


Last edited by ongoto; 01-15-2015 at 02:09 AM..
This User Gave Thanks to ongoto For This Post:
# 13  
Old 01-15-2015
Excellent ongoto, thank you. it works now SmilieSmilieSmilieSmilieSmilieSmilieSmilie
# 14  
Old 01-15-2015
A more complete example doesn't reduce the size of the script much. Sea and Chubler_XL have the best solutions as far as I can see.
Good luck.

Last edited by ongoto; 01-17-2015 at 03:25 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print number of lines for files in directory, also print number of unique lines

I have a directory of files, I can show the number of lines in each file and order them from lowest to highest with: wc -l *|sort 15263 Image.txt 16401 reference.txt 40459 richtexteditor.txt How can I also print the number of unique lines in each file? 15263 1401 Image.txt 16401... (15 Replies)
Discussion started by: spacegoose
15 Replies

2. Programming

Code review: recursion in circular array, reduce two functions to one?

Hello, I think there's an easier way to do this but can't seem to recall but given an array of animals and an initial value is a random index in the array, here it's 3. 3,4,5,4,3,2,1,0,1,2,3,4,5,4,3,2,1,0... inifinite repeat a quick brute force solution i came up with was two functions, i... (6 Replies)
Discussion started by: f77hack
6 Replies

3. Shell Programming and Scripting

Grep lines for number greater than given number

Hello, I am newbie to bash scripting. Could someone help me with the following. I have log file with output as shown below **************************LOG************************* 11/20/2013 9:11:23.64 Pinging xx.xx.xx.xx with 32 bytes of data: 11/20/2013 9:11:23.64 Reply from xx.xx.xx.xx:... (4 Replies)
Discussion started by: meena_2013
4 Replies

4. Shell Programming and Scripting

Reduce the number of lines in script

Hello All, I have created a script which will show the errors from a log file in between a particular section. v1=$(sed -n "/Main Report/,/Main Report End/p" input | grep -i 'Unable to find' v2=$(sed -n "/Main Report/,/Main Report End/p" input | grep -i 'Unable to add' if then echo... (5 Replies)
Discussion started by: Vikram_Tanwar12
5 Replies

5. UNIX and Linux Applications

Looking to reduce the number a cpus available for SGE

Hey all Im looking to reduce the number of cpus available on a certain node in our cluster available for jobs using SGE. i.e. we have one node that has 24 cpus available for jobs on SGE, i would like to reduce that to 16. Thanks (1 Reply)
Discussion started by: olifu02
1 Replies

6. Shell Programming and Scripting

Largest number in array. Help!

I need to calculate the biggest number in array size n. Example: Users enter: 1 7 4 9 The biggest number is : 9 Simple but I'm really new on this on Shell/Bash! Anything will be helpful! Thanks! #!/bin/bash printf "\tEnter a list of numbers, with spaces: " read -a ARRAY BIG=$1... (5 Replies)
Discussion started by: Sundown
5 Replies

7. Shell Programming and Scripting

count the number of lines that start with the number

I have a file with contents similar to this. abcd 1234 4567 7666 jdjdjd 89289 9382 92 jksdj 9823 298 I want to write a shell script which count the number of lines that start with the number (disregard the lines starting with alphabets) (1 Reply)
Discussion started by: grajp002
1 Replies

8. Shell Programming and Scripting

Number lines of file and assign variable to each number

I have a file with a list of config files numbered on the lefthand side 1-300. I need to have bash read each lines number and assign it to a variable so it can be chosen by the user called by the script later. Ex. 1 some data 2 something else 3 more stuff which number do you... (1 Reply)
Discussion started by: glev2005
1 Replies

9. Shell Programming and Scripting

How to insert the number to an array

Hi I work in ksh88 and have a file which has several "set -A " statements: set -A PNUM_LSTM 2713 4124 2635 270 2529 2259 2214 set -A PNUM_LSTM $* for pn in ${PNUM_LSTM} etc... I need to add another number, lets say 555, to the first line ONLY so only the the first line will be updated... (2 Replies)
Discussion started by: aoussenko
2 Replies

10. UNIX for Dummies Questions & Answers

How to reduce multiple files into a specific number of files

Can anyone please let me know how do I reduce files into a specific number of files by cat'ing files? For example: 15 files must be reduced to 1 or 5 or 9 (possible values 1 to 14) (5 Replies)
Discussion started by: aryanbelank
5 Replies
Login or Register to Ask a Question