LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 04-27-2005, 01:35 PM   #1
cqmyg5
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???


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"
 
Old 04-27-2005, 01:49 PM   #2
gbonvehi
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: 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 02:35 PM.
 
Old 04-27-2005, 01:56 PM   #3
mschutte
Member
 
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:
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
 
Old 04-27-2005, 02:55 PM   #4
ahh
Member
 
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

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
 
Old 07-24-2007, 09:00 AM   #5
gloriant
Member
 
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.
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.
 
  


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

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 07:01 AM.

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