UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 23rd, 2004, 03:16 PM
khanjibran khanjibran is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 11 khanjibran New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 59 m 7 sec
Reputation Power: 0
Need help with basic Shell programming

Hi
I am trying to write a shell program which takes in three parameters: the file name of text document, the original word and the new word e.g.
$ replace.sh blah.txt Dev Shed
So this should convert all the "Dev" strings in the blah.txt to "Shed"
Following is my script
#! /bin/csh
set file1=$argv[1]
set file2=$argv[2]
set file3=$argv[3]
sed 's/file2/file3/ file1

when I run it, it says cannot open file1. Can somebody help me with this. I am a beginner and its really frustrating me.
Thanx

Reply With Quote
  #2  
Old October 23rd, 2004, 03:37 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,536 Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 6 h 45 m 12 sec
Reputation Power: 876
Moving this thread to the unix forum as it has nothing to do with C programming. As for your specific problem, you need to refer to your variables as $file1, $file2 etc.
Code:
#! /bin/csh
set file1=$argv[1]
set file2=$argv[2]
set file3=$argv[3]

sed 's/$file2/$file3/' $file1

Note the $ signs in the sed line.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Puzzle of the Month solved by Keath and KevinADC, superior perl programmers of the month
Looking for a perl job with kick-*** programmers in a well-known NASDAQ listed tech company with branches in the US and Europe? We're hiring. PM me for details. Requirements

Reply With Quote
  #3  
Old October 23rd, 2004, 03:43 PM
khanjibran khanjibran is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 11 khanjibran New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 59 m 7 sec
Reputation Power: 0
Thank You. I am totally new to this forum thats why I kind of don't know where to post what. Thanx again

Reply With Quote
  #4  
Old October 23rd, 2004, 03:49 PM
khanjibran khanjibran is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 11 khanjibran New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 59 m 7 sec
Reputation Power: 0
Now it seems as if the sed command does not work because it is basically giving me the same output when I run replace.sh blah.txt Dev Shed

Reply With Quote
  #5  
Old October 24th, 2004, 04:19 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,089 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 Days 3 h 40 m 34 sec
Reputation Power: 9
sed 's/$file2/$file3/' $file1 <---- will never expand $file[23]
sed "s/$file2/$file3/" $file1 <--- does it
see the difference ! girl? boy ? 1 chromosome
are you beginning... so do it the proper way
csh is a fantastic tool
on the command line (my preferred) but
the BSD-guys did not complete the job, so
NEVER, NEVER, NEVER write csh SCRIPTS
it's buggy, to be portable prefer the old BOURN,
else KSH, BASH or other.

Last edited by guggach : October 24th, 2004 at 04:22 AM. Reason: typo

Reply With Quote
  #6  
Old October 24th, 2004, 04:08 PM
khanjibran khanjibran is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 11 khanjibran New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 59 m 7 sec
Reputation Power: 0
Hi

Hi guys. Thanx for being so helpful. I just have one last thing that I can not figure out. I wanna make permanent changes to the actual file using sed command. How do I do it. It seems like it does not change the actual contents of the file . thanx

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Need help with basic Shell programming


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway