LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-24-2011, 06:34 PM   #1
zev42
LQ Newbie
 
Registered: Sep 2010
Distribution: Unbuntu 10.04
Posts: 18

Rep: Reputation: 0
.sh script to replace text


Hello all,
heres whats up:

i'm trying to script a lil piece to execute in terminal
so when i type something like:

scriptname textfile

the script searches the text file, replaces charecters of my choosing with charecters of my choosing

"sed" im told is the way to go, and i got summat like this going

Code:
#!/bin.sh

bash -c "sed -i.backup -e /s/char/newchar/"
(if need be, i think i can just add another line of "-e /s/char/newchar/" if i need to target more charecters or words as needed.)

the issue with the above code. . . how do i get it to target whatever text file follows the command? without having to manually designate sed to it each time?
e.g. in commanding:

scriptname textfile

how do i get the script to target textfile^^^?

much thanks, and obbvs, im a n00b.
 
Old 02-24-2011, 06:55 PM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
You can use $0 is your script to take place of the first argument.

Code:
#!/bin/bash
sed -i.backup -e /s/char/newchar/ $0
 
0 members found this post helpful.
Old 02-24-2011, 06:57 PM   #3
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Code:
#!/bin/bash
for var in "$@"
do
    sed -i.backup -e /s/char/newchar/ $var
done
That would process every argument that you give it.
 
Old 02-24-2011, 09:15 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by corp769 View Post
You can use $0 is your script to take place of the first argument.
$0 is the scriptname, $1 is the first argument. Also there is an extra slash on the sed command it should be
Code:
sed -i.backup -e s/char/newchar/g
The trailing g is to replace all char with newchar not just the first one on the line.
 
Old 02-26-2011, 09:00 AM   #5
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Code:
ruby -i.bakup -pne '$_.gsub!(/char/,"newchar");print' file
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script to create text in a file or replace value of text if already exists knightto Linux - Newbie 5 09-10-2008 11:13 PM
Need a script to search and replace text in file using shell script unixlearner Programming 14 06-21-2007 10:37 PM
Help! Script or commanded needed to replace text in a file farmerjoe Programming 3 01-02-2005 05:59 PM
Script to search and replace in text file - kinda... jeffreybluml Programming 45 11-07-2004 05:37 PM
Search and replace text in file using shell script? matthurne Linux - Software 2 11-02-2004 10:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration