Visit Our UNIX and Linux User Community


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
# 1  
Old 01-08-2015
Reduce the number of lines by using Array

I have the following code to count the number of how many times the name occurred in one file. The code is working fine and the output is exactly what I want. The problem is the real code has more than 50 names in function listname which cause function name to have more than 50 case ,and function setname more than 50 lines.

is there a way to rewrite function name and function setname in short code? someone told me I can use Array but not sure how. My goal is to reduce the number of lines.

Code:
function listname {

echo "List of Name:-"

  echo "1-Kim    2-Lee     3-Jack     4-Sam"
  echo "5-Nick    6-Smith"

name;
 }

function name {
  echo "Which name ou want to change?"
        read iname
 
        case $iname in
        1 ) echo "Kim"

               read -p "Enter Kim: " Kim
               echo "Kim=($Kim)"
        ;;
        2 ) echo "Lee"

               read -p "Enter Lee: " Lee
               echo "Lee=($Lee)"
     
        ;;

        3 ) echo "Jack"

               read -p "Enter Jack: " Jack
               echo "Jack=($Jack)"
        ;;

        4 ) echo "Sam"

               read -p "Enter Sam: " Sam
               echo "Sam=($Sam)"
        ;;
        5 ) echo "Nick"

               read -p "Enter Nick: " Nick
               echo "Nick=($Nick)"
        ;;
        6 ) echo "Smith"

               read -p "Enter Smith: " Smith
               echo "Smith=($Smith)"

        ;;
   
        * ) echo "You did not enter a number"
                echo "between 1 and 6." 

esac
 echo "Do you want another change?"
 read anothchang
     case $anothchang in
        [yY] | [yY][Ee][Ss] )
        listname;
             ;;
       [nN] | [nN][Oo] )
        setname;

}


function setname {      
               [ -z "$Kim" ] && Kim="cat /tmp/list | grep -c Kim"
               [ -z "$Lee" ] && Lee="cat /tmp/list | grep -c Lee"
               [ -z "$Jack" ] && Jack="cat /tmp/list | grep -c Jack"
               [ -z "$Sam" ] && Sam="cat /tmp/list | grep -c Sam"
               [ -z "$Nick" ] && Nick="cat /tmp/list | grep -c Nick"
               [ -z "$Smith" ] && Smith="cat /tmp/list | grep -c Smith"
output;
    }

 function output {

 echo "Kim ($Kim)  
   Lee($Lee) 
   Jack($Jack) 
   Sam($Sam) 
   Nick($Nick)   
   Smith($Smith)"
  }
echo "Hello"
listname;

# 2  
Old 01-08-2015
I don't think above code is working fine. Having corrected several logical and syntactical errors, I'm still not sure I understand what you are doing - you have a list of six name constants, and six variables named identically. The latter are used to hold either a character string alternative name to be read from stdin (user terminal) or, if that is missing, an integer word count for, again, text constants. In the end, if an alternate name is entered, you output that, otherwise the word count for the respective text constant in a /tmp file. On top, your recursive use of functions is somewhat unorthodox.

Assuming you have a recent bash, and names in a file like
Code:
Kim
Lee
Jack
Sam
Nick
Smith

, try this
Code:
mapfile -tO1 ORIG <file
mapfile -tO1 NAMES <file
select NAME in ${NAMES[@]}
        do [ $REPLY -eq 0 ] && break
           read -p "Enter ${NAMES[$REPLY]} " NAMES[$REPLY]
        done
for i in ${!ORIG[@]}
        do printf "%s(" ${ORIG[$i]}
           [ "${ORIG[$i]}" == "${NAMES[$i]}" ] && 
                printf "%s)\n" $(grep -c ${NAMES[$i]} /tmp/list) ||
                printf "%s)\n" "${NAMES[$i]}"
        done

