Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Slackware This Forum is for the discussion of Slackware Linux.


  Search this Thread
Old 04-27-2005, 02:35 PM   #1
LQ Newbie
Registered: Apr 2005
Posts: 29

Rep: Reputation: 15
How to write a bash script to replace all "KH" to "K" in file ABC???


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"
Old 04-27-2005, 02:49 PM   #2
Senior Member
Registered: Jun 2004
Location: Argentina (SR, LP)
Distribution: Slackware
Posts: 3,145

Rep: Reputation: 53
My advice would be to look for sed man pages. Also here:
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:
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

Last edited by gbonvehi; 04-27-2005 at 03:35 PM.
Old 04-27-2005, 02:56 PM   #3
Registered: Jan 2005
Location: Innsbruck, Austria
Distribution: Debian GNU/Linux Lenny
Posts: 68

Rep: Reputation: 15
Code for the GNU Bourne Again Shell, aka bash:
#!/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,
Old 04-27-2005, 03:55 PM   #4
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
Maybe not as simple as you wanted, but there's nothing on tv and I got a bit carried away


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) ;;

change_file() {
	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
			mv -f "$file" "$file.bak$i"
			mv -f "$newfile" "$file"
			echo "File replaced"
			echo ;;
			*) echo
			echo "File not replaced"
			echo ;;
		esac ;;
		*) echo "An unspecified error occured"
		echo "See, Linux can be just like Windows!" ;;

Old 07-24-2007, 10:00 AM   #5
Registered: Sep 2004
Distribution: Debian{Woody,Sarge,Etch}, UbuntuLTS6.06, SuSE{6.2,8.0}
Posts: 42

Rep: Reputation: 16
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.
origvar="This is the original string";
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 02:03 AM
Where to download precompiled bash binaries, such as "time" and "top"? elinuxqs Linux - Newbie 12 11-14-2005 09:36 PM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 03:32 PM
bash equivalence of tcsh "alias em "emacs \!:1 &""? rgiggs Slackware 3 07-29-2004 03:07 AM
bash-2.05b# Xlib: extension "XFree86-DRI" missing on display ":0.0". citrus Linux - General 8 02-22-2004 11:43 AM > Forums > Linux Forums > Linux - Distributions > Slackware

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

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