Multiply variable ranges with Bash brace expansion - bash

I've a question extending the code in this question: Can you multiply two variable ranges in Bash using brace expansion (not seq) and not using loops?
This is what I've tried so far
Work out how variable boundary ranges work (finally, a good use of eval):
$ echo {1..10}
1 2 3 4 5 6 7 8 9 10
$ boundary=10
$ echo {1..$boundary}
{1..10}
$ eval echo {1..$boundary}
1 2 3 4 5 6 7 8 9 10
But how can you multiply two variable boundary ranges?
$ echo $(({1..10}*{1..10}))
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
$ boundary=10
$ echo $(({1..$boundary}*{1..$boundary}))
bash: {1..10}*{1..10}: syntax error: operand expected (error token is "{1..10}*{1..10}")
$ eval echo $(({1..$boundary}*{1..$boundary}))
bash: {1..10}*{1..10}: syntax error: operand expected (error token is "{1..10}*{1..10}")

this seems to work, just escaped the $ and [] to delay their evaluation (so that they are echoed, then evaluated)
eval echo \$\[{1..$boundary}*{1..$boundary}\]
That said I now need to go lookup what $[] does ;-)
Second answer, with non deprecated $[] syntax (but two evals)
eval eval echo "\$\(\("{1..$boundary}*{1..$boundary}"\)\)"
or
eval eval echo \\\$\\\(\\\({1..$boundary}*{1..$boundary}\\\)\\\)

Related

Multiplication Table using neste for loops

