addition of values in row


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting addition of values in row
# 1  
Old 05-07-2012
Error addition of values in row

file content are like this

Code:
sam,22,29,23,24,25,26,22

i want to add the values from column 3 (fix column no) to as per user input say up to column 8 (variable as per user)
can we do this without using "awk" for each column (as number of columns are variable as per user input )


Thanks in advance
# 2  
Old 05-07-2012
awk takes shell variables.
Code:
last_col=7
awk -v L=$last_col  '{sum=0; for(i=3; i<=L; i++) {sum+=$i}; print sum}' infile

Otherwise in shell you use arrays. What shell do you have?
# 3  
Old 05-07-2012
Code:
# ./justdoit file
please give the your value(s)= test ,   tess121 , unix 12 1999999
sam,22,29,test,tess121,unix,12,1999999,23,24,25,26,22

Code:
#!/bin/bash
fix=3;x=1;fs=$(grep -o '[,:;.*|]' $1|sort|uniq)
if [[ $(echo "$fs"|wc -l) -gt 1 ]] ; then
echo "a lot of FS" ; exit 1; fi
read -p "please give the your value(s)= " v
add="$(echo "$v"|sed 's/[0-9A-Za-z][A-Za-z0-9]*[^,]\|[A-Za-z0-9][A-Za-z0-9]*[^,]/&,/g;s/ //g;s/,\+/,/g;s/,$//')"
cut="$(cut -d"$fs" -f$x $1)";(( fix++ ));>new
while [[ ! -z "$cut" ]] ; do (( x++ ))
printf "%s%c" "$cut" "$fs">>new
cut="$(cut -d"$fs" -f$x $1)"
if [ $x -eq $fix ] ; then
printf "%s%c" "$add" "$fs">>new
fi;done<$1;sed 's/'$fs'$\|^'$fs'//g' new

regards
ygemici
This User Gave Thanks to ygemici For This Post:
# 4  
Old 05-07-2012
i use
/bin/sh shell
# 5  
Old 05-07-2012
Quote:
Originally Posted by sagar_1986
i use
/bin/sh shell
try this?
Code:
#!/bin/sh
fix=3;x=1;fs=`sed 's/.*\([,:;.*|]\).*/\1/' $1|sort|uniq`
if [ `echo "$fs"|wc -l` -gt 1 ] ; then
echo "a lot of FS" ; exit 1; fi
printf "%s" "please give the your value(s)= " ;read v
add="`echo "$v"|sed 's/[0-9A-Za-z][A-Za-z0-9]*[^'$fs']/&'$fs'/g;s/ //g;s/'$fs'\+/'$fs'/g;s/'$fs'$//'`"
cut="`cut -d"$fs" -f$x $1`";fix=`expr $fix + 1`;>new
while [ ! -z "$cut" ] ; do x=`expr $x + 1`
printf "%s%c" "$cut" "$fs">>new
cut="`cut -d"$fs" -f$x $1`"
if [ $x -eq $fix ] ; then
printf "%s%c" "$add" "$fs">>new
fi;done<$1;
sed 's/'$fs'$//g;s/^'$fs'//g' new

# 6  
Old 05-07-2012
Here is a basic alternative POSIX shell script you could try. I hope it is readable:

Code:
max=$1                                           # set max to the first parameter of the script
IFS=,						 # set the internal field separator to a comma
while read line                                  # process each line
do
  i=0                                            # reset counter
  tot=0                                          # reset total
  for val in $line                               # for each value on the line
  do
    i=$(( i+1 ))                                 # i=i+1
    if [ $i -ge 3 ] && [ $i -le $max ]; then     # if 3 <= i <= max
      tot=$(( tot + val ))                       # add val to tot
    fi
  done
  echo $tot                                      # print total
done < infile                                    # read for file "infile"



--
If you need to do more processing in the script, use
Code:
oldIFS=$IFS

at the start and
Code:
IFS=$oldIFS

at the end of this

Last edited by Scrutinizer; 05-07-2012 at 06:34 PM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sum specified values (columns) per row

