save script as filename.sh chmod +x filename.sh execute this command - ./filename.sh
#!/bin/bash
#! - shebang
if we not mentioning shebang we can execute it as -
+++
:%s/old/new/g
This replaces all occurrences of "old" with "new" in the entire file without confirmation.
:%s/old/new/gc
Adds thecflag for confirmation before each replacement. You can confirm withy(yes),n(no),a(all),q(quit),l(last), orCtrl-EandCtrl-Yto scroll while confirming.
-
:10,20s/old/new/g
Replaces all occurrences of "old" with "new" only in lines 10 through 20. -
:'<,'>s/old/new/g
If text is selected in visual mode, this replaces all occurrences in the selected area.
:%s/old/new/
Omits thegflag, so only the first occurrence of "old" is replaced on each line.
:%s/old/new/gI
Adds theIflag to ignore case while searching for "old".
:%s/\<old\>/new/g
This replaces only whole words "old" with "new", preventing partial matches (e.g., it will not replace "folder" if searching for "old").
:%s//new/g
Skips specifying the "old" pattern and reuses the last search pattern for replacement.
:%s#old#new#g
Uses#as the delimiter instead of/, which can be useful when replacing strings that contain/.
These commands enhance flexibility and efficiency when using search and replace in vim.
practice-
Update the script to use 2 command line variables $1 and $2 for country and capital respectively. When the script is executed it should now print the country and its capital using the values passed in as command line arguments.
eg: ./print-capital.sh Nigeria Abuja should print Capital city of Nigeria is Abuja
echo "Capital city of $1 is $2"
/home/bob/backup-file.sh to create a backup of any given file. Update the script to use command line argument $1 for the filename to be backed up instead of the hard-coded filename.
eg: ./backup-file.sh create-and-launch-rocket should backup create-and-launch-rocket to create-and-launch-rocket_bkp
# This script creates a backup of a given file by creating a copy as bkp
# For example some-file is backed up as some-file_bkp
set -e
file_name=$1
cp $file_name ${file_name}_bkp
echo "Done"


