0

Say I had a file called name.txt with the following:

white-list=true max-players=3 motd=Welcome // Version: One 

How would I go about finding the world One and only replacing this world with Two for example?

2 Answers 2

2

You could use sed 's/One/Two/1' to replace one occurrence of One with Two (replace '1' with 'g' to replace every occurrence of One).

# Note the /1 makes the One to Two happen once. echo "Welcome // Version: One" | sed 's/One/Two/1' 

So, for a file "name.txt" you could do

cat "name.txt" | sed 's/One/Two/1' > "temp.name.txt" && mv "temp.name.txt" "name.txt" 
Sign up to request clarification or add additional context in comments.

Comments

1

As a complement to the answer by Elliot Frisch, some versions of sed have a -i option to change "in place" (in fact, sed itself create the temporary file for you)

 -i[SUFFIX], --in-place[=SUFFIX] edit files in place (makes backup if extension supplied) 

from man sed (GNU sed version 4.2.1)

So you could simplify use the command:

sed -i 's/One/Two/' name.txt 

or

sed -i 's/One/Two/g' name.txt 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.