Question:
Write a script to replace a pattern with other pattern without opening the file.
Answer:
#!/usr/bin/bash
inputfile="/home/kalam/filename.sh"
if [ -e $inputfile ]
then
echo "File is exists in the following path: $inputfile"
#pattern to replaced
pattern_to_replace="state_name"
#replacement pattern
replacement_text="STATE_NAME"
sed -i "s/$pattern_to_replace/$replacement_text/g" "$input file"
echo "pattern '$pattern_to_replace' replaced with '$replacement_text' in '$inputfile'"
else
echo "file not found: $inputfile"
echo "Please provide realpath of the file and re-run the script\n Thank you for using this script"
fi
output:
echo "Please Provide the user name and state name"
read name STATE_NAME
echo "User provided name is :$name and His state name is : $STATE_NAME "
if [ "$STATE_NAME" = "andra" ]; then
echo "$name speaks Telugu"
else
echo "$name does not speak Telugu"
fi
No comments:
Post a Comment