0

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

closed as too broad by Thomas Dickey, GAD3R, Wildcard, techraf, Eric Renouf Oct 10 '16 at 11:10

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

1

Edit II: You can NOT have spaces in Bash when you assign a variable. This (from your post):

number_of_elements = $( sed 's/[^,]//g' elements.txt | wc -c )

...will not work. But this will:

number_of_elements=$( sed 's/[^,]//g' elements.txt | wc -c )

Remove the spaces before and after the = sign. Please read @heemayl's well written (and upvoted) answer on how to use command substitution!


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?

Something like this:

myVar=2
total=$(($myVar+2+3+1))
echo $total
8

Here, the variable $myVar (containing the integer 2) is used, and then we add the integers 2, 3 and 1 which then are saved in a new variable $total.

2

You need command substitution, $():

variable=$(sed 's/[^,]//g' file.txt | wc -c)

Now you can get the value the variable refers by using $variable. Note that, it is almost always a good idea to quote the variable while expanding to avoid unexpected effect (word splitting and pathname expansion), so better use "$variable".

Note that, i have removed the cat file.txt as sed takes filename(s) as argument.

  • Thank you for your tips! I need to learn deeper unix command – heisen Oct 10 '16 at 10:30
1
number_of_elements = $( sed 's/[^,]//g' elements.txt | wc -c )

Would try and run the number_of_elements command with = and the result of split+glob applied to the output of the wc command as arguments. In Bourne-like shells, scalar variable assignments are with var=value with no space character around the = (the var also has to be literal and be a valid variable name).

Also note that that sed command would remove all but the ,, newline and bytes not forming part of valid characters in elements.txt.

If you want to count the , characters only, you'd do:

count=$(tr -cd , < elements.txt | wc -c)

For , and newline:

count=$(tr -cd ',\n' < elements.txt | wc -c)

(though beware that with some wc implementation, $count may have some leading or trailing blanks).

To add some number:

count=$(($(tr -cd , < elements.txt | wc -c) + 1))

The content of $((...)) is treated as if inside "...": parameter expansion, arithmetic expansion and command substitution are still performed. To strip the extra blanks mentioned above, you can actually do:

count=$(($(... | wc -c)))

instead of:

count=$(... | wc -c)

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