I am trying to produce a square-formatted multiplication table with the output at the end using code below:
def multiplicationTable(maxValue):
for i in range(1, maxvalue):
for j in range(1, maxvalue):
print(("{:6d}".format(i * j,)), end='')
print()
print(multiplicationTable(1)
print(multiplicationTable(5))
print(multiplicationTable(10))
1
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
I get an error:
File "", line 7
print(multiplicationTable(5))
^
SyntaxError: invalid syntax
print(multiplicationTable(1) is missing a closing ).
You are using maxValue (with capital V) in your function definition while using maxvalue (with small v) in the function body.
Here is the new version:
def multiplicationTable(maxvalue): # maxvalue, not maxValue
for i in range(1, maxvalue+1):
for j in range(1, maxvalue+1):
print(("{:6d}".format(i * j,)), end='')
print()
multiplicationTable(1)
multiplicationTable(5)
multiplicationTable(10)
EDIT 1: Changed range(1, maxvalue) to range(1, maxvalue+1)
EDIT 2: Changed print(multiplicationTable(n)) to multiplicationTable(n)

Pythagorean Theorem in bash as a function

I am trying to display the Pythagorean Theorem in bash for my son - which should be easy. I need it in function. However the theorem a2 + b2 = c2 is just not making sense here. Don't know what I am doing wrong.
#!/bin/bash
read side_a side_b
hypo=$(( (side_a*side_a) + (side_b*side_b) ))
echo "side: $side_a side: $side_b hypotenuse: $hypo"
$ /tmp/hypo
5 5
side: 5 side: 5 hypotenuse: 50
time to switch to awk
$ awk '{print "side:",$1,"side:",$2,"hypotenuse:",sqrt($1^2+$2^2)}'
3 4
side: 3 side: 4 hypotenuse: 5
$1 and $2 are the input fields, the rest should read trivially.
With little more effort, you can generate the integer solutions as well...
$ awk 'BEGIN{for(i=1;i<=10;i++) for(j=1;j<i;j++) print 2*i*j, i^2-j^2, i^2+j^2}'
4 3 5
6 8 10
12 5 13
8 15 17
16 12 20
24 7 25
10 24 26
20 21 29
30 16 34
40 9 41
12 35 37
...

Remove rows that have a specific numeric value in a field

I have a very bulky file about 1M lines like this:
4001 168991 11191 74554 60123 37667 125750 28474
8 145 25 101 83 51 124 43
2985 136287 4424 62832 50788 26847 89132 19184
3 129 14 101 88 61 83 32 1 14 10 12 7 13 4
6136 158525 14054 100072 134506 78254 146543 41638
1 40 4 14 19 10 35 4
2981 112734 7708 54280 50701 33795 75774 19046
7762 339477 26805 148550 155464 119060 254938 59592
1 22 2 12 10 6 17 2
6 136 16 118 184 85 112 56 1 28 1 5 18 25 40 2
1 26 2 19 28 6 18 3
4071 122584 14031 69911 75930 52394 89733 30088
1 9 1 3 4 3 11 2 14 314 32 206 253 105 284 66
I want to remove rows that have a value less than 100 in the second column.
How to do this with sed?
I would use awk to do this. Example:
awk ' $2 >= 100 ' file.txt
this will only display every row from file.txt that has a column $2 greater than 100.
Use the following approach:
sed '/^\w+\s+([0-9]{1,2}|[0][0-9]+)\b/d' -E /tmp/test.txt
(replace /tmp/test.txt with your current file path)
([0-9]{1,2}|[0][0-9]+) - will match either digits from 0 to 99 OR a digits with leading zero (ex. 012, 00982)
d - delete the pattern space;
-E(--regexp-extended) - Use extended regular expressions rather than basic regular expressions
To remove matched lines in place use -i option:
sed -i -E '/^\w+\s+([0-9]{1,2}|[0][0-9]+)\b/d' /tmp/test.txt

Replace repeated elements in a list with unique identifiers

I have a list like the below:
1 . Fred 1 6 78 8 09
1 1 Geni 1 4 68 9 34
2 . Sam 3 4 56 6 89
3 . Flit 2 4 56 8 34
3 4 Dog 2 5 67 8 78
3 . Pig 2 5 67 2 21
(except the real list is 40 million lines long).
There are repeated elements in the second column (i.e. the ".")
I want to replace these with unique identifers (e.g. ".1", ".2", ".3"...".n")
I tried to do this with a bash loop / sed combination, but it didn't work...
Failed attempt:
for i in 1..4
do
sed -i "s_//._//."$i"_"$i""
done
(Essentially, I was trying to get sed to replace each n th "." with ".n", but this didn't work).
Here's a way to do it with awk (assuming your file is called input:
$ awk '$2=="."{$2="."++counter}{print}' input
1 .1 Fred 1 6 78 8 09
1 1 Geni 1 4 68 9 34
2 .2 Sam 3 4 56 6 89
3 .3 Flit 2 4 56 8 34
3 4 Dog 2 5 67 8 78
3 .4 Pig 2 5 67 2 21
The awk program replaces the second column ($2) by a string formed by concatenating . and a pre-incremented counter (++counter) if the second column was exactly .. It then prints out all the columns it got (with $2 modified or not) ({print}).
Plain bash alternative:
c=1
while read -r a b line ; do
if [ "$b" == "." ] ; then
echo "$a ."$((c++))" $line"
else
echo "$a $b $line"
fi
done < input
Since your question is tagged sed and bash, here are a few examples for completeness.
Bash only
Use parameter expansion. The second column will be unique, but not sequential:
i=1; while read line; do echo ${line/\./.$((i++))}; done < input
1 .1 Fred 1 6 78 8 09
1 1 Geni 1 4 68 9 34
2 .3 Sam 3 4 56 6 89
3 .4 Flit 2 4 56 8 34
3 4 Dog 2 5 67 8 78
3 .6 Pig 2 5 67 2 21
Bash + sed
sed cannot increment variables, it has to be done externally.
For each line, increment $i if line contains a ., then let sed append $i after the .
i=0
while read line; do
[[ $line == *.* ]] && i=$((i+1))
sed "s#\.#.$i#" <<<"$line"
done < input
Output:
1 .1 Fred 1 6 78 8 09
1 1 Geni 1 4 68 9 34
2 .2 Sam 3 4 56 6 89
3 .3 Flit 2 4 56 8 34
3 4 Dog 2 5 67 8 78
3 .4 Pig 2 5 67 2 21
you can use this command:
awk '{gsub(/\./,c++);print}' filename
Output:
1 0 Fred 1 6 78 8 09
1 1 Geni 1 4 68 9 34
2 2 Sam 3 4 56 6 89
3 3 Flit 2 4 56 8 34
3 4 Dog 2 5 67 8 78
3 5 Pig 2 5 67 2 21

How can I make a multiplication table using bash brace expansion? So far I have this: echo $[{1..10}*{1..10}]

I am trying to learn bash at a deeper level, and I decided to make a multiplication table. I have the functionality with the statement :
echo $[{1..10}*{1..10}]
but that gives me the following output:
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
Is there any way to format this output like the following using only 1 statement (i can figure out how to do this with loops, but that's no fun :p )
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Is it even possible to do in one statement, or would I have to loop?
Use this line for a nice output without using loops:
echo $[{1..10}*{1..10}] | xargs -n10 | column -t
Output:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Update
As a logical next step, I asked here if this multiplication table can have a variable range. With this help, my answer works with a variable ($boundary) range and stays quite readable:
boundary=4; eval echo $\[{1..$boundary}*{1..$boundary}\] | xargs -n$boundary | column -t
Output:
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
Also note that the $[..] arithmetic notation is deprecated and $((...)) should be used instead:
boundary=4; eval eval echo "$\(\({1..$boundary}*{1..$boundary}\)\)" | xargs -n$boundary | column -t
The printf built-in repeats its format as many times as necessary to print all arguments, so:
printf '%d %d %d %d %d %d %d %d %d %d\n' $[{1..10}*{1..10}]
If you want to avoid repeating the %d bit, it's trickier.
printf "$(echo %$[{1..10}*0]d)\\n" $[{1..10}*{1..10}]
In production code, use a loop.

Resources