1

I have two column in one file :

1 2  
3 5  
4 8  
9 18  
3 5  
4 19

I want to divide each element of first column to each element of second column and want to print that number too. for example:

1,2,1/2,  
1,5,1/5,  
1,8,1/8,  
1,18,1/18,  
1,19,1/19,  
3,5,3/5,  
4,19,4/19,  
3,2,3/2,  
3,5,3/8,  
3,19,3/19 and so on...   

Please help me How can i proceed ?

  • You need to read each column into an array, then have a nested loop to print the results. – glenn jackman Nov 18 '19 at 13:36
1

GNU Parallel

parallel echo "{1},{2}" :::: <(cut -d' ' -f1 file) :::: <(cut -d' ' -f2 file) |
awk -F, '{ print $1,$2,$1"/"$2,$1/$2 }' OFS=, OFMT='%.2g'

Output:

1,2,1/2,0.5
1,5,1/5,0.2
1,8,1/8,0.12
1,18,1/18,0.056
1,5,1/5,0.2
1,19,1/19,0.053
3,2,3/2,1.5
3,5,3/5,0.6
3,8,3/8,0.38
3,18,3/18,0.17
3,5,3/5,0.6
3,19,3/19,0.16
4,2,4/2,2
4,5,4/5,0.8
4,8,4/8,0.5
4,18,4/18,0.22
4,5,4/5,0.8
4,19,4/19,0.21
9,2,9/2,4.5
9,5,9/5,1.8
9,8,9/8,1.1
9,18,9/18,0.5
9,5,9/5,1.8
9,19,9/19,0.47
3,2,3/2,1.5
3,5,3/5,0.6
3,8,3/8,0.38
3,18,3/18,0.17
3,5,3/5,0.6
3,19,3/19,0.16
4,2,4/2,2
4,5,4/5,0.8
4,8,4/8,0.5
4,18,4/18,0.22
4,5,4/5,0.8
4,19,4/19,0.21
| improve this answer | |
  • how to run this script – daizy123 Nov 19 '19 at 3:55
  • @daizy123: replace file with your input file – Thor Nov 19 '19 at 8:52
  • it is working. one more question. if my columns are not equal means one contain 1000 data second column contain 567 data . because if data in columns are not equal it is not working – daizy123 Nov 19 '19 at 9:02
  • and suppose I have three column but I want third column to print as it is. How change in it. – daizy123 Nov 19 '19 at 9:05
  • @daizy123: look it up in the cut manual page – Thor Nov 19 '19 at 9:37
0

This bash script snippet reads a text file called numbers.txt as formatted in your question and outputs a CSV, separated by colons, each line in the format:

Dividend;divisor;fraction;decimal

For the decimal calculation, it uses the tool bc.

#!/bin/bash
declare -a Dividend Divisor
while read -r Line; do
  if [ -n "$Line" ]; then
    Dividends+=("${Line%% *}")
    s="${Line#* }"
    Divisors+=("${s%% *}")
  fi
done < test.txt
for dividend in "${Dividends[@]}"; do
  for divisor in "${Divisors[@]}"; do
    echo "$dividend;$divisor;$dividend/$divisor;$(bc <<<"scale=5; $dividend/$divisor")"
  done
done

How does it work?

It reads in the entire file into the two arrays Dividends and Divisors and iterates over them in nested loops (full Cartesian product). In this way, each dividend is divided by each divisor.

| improve this answer | |
0

Python 3

rows = open("file.txt", "r").read().split("\n")
for r in rows[:-1]:
    r = r.split(" ")
    print("%s,%s,%s/%s,%.2f" % (r[0], r[1], r[0], r[1], int(r[0])/int(r[1])))

For file.txt:

1 2
3 5
4 8
9 18
3 5
4 19

you will get:

1,2,1/2,0.50
3,5,3/5,0.60
4,8,4/8,0.50
9,18,9/18,0.50
3,5,3/5,0.60
4,19,4/19,0.21
| improve this answer | |
0

With bash brace-expansion and awk

c1="{$(cut -d' ' -f1 file | paste -sd,)}"
c2="{$(cut -d' ' -f2 file | paste -sd,)}"
echo echo $c1,$c2                     | 
bash                                  | 
tr ' ' '\n'                           |
awk -F, '{ print $0, $1"/"$2, $1/$2 }' OFS=, OFMT='%.2g'

Output:

1,2,1/2,0.5
1,5,1/5,0.2
1,8,1/8,0.12
1,18,1/18,0.056
1,5,1/5,0.2
1,19,1/19,0.053
3,2,3/2,1.5
3,5,3/5,0.6
3,8,3/8,0.38
3,18,3/18,0.17
3,5,3/5,0.6
3,19,3/19,0.16
4,2,4/2,2
4,5,4/5,0.8
4,8,4/8,0.5
4,18,4/18,0.22
4,5,4/5,0.8
4,19,4/19,0.21
9,2,9/2,4.5
9,5,9/5,1.8
9,8,9/8,1.1
9,18,9/18,0.5
9,5,9/5,1.8
9,19,9/19,0.47
3,2,3/2,1.5
3,5,3/5,0.6
3,8,3/8,0.38
3,18,3/18,0.17
3,5,3/5,0.6
3,19,3/19,0.16
4,2,4/2,2
4,5,4/5,0.8
4,8,4/8,0.5
4,18,4/18,0.22
4,5,4/5,0.8
4,19,4/19,0.21
| improve this answer | |
  • where to give file name? I have wrote down this script in one file. After that I made that file executable. where to give input file name like Glyc.txt in script. I am running this script using ./executable script – daizy123 Nov 19 '19 at 4:03
  • @daizy123: replace file with your input file – Thor Nov 19 '19 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.