setting a variable by searching within a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting setting a variable by searching within a file
# 1  
Old 08-25-2009
setting a variable by searching within a file

Hi,

I am trying to set a variable to be used in later scripting, and am having some difficulty. I want to look in a file called scan.info and find the line that says "variable ok". Then I want to cut the number at the beginning of that line and assign that number as a variable so that later commands can refer back to ${variable}. I've tried both:

variable = $( grep "variable ok" scan.info | cut -c2-3 )
set variable = $( grep "variable ok" scan.info | cut -c2-3 )

I know that the part in parenthesis works when I type it into the terminal, but when I try to run it in a script with the set variable aspect added, it says:

Illegal variable name.

The part I'm most confused about is that the same command line works in a script used by a coworker, but not in my script.

Any ideas? Thanks!
# 2  
Old 08-25-2009
Doing yours I got a 0 as the variable using the below file.

Here is how to do it:

Code:
me@hostname:~/tmp> cat tmp.sh
#!/bin/sh

var=$(grep "variable ok" scan.info | cut -d' ' -f 1)
echo $var
me@hostname:~/tmp> cat scan.info
10 variable ok
20 testing
30 test
me@hostname:~/tmp>

This way is assuming that the number you want is always at the beginning and there is always going to be a space right after it.
# 3  
Old 08-25-2009
I tried that way, and it still didn't work. I'm using csh, could that be why?

My scan.info file is set up like:

1 test ok 512 512 3
2 testing ok 64 64 1
3 variable error 56 64 1
4 variable ok 56 64 1
etc

So I want to set alpha to the number 4. I tried:

alpha = $(grep "variable ok" scan.info | cut -d" " -f 1) but still got the Illegal variable name error.

Any idea?
# 4  
Old 08-25-2009
Quote:
Originally Posted by garth6@hotmail.
I tried that way, and it still didn't work. I'm using csh, could that be why?

My scan.info file is set up like:

1 test ok 512 512 3
2 testing ok 64 64 1
3 variable error 56 64 1
4 variable ok 56 64 1
etc

So I want to set alpha to the number 4. I tried:

alpha = $(grep "variable ok" scan.info | cut -d" " -f 1) but still got the Illegal variable name error.

Any idea?
I'm not very familiar with the c shell, but this seemed to work:

Code:
#!/bin/csh

set var=`grep "variable ok" scan.info | cut -d' ' -f 1`
echo $var

# 5  
Old 08-25-2009
Thanks! I got it to work!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading properties from file and setting variable values

I want to read properties from a file and print evaluated values of each key. I am using AIX6.1. myfile.props protocol=http siteA.host=siteAhostname pageA=pageNameA siteAURL1=${protocol}:/${siteA.host}/pagea/blabla?v1=32 siteAURL2=${protocol}:/${siteA.host}/${pageA}/blabla?v1=32... (5 Replies)
Discussion started by: kchinnam
5 Replies

2. Shell Programming and Scripting

Fetch the different data by searching with a same variable from a file in AIX server

Hi, I am trying to fetch the different values in an xml file by searching with the same variable in AIX Server. <name>SharedResources/Shared/JNDI/Username</name> <value>admin</value> <name>SharedResources/Shared/JNDI/Username</name> ... (1 Reply)
Discussion started by: tejastrikez
1 Replies

3. Shell Programming and Scripting

Grabbing top line of text in a file and setting it to a variable

If I have a txt file with test.txt somelineoftext and I want to set that line of text to variable in a script: so #!/bin/bash var='' becomes #!/bin/bash var='somelineoftext' (3 Replies)
Discussion started by: digitalviking
3 Replies

4. Shell Programming and Scripting

create file as variable for searching point

Hi Friends, I need expert help:), I have bellow script that function for searching string in multiple file, the script is working well. but I thing it still can be optimize since so many repetition in bellow command, where string that I marked BOLD italic is clue for what I am looking for... (2 Replies)
Discussion started by: budi.mulya
2 Replies

5. Shell Programming and Scripting

Setting Environment variable from value in file

I've searched Google and now this forum. Best guess is my search fu is not good (and it probably isn't). The Google search did bring me here. Background I have a number of Korn Shell scripts who all use one of 3 values for an environment variable used in the backup system. On occasion one or... (8 Replies)
Discussion started by: WolfBrother
8 Replies

6. Shell Programming and Scripting

Help setting variable from file contents with spaces

I suppose the easiest way to explain my problem is to show you some code and then show you what the code outputs: -------------------------------------------------- #!/bin/bash echo "This line has spaces" > "/tmp/This Filename Has Spaces.txt" export Foo=$(cat "/tmp/This Filename Has... (4 Replies)
Discussion started by: nrogers64
4 Replies

7. UNIX for Dummies Questions & Answers

Searching a text file and assigning it to a variable

Hi Gurus, I am new to unix.I have a requirement as below I have text file like a.txt which contains a.txt hi hello process update status Ok to Proceed no issues good data arrangement My requirement here is i need to read the file and check for the words "OK to Proceed" and if... (2 Replies)
Discussion started by: pssandeep
2 Replies

8. Shell Programming and Scripting

Setting Variable to oldest file in a directory

I am using a bash script to perform some automated maintenance on files in a directory. When I run the script using $sh -x script.sh <directory> the script works fine. It sets the variable to the oldest file, and continues on. However when I run the script like this $./script.sh <directory>, it... (5 Replies)
Discussion started by: spaceherpe61
5 Replies

9. Shell Programming and Scripting

Piping to a file and setting filename using a variable

Hi all, I would like to send the output of a line in a ksh script to a file, but I need to name the file using a predefined variable: ls -l > $MYVAR.arc But what is the correct syntax for achieving this? I can't seem to find the correct syntax for giving the file an extension. Any... (8 Replies)
Discussion started by: mandriver
8 Replies

10. Shell Programming and Scripting

setting file count to a variable

Hey guys. My goal here is to count the number of .dat files in in a directory(28 files). If 28 files exist I am ok. Having trouble doing this. Any help would b e greatly appreciated. #!/usr/bin/ksh #============================================================================= ### Define local... (3 Replies)
Discussion started by: ecupirate1998
3 Replies
Login or Register to Ask a Question