• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

sed command in unix

SoundTheSurrender

Diamond Member
I'm trying to use the sed command in unix with variables. It takes my input but doesn't export the changes over. Obviously I have to get rid of the slashes but everything I've tried doesn't work. Does SED only work with manually enter words to look for and change? I need it to use variables. If that's not possible, what would be the best way to go about this?

#! /bin/csh

# Check to make sure the testfile exists

set list="testfile"

if ( -e $list) then
echo $list exists
else
echo $list does not exit
endif

# End Check to make sure the testfile exists

# Read each mispelled word
foreach name (`ispell -l < testfile`)
echo "'$name' is mispelled."
#Correct it
echo Correct the spelling
set name1 = $<

echo Name $name
# The echo command above and below just check to see if variables exist
echo Name1 $name1

# Replace the word inputted in name1 with the incorrect spelling of name
#sed -e 's/$name1/$name/g' < testfile > testfile.tmp
sed -e 's/$name/$name1/g' < testfile > testfile.tmp




/****** testfile

The dog is deadd
 
you need to put brackets around your variables like this: ${name1} and ${name}

so your sed line would be like this:
sed -e 's/${name}/${name1}/g' < testfile > testfile.tmp

The only other thing is I'm not sure (I am a sed novice) that you can do the redirection with testfile the way you are. I think I've done it like this before:

cat testfile | sed -e 's/${name}/${name1}/g' > testfile.tmp
 
Along the same lines as what Brazen said, are you sure the right sed command is even being run? It sounds like what you're asking is more of a csh question than sed since it's csh that's supposed to handle the variables and run sed for you.
 
oops, didn't realize he was using csh. I'm not sure about variable handling with csh; what I said would apply to BASH.
 
Damn 🙁

ya it's csh..

lx:~/assn3> ./wordfile
testfile exists
'deadd' is mispelled.
dead <---------My input
Name deadd
Name1 dead
lx:~/assn3> cat testfile.tmp
The dog is deadd
lx:~/assn3> cat testfile
The dog is deadd
lx:~/assn3>
 
Also, in BASH anyway, variables will not be resolved when inside single quotes. So:

sed -e 's/${name}/${name1}/g'

would actually have to be:

sed -e "s/${name}/${name1}/g"

And keep in mind since it is in csh, I don't know if you need the brackets around the variables or not.
 
Back
Top