Bash 脚本创建一个文件,写入文件,然后将文件中的内容加载到数组中。然后搜索并替换文件中的单词[关闭]

问题描述 投票:0回答:1

这是新的,所以任何建议都会很棒!

我正在尝试创建一个文件,输入文件的内容,显示文件,最后搜索并替换该文件中的单词并再次显示内容。

该文件应包含以下内容:

Write a bash script
To show off 
Your scripting skills
Then show the results 

搜索“你的”并将其替换为“我的”,将“然后”替换为“现在”

到目前为止我已经得到:

#Check if the file exits and create if not (im not sure if there is a better way to do this)
if [ ! -e "NewFile.txt" ]; then
    touch NewFile.txt
fi

#Ask user for input
echo "Input the content of the file:"

#Display the file the first time
cat NewFile.txt

#load what's in the file into an array. 
not sure what to put here 

#search and replace “Your” with “My” and the word “Then” with “Now”
not sure what to put here 


#display the file after words have been replaced
cat NewFile.txt
bash loops
1个回答
0
投票

sed -i 's/Your/My/g' input.txt && sed -i 's/Then/Now/g' input.txt

用第二个单词替换第一个单词,编辑文件。我有两个命令来实现它,一个用于第一次替换,第二个用于第二次替换,通过

&&
分隔。

© www.soinside.com 2019 - 2024. All rights reserved.