1

I want to find a string say 'foo' in a file sat '1.txt' using shell script and replace 'foo' with 'bar' and store the output into other file say '2.txt' without any modification in 1.txt.

so '1.txt' would contain 'foo' itself but '2.txt' will now have all the contents of '1.txt' with 'foo' replaced by 'bar'

I am using this command in bash

sed -i "s/foo/bar/g" "1.txt" > 2.txt

but its not working.

2 Answers 2

1

Remove the -i option as it stands for in-place operation.

-i[SUFFIX], --in-place[=SUFFIX]

      edit files in place (makes backup if extension supplied)

(sed(1) man page)

Sign up to request clarification or add additional context in comments.

Comments

0

Remove the -i switch, or provide a backup extension (-i.bak), so the modified file will be 1.txt and 1.txt.bak the original backup.
You don't need quotes on "1.txt", unless the filename contains spaces.

sed "s/foo/bar/g" 1.txt > 2.txt

or

sed  -i.bak "s/foo/bar/g" 1.txt

Take a look at sed manual

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.