to reproduce what I think you want to achieve.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 01-08-2015
The use of numbers suggests you want to use a simple vector array, but why not skip the numbers and use just names and an associative array?
This User Gave Thanks to DGPickett For This Post:
# 4  
Old 01-08-2015
Might be an option if the OP's intentions were clear...
This User Gave Thanks to RudiC For This Post:
# 5  
Old 01-08-2015
i apologize for the misunderstanding , this is not the really code. I just wrote the above code to gave you in idea of what I am trying to do.
I fixed the code above , and now should run. Let say here is what inside my list file:-

Code:
cat list 
Sam
Sam
Kim
Sam
Kim
Jack
Nick
Nick
Smith
Smith

when I run the code :-

Code:
List of Name:-
1-Kim    2-Lee     3-Jack     4-Sam
5-Nick    6-Smith
Which name ou want to change?
1
Kim
Enter Kim: 23
Kim=(23)
Kim (23)  
   Lee(0) 
   Jack(1) 
   Sam(3) 
   Nick(2)   
   Smith(2)

The code is running fine , I just want it to know if there is any way to rewrite it in short way.

here is the updated code
Code:

function listname {

echo "List of Name:-"

  echo "1-Kim    2-Lee     3-Jack     4-Sam"
  echo "5-Nick    6-Smith"

name;
 }

function name {
  echo "Which name ou want to change?"
        read iname

        case $iname in
        1 ) echo "Kim"

               read -p "Enter Kim: " Kim
               echo "Kim=($Kim)"
        ;;
        2 ) echo "Lee"

               read -p "Enter Lee: " Lee
               echo "Lee=($Lee)"

        ;;

        3 ) echo "Jack"

               read -p "Enter Jack: " Jack
               echo "Jack=($Jack)"
        ;;

        4 ) echo "Sam"

               read -p "Enter Sam: " Sam
               echo "Sam=($Sam)"
        ;;
        5 ) echo "Nick"

               read -p "Enter Nick: " Nick
               echo "Nick=($Nick)"
        ;;
        6 ) echo "Smith"

               read -p "Enter Smith: " Smith
               echo "Smith=($Smith)"

        ;;

        * ) echo "You did not enter a number"
                echo "between 1 and 6."

esac
setname;
}


function setname {
               [ -z "$Kim" ] && Kim=`cat list | grep -c Kim`
               [ -z "$Lee" ] && Lee=`cat list | grep -c Lee`
               [ -z "$Jack" ] && Jack=`cat list | grep -c Jack`
               [ -z "$Sam" ] && Sam=`cat list | grep -c Sam`
               [ -z "$Nick" ] && Nick=`cat list | grep -c Nick`
               [ -z "$Smith" ] && Smith=`cat list | grep -c Smith`
output;
    }

 function output {

 echo "Kim ($Kim)
   Lee($Lee)
   Jack($Jack)
   Sam($Sam)
   Nick($Nick)
   Smith($Smith)"
  }
echo "Hello"
listname;

# 6  
Old 01-09-2015
Here is something to get started, to chew on Smilie

Code:
#!/bin/bash
#
#
#	Variables
#
	declare -A AGE				# Create an associative array
	NAMES="Kim Lee Jack Sam Nick Smith"	# List with names
#
#	Fill the array
#
	select name in Back $NAMES;do		# Select an entry of the list NAMES
		[[ $name = Back ]] && break	# Exit the select-loop / break out
		
		echo "Hello $name, what is your age?"
		read age
		AGE[$name]=$age			# Set the arrays entry '$name' to value '$age'
	done
#
#	Output the content
#
	for n in $NAMES
	do
		echo "$n is ${AGE[$n]} years old"
	done
	echo "Have a nice day :)"

Hope this helps
This User Gave Thanks to sea For This Post:
# 7  
Old 01-09-2015
Thank you sea , it helped me with other code Smilie , but it is not what I am looking here

Previous Thread | Next Thread
Test Your Knowledge in Computers #313
Difficulty: Easy
The programming language Python is based on a modified version of JavaScript.
True or False?

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

Featured Tech Videos