Hello out there, file.txt: comp51820_c1_seq1 42 N 0:0:0:0:0:0 1:0:0:0:0:0 0:0:0:0:0:0 3:0:0:0:0:0 0:0:0:0:0:0 comp51820_c1_seq1 43 N 0:0:0:0:0:0 0:1:0:0:0:0 0:0:0:0:0:0 0:3:0:0:0:0 0:0:0:0:0:0 comp51820_c1_seq1 44 N 0:0:4:0:3:1 0:0:1:9:0:0 10:0:0:0:0:0 0:3:3:2:2:6 2:2:2:5:60:3... (16 Replies)
Discussion started by: pathunkathunk
16 Replies

2. Shell Programming and Scripting

Move values from column to row

Hello guys. Please can you help me with this.. Thanks in advance:b: Input file 2134 6371 N 2150 6371 M 2166 6371 S 2138 6417 N 2154 6417 M 2170 6417 S 2157 6603 N 2173 6603 M 2189 6603 S desired uotput 6371 2134N 2150M 2166S 6417 2138N 2154M 2170S... (2 Replies)
Discussion started by: jiam912
2 Replies

3. Shell Programming and Scripting

Averaging each row with null values

Hi all, I want to compute for the average of a file with null values (NaN) for each row. any help on how to do it. the sample file looks like this. 1.4 1.2 1.5 NaN 1.6 1.3 1.1 NaN 1.3 NaN 2.4 1.3 1.5 NaN 1.5 NaN 1.2 NaN 1.4 NaN I need to do a row-wise averaging such that it will sum only... (14 Replies)
Discussion started by: ida1215
14 Replies

4. Shell Programming and Scripting

Keep 3 values in each row

Hi, I have n number of values like 1 2 3 4 I want the output like 1 2 3 4 5 6 - - - - - - Please help me on this:wall: (4 Replies)
Discussion started by: cns1710
4 Replies

5. Shell Programming and Scripting

dynamic values in a row

hi i have an input file in which there are diffrent values for xxxx,yyyyyy,zzzzzzz how can i arrange the dynamic values of x,y&z in a row. input file: xxxxx 1 yyyyyy 4 yyyyyy 5 zzzzzzzz 7 yyyyyy 13 zzzzzzzz 7 zzzzzzzz 6 yyyyyy 14 yyyyyy 12 zzzzzzzz 4 yyyyyy 4 yyyyyy 5 yyyyyy 6... (6 Replies)
Discussion started by: dodasajan
6 Replies

6. Shell Programming and Scripting

Arrange values of a column in row

HI, I need to arrange values of a colum in row. e.g. input file : Alpha<>123 AAAA<>6754 Beta<>456 BBBB<>63784 CCC<>783 Gama<>789 Alpha<>555 AAAA<>6754 BBBB<>63784 Beta<>666 CCC<>783 Gama<>888 (9 Replies)
Discussion started by: The_Archer
9 Replies

7. Shell Programming and Scripting

merge files with same row values

Hi everyone, I'm just wondering how could I using awk language merge two files by comparison of one their row. I mean, I have one file like this: file#1: 21/07/2009 11:45:00 100.0000000 27.2727280 21/07/2009 11:50:00 75.9856644 25.2492676 21/07/2009 11:55:00 51.9713287 23.2258072... (4 Replies)
Discussion started by: tonet
4 Replies

8. Shell Programming and Scripting

Sum of values coming in a row

Hi, my requirement is to sum values in a row. eg: input is: sum,value1,value2,value3,.....,value N Required Output: sum,<summation of N values> Please help me... (5 Replies)
Discussion started by: MrGopal666
5 Replies

9. Shell Programming and Scripting

How to insert data befor some field in a row of data depending up on values in row

Hi I need to do some thing like "find and insert before that " in a file which contains many records. This will be clear with the following example. The original data record should be some thing like this 60119827 RTMS_LOCATION_CDR INSTANT_POSITION_QUERY 1236574686123083rtmssrv7 ... (8 Replies)
Discussion started by: aemunathan
8 Replies

10. Shell Programming and Scripting

Unix addition ( Row wise)

Hi I have a file like a,1 b,2 d,3 a,2 b,3 c,7 Result Desired: a,3 b,5 d,3 c,7 i.e on the bases of 1st field the addition is done of the 2nd field and result printed out. (3 Replies)
Discussion started by: gauravgoel
3 Replies
Login or Register to Ask a Question