Sponsored Content
Top Forums Shell Programming and Scripting Write a shell program to find the sum of alternate digits in a given 5-digit number Post 302094542 by rajnikanth.1912 on Monday 30th of October 2006 12:17:57 AM
Old 10-30-2006
You need to initialise the variables num and sum

#!/bin/ksh

a=12346
b=`expr length $a`
echo " length : $b"
typeset -i i=1
num=0
sum=0
while (( i <= $b ))
do
((num=$(echo "${a}" | cut -c "${i}") ))
if (( i == 1 )); then
printf "$num"
else
printf "+$num"
fi
((sum=sum + num))
((i=i+2))
done
printf "=${sum}\n"
~

Answer
-------
length : 5
1+3+6=10
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to find the sum of first n Fibonacci numbers

pls give me the solution for this i need it for my exam pls pls pls Shell script to find the sum of first n Fibonacci numbers (1 Reply)
Discussion started by: Kshitija
1 Replies

2. Programming

Find out 2^n+1 , where n is a 3 digit number

I have to write a c program which takes a 3 digit number n and calculates the value of (2^n)+1 and then determines the number is prime or not. I have tried to first calculate the value of 2^n and then adding one to it and then apply the logic of prime number. but the ultimate problem is that... (7 Replies)
Discussion started by: agrawal.prachi
7 Replies

3. Shell Programming and Scripting

Use match() in nawk to find digits in number

Hi, I just need to check whether number of digits in a phone number is 10 or not. If I am not wrong regex will be: {9} I have to use this inside nawk as this is a small portion of a big program. nawk ' BEGIN { RS="";FS=";"; regex="{9}"; } { for (i=1;i<=NF;i++) { if... (6 Replies)
Discussion started by: shekhar2010us
6 Replies

4. Shell Programming and Scripting

Find filenames with three digits and add zeros to make five digits

Hello all! I've looked all over the internet and this site and have come up a loss with an easy way to make a bash script to do what I want to do. I have a file with a naming convention as follows: 2012-01-18 string of words here 123.jpg 2012-01-18 string of words here 1234.jpg 2012-01-18... (2 Replies)
Discussion started by: Buzzman25
2 Replies

5. Shell Programming and Scripting

how to write a shell program for back up

Hello.. I want to take back up from server to local machine and i dont know how to do it using cron and sftp . I can do it by manually typing password in the terminal and taking the back up. How to create a 'backup.sh' file for doing all the back up process and shut down automatically after... (1 Reply)
Discussion started by: deepoos
1 Replies

6. Shell Programming and Scripting

Write a shell program with input

Hi, Here is my question: I want a shell script which I name as 'del', and can be used as del(string). when run del(string), it will delete several directories at different locations in my system,like: rm -fr /lustre/fs/scratch/user/$string rm -fr /home/user/$string rm -fr... (4 Replies)
Discussion started by: 1988PF
4 Replies

7. Shell Programming and Scripting

Find number of digits in a word

HI, Can you tell me how to find the number of digits in a word. $cat data.txt +123456ad 87645768 Output should be 6 8 (5 Replies)
Discussion started by: ashwin3086
5 Replies

8. Shell Programming and Scripting

How to find sum of any 'n' number of values from file matching target value?

I have a simple text file having payment amount value on each line. At the end of day 'n' number of payments created difference in amount that I need to match from this file. I have information about how many payments created difference and difference amount. Please help me to build shell... (3 Replies)
Discussion started by: swats007
3 Replies

9. Shell Programming and Scripting

How to write program that find winner who choose the smallest number. UNIX process?

In the game of “Unique”, multiple players privately choose an integer. They then reveal their choice. The winner is the player who chose the smallest unique number. The game is considered a draw if no unique integer was chosen. You would write a program that simulate such a game according to the... (1 Reply)
Discussion started by: dantesma
1 Replies

10. UNIX for Beginners Questions & Answers

I need to find in a file a list of number where last two digit end in a range

I all I am tryng to find a way to sort a list of number in a file by the value of last two digit. i have a list like this 313202320388 333202171199 373202164587 393202143736 323202132208 353201918107 343201887399 363201810249 333201805043 353201791691 (7 Replies)
Discussion started by: rattoeur
7 Replies
getcol(1)						      General Commands Manual							 getcol(1)

Name
       getcol - Extract specified columns from an ASCII table file

Synopsis
       getcol [-amv][-n num][-r lines][-s num] filename [column number range]

Description
       Extract specified columns from an ASCII table file

Options
       filename
	      Name  of a ASCII table file.  At least one of these must be present for any values to be printed.  If it is stdin or STDIN, an ASCII
	      table is expected as standard input.  If there is no input file, standard input is assumed.

       @filename
	      Name of a file containing a list of ASCII table files.  If this is present, any other  file  names  on  the  command  line  will	be
	      ignored.

       field range
	      Print  value  of	these  columns for the number of lines of the table specified by the -n argument after the skippiing the number of
	      lines specified by the -s argument.  A value of 0 causes the entire input line to be printed.

       -a     Sum all numeric columns selected, printing the sum on the line following the result.  Columns with  no  sum  are	filled	with  ___.
	      (Added in version 2.6.9)

       -b     Input is bar-separate table file

       -c     Add count of number of lines in each column at end

       -d <number>
	      Number of decimal places in f.p. output

       -e     Compute medians of selected columns

       -f     Print range of values in selected columns

       -h     Print Starbase tab table header

       -i     Input is tab-separate table file

       -k     Print number of columns on first line

       -l <number>
	      Number of lines to add to each line

       -m     Compute the means of all numeric columns selected, printing the mean on the line following the result (or the line following the sum
	      if -a is used).  Columns with no mean are filled with ___.  (Added in version 2.6.9)

       -n num Print selected columns for this many lines.  If not specified, all lines will be read after the number of lines specified by -s have
	      been skipped.

       -o     OR conditions insted of ANDing them

       -p     Print only sum, mmean, sigma, median, or range, not entries

       -r @listfile
	      -r  line	range  Print  columns from the lines specified as either the first nonzero number on each line of the file listfile or the
	      comma- and hyphen- delimitied range; i.e. 1-5,10-12 will print values from lines 1, 2, 3, 4, 5, 10, 11, and 12.  (added  in  version
	      2.6.12)

       -s num Skip this many line before starting to print values.  If not specified, no lines will be skipped.

       -t     Starbase (tab-separated) table output

       -v     Print more information about process.

       Web Page
	      http://tdc-www.harvard.edu/software/wcstools/getcol.html

Author
       Doug Mink, SAO (dmink@cfa.harvard.edu)

8 November 2001 						     WCSTools								 getcol(1)
All times are GMT -4. The time now is 07:49 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy