How do I substitute only first occurence with sed?

Original file

claudio
antonio
claudio
michele

I want to change only the first occurrence of “claudio” with “claudia”
so that I would get the following:

claudia
antonio
claudio
michele

I have tried the following:

sed -e '1,/claudio/s/claudio/claudia/' nomi

The above command performs global substitution (it replaces all occurrences of ‘claudio’) . Why?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

If you are using GNU sed, try:

sed -e '0,/claudio/ s/claudio/claudia/' nomi

sed does not start checking for the regex that ends a range until after the line that starts that range.

From man sed (POSIX manpage, emphasis mine):

An editing command with two addresses shall select the inclusive range
from the first pattern space that matches the first address through the
next pattern space
that matches the second.

The 0 address is not standard though, that’s a GNU sed extension not supported by any other sed implementation.

Using awk

Ranges in awk work more as you were expecting:

$ awk 'NR==1,/claudio/{sub(/claudio/, "claudia")} 1' nomi
claudia
antonio
claudio
michele

Explanation:

  • NR==1,/claudio/

    This is a range that starts with line 1 and ends with the first occurrence of claudio.

  • sub(/claudio/, "claudia")

    While we are in the range, this substitute command is executed.

  • 1

    This awk’s cryptic shorthand for print the line.

Method 2

A new version of GNU sed supports the -z option.

Normally, sed reads a line by reading a string of characters up to the end-of-line character (new line or carriage return).
The GNU version of sed added a feature in version 4.2.2 to use the “NULL” character instead. This can be useful if you have files that use the NULL as a record separator. Some GNU utilities can generate output that uses a NULL instead a new line, such as “find . -print0” or “grep -lZ”.

You can use this option when you want sed to work over different lines.

echo 'claudio
antonio
claudio
michele' | sed -z 's/claudio/claudia/'

returns

claudia
antonio
claudio
michele

Method 3

Here are 2 more programmatic efforts with sed: they both read the whole file into a single string, then the search will only replace the first one.

sed -n ':a;N;$bb;ba;:b;s/(claudi)o/1a/;p' file
sed -n '1h;1!H;${g;s/(claudi)o/1a/;p;}' file

With commentary:

sed -n '                # don't implicitly print input
  :a                    # label "a"
  N                     # append next line to pattern space
  $bb                   # at the last line, goto "b"
  ba                    # goto "a"
  :b                    # label "b"
  s/(claudi)o/1a/    # replace
  p                     # and print
' file
sed -n '                # don't implicitly print input
  1h                    # put line 1 in the hold space
  1!H                   # for subsequent lines, append to hold space
  ${                    # on the last line
    g                     # put the hold space in pattern space
    s/(claudi)o/1a/    # replace
    p                     # print
  }
' file

Method 4

Sumary

GNU syntax:

sed '/claudio/{s//claudia/;:p;n;bp}' file

Or even (to use only one time the word to be replaced:

sed '/(claudi)o/{s//1a/;:p;n;bp}' file

Or, in POSIX syntax:

sed -e '/claudio/{s//claudia/;:p' -e 'n;bp' -e '}' file

works on any sed, process only as many lines as needed to find the first claudio, works even if claudio is in the first line and is shorter as it use only one regex string.

Detail

To change only one line you need to select only one line.

Using a 1,/claudio/ (from your question) selects:

  • from the first line (unconditionally)
  • to the next line that contains the string claudio.
$ cat file
claudio 1
antonio 2
claudio 3
michele 4

$ sed -n '1,/claudio/{p}' file
claudio 1
antonio 2
claudio 3

To select any line that contains claudio, use:

$ sed -n `/claudio/{p}` file
claudio 1
claudio 3

And to select only the first claudio in the file, use:

sed -n '/claudio/{p;q}' file
claudio 1

Then, you can make a substitution on that line only:

sed '/claudio/{s/claudio/claudia/;q}' file
claudia 1

Which will change only the first occurrence of the regex match on the line, even if there may be more than one, on the first line that match the regex.

Of course, the /claudio/ regex could be simplified to:

$ sed '/claudio/{s//claudia/;q}' file
claudia 1

And, then, the only thing missing is to print all other lines un-modified:

sed '/claudio/{s//claudia/;:p;n;bp}' file

Method 5

You can use awk with a flag to know if the replacement was already done. If not, proceed:

$ awk '!f && /claudio/ {$0="claudia"; f=1}1' file
claudia
antonio
claudio
michele

Method 6

It’s actually really easy if you just setup a little delay – there’s no need to go reaching for unreliable extensions:

sed '$H;x;1,/claudio/s/claudio/claudia/;1d' <<IN
claudio
antonio
claudio
michele
IN

That just defers the first line to the second and the second to the third and etc.

It prints:

claudia
antonio
claudio
michele

Method 7

And one more option

sed --in-place=*.bak -e "1 h;1! H;$! d;$ {g;s/claudio/claudia/;}" -- nomi

The advantage is it uses double quotation, so you can use variables inside, ie.

export chngFrom=claudio
export chngTo=claudia
sed --in-place=*.bak -e "1 h;1! H;$! d;$ {g;s/${chngFrom}/${chngTo}/;}" -- nomi

Method 8

This can also be done without the hold space and without concating all lines into the pattern space:

sed -n '/claudio/{s/o/a/;bx};p;b;:x;p;n;bx' nomi

Explanation: We try to find “claudio” and if we do it we jump into the small print-load-loop between :x and bx. Otherwise we print and restart the script with the next line.

sed -n '      # do not print lines by default
  /claudio/ { # on lines that match "claudio" do ...
    s/o/a/    # replace "o" with "a"
    bx        # goto label x
  }           # end of do block
  p           # print the pattern space
  b           # go to the end of the script, continue with next line
  :x          # the label x for goto commands
  p           # print the pattern space
  n           # load the next line in the pattern space (clearing old contents)
  bx          # goto the label x
  ' nomi

Method 9

sed -n '/claudia/{p;Q}'

sed -n '           # don't print input
    /claudia/      # regex search
    {              # when match is found do
    p;             # print line
    Q              # quit sed, don't print last buffered line
    {              # end do block

Method 10

First pipe some output to awk

Then you can use awk variables to substitute

cat someFile | awk '!x{x=sub("wordToReplace","wordThatIsReplaced")}1'

Notice the number near the end.

It is required but can be any number, I chose 1 to signify the first instance.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x