luke davis
luke davis

Reputation: 27

how to change file content in text file

this is test.txt

hello mate
good morning
no worries

and this is my code

#!/bin/bash

while
do
printf "whice one do you change? "
read change
printf "how to change? "
read after
done < test.txt

I want to change "good" to "gooood" in test.txt file

and here is conditions
-not using sed
-not using perl

How can I change it? please give me a hand~!!!

Upvotes: 0

Views: 126

Answers (2)

sjsam
sjsam

Reputation: 21965

awk '{ sub(/good/, "goood"); print }' test.txt > tempfile && mv tempfile test.txt

or you could use awk with the in-place modifications :

awk -i inplace '{ sub(/good/, "goood"); print }' test.txt

Upvotes: 1

karakfa
karakfa

Reputation: 67497

sed is the best tool for this simple replacement but if not allowed to use tools, you can revert back to bash functionality

 while read line; do echo ${line/good/goood}; done < good.txt > goood.txt

assuming the original file is good.txt, this will replace the string "good" to "goood" and write to goood.txt.

Upvotes: 3

Related Questions