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" |
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 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 |
Code for the GNU Bourne Again Shell, aka bash:
Code:
#!/usr/bin/env bash 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 |
Maybe not as simple as you wanted, but there's nothing on tv and I got a bit carried away :)
Code:
#!/bin/bash |
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 |
All times are GMT -5. The time now is 03:50 PM. |