Passing output of sed/echo to a variable | Unix Linux Forums | Shell Programming and Scripting

  Unix/Linux Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Passing output of sed/echo to a variable

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-13-2005
donflamenco donflamenco is offline
Registered User
 
Join Date: Jul 2005
Last Activity: 13 July 2005, 10:08 AM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Passing output of sed/echo to a variable

I understand how to use a variable in a sed command, but for the life of me I can't get the output into a variable.
I'm making a general function to replace part of a filename with a different string, so:
>>myscript this that

would change:
this_file001.txt to that_file001.txt and
this_file002.txt to that_file002.txt and so on...

here's what I have:
#! /bin/csh -f
foreach i ($1*)
set j = echo $i|sed 's/'$1'/'$2'/'
echo "Moving $i to $j"
#mv $i $j
end

But it gives me a syntax error. I played around with using eval but that wouldn't work for me either. Suggestions? (preferably without taking a new approach to my problem even though I'm sure there are other/better ways to do it)
Thanks....
Sponsored Links
    #2  
Old Unix and Linux 07-13-2005
vino's Unix or Linux Image
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 5 March 2015, 6:21 PM EST
Location: Sydney, Down Under
Posts: 2,848
Thanks: 0
Thanked 13 Times in 13 Posts
Try this.

The changes are in bold.


Code:
#! /bin/csh -f
foreach i ($1*)
j=$(echo $i|sed 's/"$1"/"$2"/')
echo "Moving $i to $j"
#mv $i $j
end

Vino
Sponsored Links
    #3  
Old Unix and Linux 07-13-2005
donflamenco donflamenco is offline
Registered User
 
Join Date: Jul 2005
Last Activity: 13 July 2005, 10:08 AM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Still giving me a "Variable Syntax" Error.
    #4  
Old Unix and Linux 07-13-2005
vino's Unix or Linux Image
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 5 March 2015, 6:21 PM EST
Location: Sydney, Down Under
Posts: 2,848
Thanks: 0
Thanked 13 Times in 13 Posts
Can you post the whole error (variable syntax) that is thrown at the prompt ?

vino
Sponsored Links
    #5  
Old Unix and Linux 07-13-2005
zazzybob's Unix or Linux Image
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
 
Join Date: Dec 2003
Last Activity: 13 May 2013, 9:02 AM EDT
Location: Melbourne, Australia
Posts: 2,185
Thanks: 3
Thanked 24 Times in 23 Posts
This should get you going....


Code:
% set foo = "hello"
% set bar = "goodbye"
% set i = "I say hello to you all"
% set j = `echo "$i" | sed "s/$foo/$bar/"`
% echo $j
I say goodbye to you all

So, change your sed line to

set j = `echo "$i" | sed "s/$1/$2/"`

Cheers
ZB
Sponsored Links
    #6  
Old Unix and Linux 07-13-2005
donflamenco donflamenco is offline
Registered User
 
Join Date: Jul 2005
Last Activity: 13 July 2005, 10:08 AM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
I'm a tyro when it comes to cshells - all I'm getting is the output "Variable Syntax" to the command line. Is there a way to get more info from csh?
Sponsored Links
    #7  
Old Unix and Linux 07-13-2005
donflamenco donflamenco is offline
Registered User
 
Join Date: Jul 2005
Last Activity: 13 July 2005, 10:08 AM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
zazzybob:
That will set j to be `echo "$i" | sed "s/$1/$2/"` not actually execute the command.
When I run my script it wants to rename this_file001.txt to echo "$i" | sed "s/$1/$2/"
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
storing output from echo & cut into variable nkwilliams Shell Programming and Scripting 1 07-26-2009 07:05 AM
passing the output of cmd from "eval" to a variable dfezz1 Shell Programming and Scripting 8 07-21-2009 11:19 AM
Passing output to variable instead of file Furqan_79 Shell Programming and Scripting 1 05-27-2008 07:17 AM
passing a variable inside a variable to a function KingVikram UNIX for Dummies Questions & Answers 2 01-14-2008 08:28 PM
What is the output of echo * shailja UNIX for Dummies Questions & Answers 4 06-29-2007 10:27 AM



All times are GMT -4. The time now is 01:31 PM.