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

  • Thread starter Thread starter Norman
  • Start date Start date
Click For Summary
SUMMARY

The discussion focuses on using the sed command to replace the phrase "blattnig" with "blattnig/sirest" in a file named new.f. Users encountered issues with the slash character, which is typically used as a separator in sed commands. The solution involves escaping the slash using a backslash (\) or utilizing an alternative separator such as the tilde (~). The effective commands provided include s/blattnig/blattnig\/sirest/ and s~blattnig~&/sirest~.

PREREQUISITES
  • Basic understanding of command-line interfaces
  • Familiarity with sed command syntax
  • Knowledge of text file manipulation
  • Understanding of escape characters in programming
NEXT STEPS
  • Research advanced sed command techniques
  • Learn about regular expressions in sed
  • Explore alternative text processing tools like awk
  • Investigate the use of different separators in sed commands
USEFUL FOR

System administrators, developers, and anyone involved in text processing or automation using shell scripting will benefit from this discussion.

Norman
Messages
895
Reaction score
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
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.
 
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
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 30 ·
2
Replies
30
Views
7K
Replies
17
Views
3K
  • · Replies 16 ·
Replies
16
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
3
Views
5K