Question Locked

UNIX Scripting Question

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I'm trying to pull some text from a file using "grep", which is then saved as a variable. The variable is called, and the entire search string is translated fine, but the string itself contains other variables that are being treated as text, and not as variables. A simple example is below. Thoughts anyone? Thanks...

*********************************************
$cat 1.dat

@@001@@ STEP02
@@001@@ IF (STEP01 eq 0)
@@001@@ RUN TAKE SNODE
@@001@@ SYSOPTS="mv $file1 $file2"
@@001@@ EIF
*********************************************
$cat test.sh
#!/bin/ksh

file1=test1.txt
file2=test2.txt

the_string=`grep "@@001@@" 1.dat | sed s/@@001@@//`

echo "$the_string"
*********************************************
$test.sh
STEP02
IF (STEP01 eq 0)
RUN TAKE SNODE
SYSOPTS="mv $file1 $file2"
EIF

3 replies

by pls323

Grep and sed both return text, not variables. Your example shows grep & sed performing as expected. Do you want to "eval" the results of the sed or maybe pipe them into a "while ... read ..." loop? Help us understand what "other variables treated as text, and not as variables" means.

by c_tharp

It appears that you want the output to be like the last lines of your message. If that is so, the following script will produce it.

echo file1=test1.txt> program.sh
echo file2=test2.txt>> program.sh
grep "@@001@@" 1.dat | sed s/@@001@@// >> program.sh
more program.sh

This discussion is closed to new replies.