简体   繁体   中英

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~!!!

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.

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

或者您可以使用awk进行就地修改:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM