Defining variable problem

Hi

I'd say I'm having this weird problem where my script isn't taking the value off a variable or printing it. My code is like this:

set count_C= `grep -c C mols`
set count_H= `grep -c H mols`
set count_O= `grep -c O mols`

sed -i '7,7 s/$/   $count_C    $count_O    $count_H/g' input 

It doesn't work. It prints $count_C instead of the value. I've tried various ways, even echo $count_C doesn't return my value. I tried

count_C= $(grep -c C mols)

and also this:

set count_C= $(grep -c C mols)

but with no result.

Strange thing is I tried the following in the terminal instead of my script and it worked,

set count_C= `grep -c C mols`
echo $count_C

But the same thing doesn't work in my script.Instead of echoing, it only shows a blank space. In the end I'd like to know how I would use my variables in sed to insert that value in a specific line.
Thanks a lot!

As has been said many times before on these forums, shell variables are not expanded inside single quotes. Try changing:

sed -i '7,7 s/$/   $count_C    $count_O    $count_H/g' input

to:

sed -i "7s/$/   $count_C    $count_O    $count_H/g" input

Thanks a lot Don!So I tried this as you said:

#!/bin/bash
set count_C=`grep -c C mols`
set count_H=`grep -c H mols`
set count_O=`grep -c O mols`
sed -i "7s/$/   $count_C    $count_O    $count_H/g" input

still not working! :frowning:

The g flag at the end of the substitute command shouldn't have any effect, but your three counts should have been added to the end of line 7 in the file named input .
Saying it doesn't work, without showing us what it did do and without showing us what you were trying to do makes it very hard for us to figure out what went wrong. Give us a hint at what happened and what you wanted!

Show us the original contents of input .

Show us the contents of input after you ran your script.

Show us what you wanted input to look like after you run your script.

I'm sorry Don. Here's my "input"

My counts should return the values and I want add those values at the 7th line. This is what it should look like-

The numbers obviously can vary depending upon the count. The input file remains just the same as before after running the script. No change happens. :confused:
And also, here's the mols file from where it should count the values,

Thanks again!

#!/bin/bash
count_C=$(grep -c C mols)
count_H=$(grep -c H mols)
count_O=$(grep -c O mols)
value="${count_C} ${count_O} ${count_H}"
awk -v value="$value" 'NR==7{$0=$0" "value}1' input

Thanks a lot itkamaraj! It worked! Would you be kind enough the awk part? And how do I make it permanent? It's showing the changes in terminal but not in the file.

awk -v value="$value" 'NR==7{$0=$0" "value}1' input > /tmp/output
mv /tmp/output input

-v - define variable for awk
value=$value - value is a variable name which holds the value of $value. For understanding purpose, make it as v=$value # v is the variable which holds the value of $value

NR = give the number of records being processed.
NR==7 when awk reads the 7th line, we are changing the 7th line value as "7th line value + $value" and for printing all the lines i added 1 in the end. which is equivalent of {print}

Awesome man! Thanks! :smiley: