Slackware This Forum is for the discussion of Slackware Linux.
|
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
04-27-2005, 02:35 PM
|
#1
|
LQ Newbie
Registered: Apr 2005
Posts: 29
Rep:
|
How to write a bash script to replace all "KH" to "K" in file ABC???
Hello,
I want to write a simple script to replace all "KH" to "K" in file "ABC", could you give me some advice to realize these steps:
(1) first ask for a file name, e.g. "ABC"
(2) replace all "KH" to "K"
(3) repalce all "GG" to "G"
|
|
|
04-27-2005, 02:49 PM
|
#2
|
Senior Member
Registered: Jun 2004
Location: Argentina (SR, LP)
Distribution: Slackware
Posts: 3,145
Rep:
|
My advice would be to look for sed man pages. Also here: http://www.tldp.org/LDP/abs/html/
A simple line to replace text using sed would be: sed -i 's/this/tothis/' file
Now ask for input: read variablename
So basically your script would look like:
Code:
#!/bin/sh
echo "Enter filename: "
read filetowork
sed -i 's/KH/K/g' $filetowork
sed -i 's/GG/G/g' $filetowork
This should work but it's untested, test it in safe place first.
The first line is important since it tells which script to use to execute it, also remember to make the script executable with chmod +x, otherwise you should type: sh yourscript.sh
Last edited by gbonvehi; 04-27-2005 at 03:35 PM.
|
|
|
04-27-2005, 02:56 PM
|
#3
|
Member
Registered: Jan 2005
Location: Innsbruck, Austria
Distribution: Debian GNU/Linux Lenny
Posts: 68
Rep:
|
Code for the GNU Bourne Again Shell, aka bash:
Code:
#!/usr/bin/env bash
read -p 'Enter the file name: ' # Stores filename in $REPLY
sed -i 's/KH/K/g;s/GG/G/g' $REPLY # Does the replacement
'sed' is the Stream EDitor. It takes a little 'script' and applies it onto one or more files. Read the sed script s/KH/K/g:
Replace (s = substitute) every occurrence of KH with K, globally (g).
Globally means: 'KH' may occur more than once a line; if it is omitted, only the first match is replaced. Commands to sed can by seperated by ';', like you can see in my example.
Hope I could help,
mschutte
|
|
|
04-27-2005, 03:55 PM
|
#4
|
Member
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293
Rep:
|
Maybe not as simple as you wanted, but there's nothing on tv and I got a bit carried away
Code:
#!/bin/bash
user_input() {
echo "Please enter the file name including the path"
echo "Or leave blank to quit"
echo "Then press ENTER"
read file
case $file in
"") exit 0 ;;
*) [ -f "$file" ] && change_file || (echo ; echo "The file "$file" doesn't exist"; user_input) ;;
esac
}
change_file() {
newfile=$file"_new"
cat "$file" | sed -e 's/KH/K/g' -e 's/GG/G/g' > "$newfile"
case $? in
0) echo
echo "Done"
echo "The changed file can be found at "$newfile
echo "Do you want to replace the original file with the new file?"
echo "Y or N"
read answer
case $answer in
Y) i=
while [ -f "$file.bak$i" ]; do
let i=i+1
done
mv -f "$file" "$file.bak$i"
mv -f "$newfile" "$file"
echo
echo "File replaced"
echo ;;
*) echo
echo "File not replaced"
echo ;;
esac ;;
*) echo "An unspecified error occured"
echo "See, Linux can be just like Windows!" ;;
esac
user_input
}
user_input
|
|
|
07-24-2007, 10:00 AM
|
#5
|
Member
Registered: Sep 2004
Distribution: Debian{Woody,Sarge,Etch}, UbuntuLTS6.06, SuSE{6.2,8.0}
Posts: 42
Rep:
|
just for completeness, one does not need to invoke sed (o so powerful, but with quit a confusing capacity). As long as you're using bash anyway, you can search/replace in bash too.
Code:
#!/bin/bash
origvar="This is the original string";
replvar=${origvar//original/changed};
replvar=${replvar//string/result.};
echo "replvar==\"${replvar}\".";
echo "this should now state \"This is the changed result.\".";
The // indication prior to the search-pattern, cause a global replace. If you only use ${var/searchstr/replacestr}, only the first occurrence of searchstr in $var will be replaced by replacestr.
|
|
|
All times are GMT -5. The time now is 01:05 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|