1

Hi Im new to this so please bear with me. I have a .txt file where I want to replace a word but the problem is, that word is being repeated over and over again (lets assume you have million of lines like this)

TOPIC:
APPLE
APPLE
APPLE
BANANA

I want to replace the first occurence of Apple to Mango like this

TOPIC:
MANGO
APPLE
APPLE
BANANA

Is there a way to do this through sed or awk specifically(from what I understand sed can be used to replace words but not multiple lines of inputs - in my case I just want to replace the very first word) btw if its doable through other tools thats fine as well

5
  • it looks like it should be a yaml file, isn't it? are you trying to do this on a yaml file? Commented Jul 27, 2021 at 18:02
  • No its just a normal .txt file - I'm basically trying to find a specific word and replace it with another word in a file that has other repeating words
    – Geralt
    Commented Jul 27, 2021 at 18:04
  • 1
    I reverted to your original question. Please don't radically alter questions after answers have been posted. You can instead ask a new question, but make sure to explain what you need accurately so we can understand. Ideally, show us a few lines of your actual input and the output you want from that. For example, can we assume that the word TOPIC will be present? If not, we need to know this so we don't rely on it.
    – terdon
    Commented Jul 27, 2021 at 18:21
  • @terdon Apologies again. Im new to this so I'm not sure how the edits and asnwers works. Thanks for reverting it back!
    – Geralt
    Commented Jul 27, 2021 at 18:22
  • Don't worry about it, there's no reason to apologize! Our rules are weird, we know and we don't expect new users to magically get them :)
    – terdon
    Commented Jul 27, 2021 at 18:26

3 Answers 3

4

Using sed and assuming that there's always going to be a first line saying something other than APPLE:

sed '1,/APPLE/ s/APPLE/MANGO/' file

This matches the lines from line 1 to the next line that contains the substring APPLE. On those lines, it substitutes the substring APPLE with MANGO. The effect is that the first occurrence of APPLE after the first line is replaced with MANGO.

With GNU sed you could use

sed '0,/APPLE/ s/APPLE/MANGO/' file

which would also cover the first line in case that contained the substring APPLE.


With awk:

awk '!found && /APPLE/ { sub("APPLE", "MANGO"); found = 1 }; 1' file

This tries to find the substring APPLE on each line, and when it has been found, the sub() call replaces the substring with MANGO. A flag variable, found, is then set to 1 (a boolean true value) to stop the substitution from happening again. Each line, modified or not, is printed unconditionally.


Using ed:

printf '%s\n' '/APPLE/ s//MANGO/' ,p Q | ed -s file

This search for a line containing the substring APPLE. When found, it substitutes that substring with MANGO. Only the first match in the file is handled.

The editing script

/APPLE/ s//MANGO/
,p
Q

performs the search and the substitution, prints the whole editing buffer to standard output, and then quits unconditionally.

3
$ awk '$0=="APPLE" && !done++{ $0="MANGO" } 1' file
TOPIC:
MANGO
APPLE
APPLE
BANANA
1
  • @Geralt Scripts need testing. Config-type files are too variant (order, spacing, optionality). For a few files, I would use a visual editor. For a large number, I would write a script and test it against as many hard cases as I could find. Never write a five-line script for a two-line edit. And always get a backup. I usually diff to prove I didn't change anything I didn't intend. Commented Jul 28, 2021 at 14:01
0

Using perl:

$ perl -lpe '$_ = "MANGO" if m?APPLE?' file

Result:-

TOPIC:
MANGO
APPLE
APPLE
BANANA

Using POSIX sed constructs we can also do as follows:

sed -e '
  1{x;s/^/MANGO/;x;}
  /APPLE/!b
  G
  /\n$/{P;d;}
  z;x
' file

Replace zwith s/.*// for POSIX sed.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .