I need to do some math with the lines of a file and a number using bash. As in Ruby or python you can easily do string to number or viceversa. In bash all is untyped.
I have a file contain numbers like this: 1,2,3 I can iterate over it with that return exactly 2.
cat file.txt | sed 's/[^,]//g' | wc -c
I would like to store this result in a $variable, and the adding a fixed number ((3+1)). so e.g $total = 2 + 3 + 1 = 6. How can I store the result of unix command in a variable for further use?
number_of_elements = $( sed 's/[^,]//g' elements.txt | wc -c ) echo "elements are: $number_of_elements" #2 lines = $(( $number_of_elements + 3 + 1)) & echo "lines are: $lines " #2+3+1 cat file.txt | grep -A $lines "text " > output.txt #open file, grep the "text" and the 6 lines that come after.
Seems there are problems: - command not found for line 1 / 3 - and as a conseguence grep it doesn't work with $lines