Defining variable problem


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Defining variable problem
# 1  
Old 07-14-2014
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:
Code:
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
Code:
count_C= $(grep -c C mols)

and also this:
Code:
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,
Code:
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!
# 2  
Old 07-14-2014
As has been said many times before on these forums, shell variables are not expanded inside single quotes. Try changing:
Code:
sed -i '7,7 s/$/   $count_C    $count_O    $count_H/g' input

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

# 3  
Old 07-14-2014
Thanks a lot Don!So I tried this as you said:
Code:
#!/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! Smilie
# 4  
Old 07-14-2014
Quote:
Originally Posted by saleheen
Thanks a lot Don!So I tried this as you said:
Code:
#!/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! Smilie
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.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 07-15-2014
I'm sorry Don. Here's my "input"
Quote:
Ruthenium 444 surface
1.00000000000000
10.8727200000000011 0.0000000000000000 0.0000000000000000
0.0000000000000000 9.4160599999999999 0.0000000000000000
0.0000000000000000 0.0000000000000000 21.5000000000000000
Ru
64
Selective dynamics
Direct
My counts should return the values and I want add those values at the 7th line. This is what it should look like-
Quote:
Ruthenium 444 surface
1.00000000000000
10.8727200000000011 0.0000000000000000 0.0000000000000000
0.0000000000000000 9.4160599999999999 0.0000000000000000
0.0000000000000000 0.0000000000000000 21.5000000000000000
Ru
64 2 1 3
Selective dynamics
Direct
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. :/
And also, here's the mols file from where it should count the values,
Quote:
H 0.1823565363480026 0.4604101472535956 0.4005758974370104
H 0.2570433232476637 0.6231167698839984 0.3815017011047572
H 0.2135086777493862 0.5877276165535853 0.4600917787525729
C 0.2493254339588567 0.5395125405888599 0.4170749011908661
C 0.3737714706179839 0.4730560514301286 0.4295765297889743
O 0.4284154246482464 0.4351728266366453 0.3710697347999991
Thanks again!

Last edited by saleheen; 07-15-2014 at 12:11 AM.. Reason: Clarification
# 6  
Old 07-15-2014
Code:
#!/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

This User Gave Thanks to itkamaraj For This Post:
# 7  
Old 07-15-2014
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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Problem defining a struct

I have the following code and getting the compilation errors baseLib/DynBaseObj.h:80: error: expected constructor, destructor, or type conversion before ‘(' token baseLib/DynBaseObj.h:89: error: expected constructor, destructor, or type conversion before ‘(' token baseLib/DynBaseObj.h:101:... (0 Replies)
Discussion started by: kristinu
0 Replies

2. UNIX for Dummies Questions & Answers

defining variable in .profile

In root dir i have created a .profile file and added variable and assigned a path to it: a = '/dir/dir' export a but when i echo (echo $a) the path or use this variable the value or path not getting displayed. i tried executing the .profile and logging out and logging in, didnt workout. am... (1 Reply)
Discussion started by: abhi_n123
1 Replies

3. Shell Programming and Scripting

alias defining problem in .cshrc file

Hi folks, I'm trying to define the following command as alias in .cshrc file: ls -ltr | grep ^d | awk '{print $9}' | xargs du -hs I defined it as the following: alias nirdirs '`ls -ltr | grep "^d" | awk "{print \\$9}" | xargs du -hs`' I've got the following error when I've run the alias:... (7 Replies)
Discussion started by: nir_s
7 Replies

4. Shell Programming and Scripting

defining a variable using multiple "entries/columns" within a line using read

Hello All, I am trying to figure out how to use the read command. I have a txt file that has rows looking something like this: 1 2 3 4 5 6 7 8 9 where each number represents an entry of various characters deliminated by tabs. My goal is to set entries 2-7 as a variable/string that I... (3 Replies)
Discussion started by: Torinator
3 Replies

5. Shell Programming and Scripting

defining variables

Hey all, I was wondering if someone would take a look at this script I'm working on. I don't know if i have the syntax correct for my variables and if the for loop is written correctly. any assistance would be greatly appreciated. #!/usr/bin/bash ###########################################... (12 Replies)
Discussion started by: em23
12 Replies

6. UNIX for Dummies Questions & Answers

Defining EDITOR Variable - Tru64

Hi, I have to edit a lot of partitions soon and wanted to do so with EMACS rather than VI. The "disklabel -e -r disknumber" command picks up VI as it's defined, but how do I set "-e" so it uses EMACS instead please? Thanks...! (2 Replies)
Discussion started by: Bagel08
2 Replies

7. Solaris

Problem defining remote printers on solaris 10

Good morning, I have a server with solaris 10 that I want to intall remote printers. I started lpsched deamon without problems with command: svcadm enable application/print/server I want to install printers that are defined locally on print server, so: lpadmin -p <device> -s <print... (2 Replies)
Discussion started by: bonovox
2 Replies

8. Shell Programming and Scripting

Sorting/Filed Defining/Output problem

need a little help with a few tid bits. I wrote a script that checks the resoluion of image files and writes them out to a file then sorts the resolutions by largets Width. Then take the sorted files information and toss all the 835 widths into a seperate file. Ignore the redundancies its a... (1 Reply)
Discussion started by: TiredOrangeCat
1 Replies

9. UNIX for Dummies Questions & Answers

Defining Variables

I'm trying to define a variable named sin I already have a variable named cos, which has the value "hello" I want sin to have the value of "hellothere", so sin would be something like sin = $cos & "there" but I'm not sure that I know the syntax. Can anyone help? :confused: (4 Replies)
Discussion started by: sailorliones
4 Replies

10. UNIX for Dummies Questions & Answers

defining a variable as a number or character?

I am writing a script that needs to accept numbers into a variable by prompting and using the code read num case $num in 000) break ;; *) I am fairly new to unix and am loving the versatility of the language but need a little shove in the right... (1 Reply)
Discussion started by: noobian
1 Replies
Login or Register to Ask a Question