Sed - Replacing "blattnig" with "blattnig/sirest"

  • Thread starter Norman
  • Start date
In summary, The conversation is about using sed command to replace a phrase in a file with another phrase that includes a slash. The problem is that sed does not seem to like the slash and the solution is to escape it with a backslash (\). Another option is to use the & symbol to repeat the search pattern. It is also mentioned that the slash can be used as a separator in sed commands, but it is not necessary to use it. The person asking for help is grateful for the quick response.
  • #1
Norman
897
4
I need some help with a sed command.

want to take a file new.f and replace the phrase "blattnig" with "blattnig/sirest" every where in it. Well sed doesn't seem to like the slash. Is there an escape character in sed so it reads the / correctly?
thanks,
Ryan
 
Computer science news on Phys.org
  • #2
backslash (\) will escape the / like this:

s/blattnig/blattnig\/sirest/

also, & repeats the search pattern like this:

s/blattnig/&\/sirest/

Also, the reason you need to escape the slash is because you used the slash for a separator. Try this:

s~blattnig~&/sirest~

In other words, the first character after the s is the separator.
 
  • #3
jimmysnyder said:
backslash (\) will escape the / like this:
s/blattnig/blattnig\/sirest/
also, & repeats the search pattern like this:
s/blattnig/&\/sirest/
Also, the reason you need to escape the slash is because you used the slash for a separator.
it didn't like this at all. It still atleast the first one.

Try this:
s~blattnig~&/sirest~
In other words, the first character after the s is the separator.

very good to know! you don't have to use the / as a separator. Thanks a ton for the quick reply.
Cheers,
Ryan
 

FAQ: Sed - Replacing "blattnig" with "blattnig/sirest"

What is "Sed" and how is it used to replace text?

Sed is a command-line utility program used in Linux and Unix systems to manipulate and transform text. It can be used for various tasks, such as searching, replacing, and editing text within files.

How do you use Sed to replace specific words within a text file?

To replace a word or phrase, such as "blattnig" with "blattnig/sirest", you would use the following command: sed -i 's/blattnig/blattnig\/sirest/g' input.txt

Here, the -i flag is used to edit the file in place, the 's' indicates a substitution command, the first word after the first forward slash is the word to be replaced, and the second word after the second forward slash is the word to be inserted. The 'g' at the end means to perform the substitution globally, replacing all instances of the word in the file.

Can Sed be used to replace multiple words at once?

Yes, Sed can be used to replace multiple words at once. You can use the same 's' substitution command and add multiple pairs of words to be replaced, separated by a semicolon. For example: sed -i 's/blattnig/blattnig\/sirest/g; s/word1/word2/g' input.txt

Is it possible to use Sed to replace text within multiple files at once?

Yes, you can use Sed to replace text within multiple files at once. Instead of specifying a single input file, you can use wildcards to target multiple files. For example: sed -i 's/blattnig/blattnig\/sirest/g' *.txt

This command will replace the word "blattnig" with "blattnig/sirest" in all files with the .txt extension in the current directory.

Are there any limitations to using Sed for text replacement?

While Sed is a powerful tool for text manipulation, it does have some limitations. It can only search and replace within plain text files, and it may struggle with complex patterns or special characters. Additionally, Sed is a command-line tool, so it may not be as user-friendly for those who are not familiar with using terminal commands.

Similar threads

Back
Top