1

I've got a problem with some regular expression in sed.

I want to change a line in my file, so I did it like that:

sed -i '3s/.*/my new text in file/' file.txt.

This extension is looking for 3rd line in a file and change it.

This solution isn't good but I don't know how to build a good regular extension. Let's say that I am looking for the word 'Host' in some file. How to build a good regular extension?

I have tried the following:

sed -e '<\H...s>\ /my new text in file/' file.txt'.

What am I doing wrong?

  • When you have found the word, would you want to replace the hole line, or just the word? – Kusalananda Jan 26 '17 at 8:32
  • I wanna replace all line. Old line will be delete, and new will be add. – SanHolo Jan 26 '17 at 8:37
0
$ sed 's/^.*[[:<:]]Host[[:>:]].*$/replacement text/' file.txt

The above will replace every line in the file file.txt that contains the word Host with the string replacement text. The result will be given on standard output.

The [[:<:]] is a zero-length pattern that matches at the beginning of a word. Similarly, [[:>:]] will match at the end of a word. This means that the above substitution will not replace a line with the word Hostname or xHost on it (unless the word Host appears elsewhere on the line).

  • sed -e expression # 1 sign 68: Wrong name of a character class<-- this shows when i use your extension. – SanHolo Jan 26 '17 at 9:19
  • @SanHolo Then try using the older syntax \< in place of [[:<:]] and \> in place of [[:>:]]. – Kusalananda Jan 26 '17 at 9:24
  • Now extensions doesnt show error, but didnt change anythink. – SanHolo Jan 26 '17 at 9:35
  • 1
    @SanHolo It may be enough to just use 's/^.*Host.*$/replacement text/' then. It's difficult for me to know, because you didn't supply an example of the input. – Kusalananda Jan 26 '17 at 10:09
  • 1
    @SanHolo What sed are you actually using? What does sed --version output? What Unix are you on? – Kusalananda Jan 26 '17 at 10:15
1

Although almost no-one seems to use it, sed provides a c (change) command for whole-line replacement:

c\ 
text

    Delete the lines matching the address or address-range, and 
    output the lines of text which follow this command.

So for example you could use

sed '/\bHost\b/c\                      
my new text
' file

to change lines containing whole-word Host to my new text. The \b sequence represents a word boundary so it will exclude partial matches like Hostname - in GNU sed, you can use \< and \> instead i.e. \<Host\> and you can also put the new text on a single line:

sed '/\<Host\>/cmy new text' hostfile

which seems to be what you were aiming for in your expression - you just missed the actual c command and got the word boundary specifiers backwards i.e. <\. . .>\ in place of \<. . .\>

  • The issue with the c command (and a and i) is that they take the text on the next line of input, which is always a bit of a hassle (unless you're using GNU sed, probably). – Kusalananda Jan 26 '17 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.