LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (http://www.linuxquestions.org/questions/slackware-14/)
-   -   How to write a bash script to replace all "KH" to "K" in file ABC??? (http://www.linuxquestions.org/questions/slackware-14/how-to-write-a-bash-script-to-replace-all-kh-to-k-in-file-abc-317776/)

cqmyg5 04-27-2005 01:35 PM

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"

gbonvehi 04-27-2005 01:49 PM

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

mschutte 04-27-2005 01:56 PM

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

ahh 04-27-2005 02:55 PM

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


gloriant 07-24-2007 09:00 AM

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 05:25 AM.