Hi All,
I am facing a problem in reporting and the problem is like
I have a spool file with n number of lines I want to substract second line from the first line. And replace the first line with the above output.
Eg: Ist Line: Books 23929 12 2650 2172 2601 6552 4919 5033
2nd Line: Sold 3345 2 771 225 388 500 1449 0
Now I need the first line As:
Ist Line: Books 20584 10 1879 1947 2213 6052 3470 5033
Please reply soon Its urgent
here's an alternative in Python:
line1 = "Books 23929 12 2650 2172 2601 6552 4919 5033"
line2 = "Sold 3345 2 771 225 388 500 1449 0"
num1 = line1.split()[1:]
num2 = line2.split()[1:]
total = [int(num1[count]) - int(num2[count]) for count in range(0,len(num1)) ]
prnit total
output:
[/code]
[20584, 10, 1879, 1947, 2213, 6052, 3470, 5033]
[/code]
Thanks a lot for your inputs..
But as I am working with shell script its not giving proper results.
I am working with teh appraoch u have given.
Here is the shell script that will do almost close to what you want:
I assume that samplefile.text contains two lines as you had mentioned earlier. File soldfile is created containg the result you wanted.
#!/bin/bash
# Please note: This is a bash script
books_line=`grep -i books samplefile.text | cut -d ' ' -f 2-`
sold_line=`grep -i sold samplefile.text | cut -d ' ' -f 2-`
books=(`echo $books_line`) # create an array containing book values
sold=(`echo $sold_line`) # create an array containing sold values
echo "sold" > soldfile # create file 'soldfile' and write word "sold" into it
for (( i = 0 ; i < ${#books [@]} ; i++ ))
do
echo "book =${books[$i]}= sold =${sold[$i]}="
diff=`expr ${books[$i]} - ${sold[$i]}`
echo "===$diff==="
echo -n "$diff " >> soldfile
done
echo " " >> soldfile # write a newline into the file
Dhruva
September 21, 2006, 6:11am
5
try this.here book_data.txt is the data file.
max_col=`sed '1q' book_data.txt | awk -F" " '{print NF}'`
col_first=`sed '1q' book_data.txt`
col_second=`sed -n '2p' book_data.txt`
set -A book $col_first
set -A sold $col_second
set -A pos
pos[0]=Books
i=1
while [ $i -lt $max_col ]
do
pos[$i]=`expr ${book[$i]} - ${sold[$i]}`
i=`expr $i + 1`
done
echo "${pos[*]}">temp_file
tail +2 book_data.txt>>temp_file
mv temp_file book_data.txt