I currently have 2 bash command line strings I use to gather data needed for a certain task. I was trying to simplify and have only one command used to gather the data without using a script.

First I cat a file and pipe into a grep command and only display the integer value. I then copy and paste that value into an equation which will always be constant except for the grepped value from the first command.

  • 1st command: cat /proc/zem0 |grep -i isrs|grep -Eo '[0-9]+$'
  • 2nd command: echo $(( (2147483633-"**grep value**")/5184000 ))

I'm stumped as to how I can accomplish this. Any guidance on this would be greatly appreciated!


Here it is as one command:

echo $(( (2147483633 - $(grep -i isrs /proc/zem0 | grep -Eo '[0-9]+$') )/5184000 ))

How the simplification was done

First consider this pipeline:

cat /proc/zem0 |grep -i isrs`

This can be simplified to:

grep -i isrs /proc/zem0

Thus, the whole of the first command becomes:

grep -i isrs /proc/zem0 | grep -Eo '[0-9]+$'

The last change is to substitute the first command into the second using command substitution: $(...). Thus, we replace:

echo $(( (2147483633-"**grep value**")/5184000 ))


echo $(( (2147483633-$(grep -i isrs /proc/zem0 | grep -Eo '[0-9]+$'))/5184000 ))

One more simplification

If your grep supports perl-style regular expressions, such as GNU grep, then, as suggested by jimmij in the comments, one more simplification is possible:

echo $(( (2147483633-$(grep -Pio 'isrs.*?\K[0-9]+$' /proc/zem0))/5184000 ))
  • Even better to use only one grep with perl regexp: echo $(( (2147483633-"$(grep -Pio 'isrs.*?\K[0-9]+$' /proc/zem0)")/5184000 )) – jimmij Jan 26 '15 at 1:17
  • @jimmij Excellent. I added your idea to the answer. – John1024 Jan 26 '15 at 1:32
  • Thanks for the feedback, folks! This is very useful in helping me understand. – byron Jan 26 '15 at 3:15
  • Hey gents, I tried applying the above solution and am getting a syntax error. Any idea what's happening? -bash: (2147483633-"16463368")/5184000 : syntax error: operand expected (error token is ""16463368")/5184000 ") – byron Jan 26 '15 at 15:55
  • @byron Hmmm. I just revised the answer slightly. Try it now. – John1024 Jan 26 '15 at 17:56

Use of command substitution as answered by John1024 is be best way to solve this problem.

Alternatively, you can pipe your output to while read, which will assign each space-separated value to a variable of your choosing. This is usually used when your output has more than one variable you'd like to use.

cat /proc/zem0 |grep -i isrs|grep -Eo '[0-9]+$' | while read myvariable ; do echo $(( (2147483633-"${myvariable}")/5184000 )) ; done

while read is really useful for columnar data, inserting stuff into inventory, etc. E.g. if file1 content is:

host1 web nyc1 00-23-54-EE-10-5C
host2 web nyc2 00-23-54-EE-10-5D
host3 web nyc1 00-23-54-EE-10-5E

then you can do

cat file1 | while read host ip role dc mac ; do
    cobbler system add --name=${host} --ip-address=${ip} --netmask= --mac-address=${mac} --comments="${role} ${dc}"

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.