sed to find first appearance and append string

I have a file like below

#GROUP A belongs to Asia
GROUP A jojh hans local admin
GROUP A gege fans michel jing jong

#GROUP U belongs to USA
GROUP U jeff goal hello world

My requirement is to grep for first apperence of GROUP A which is not commented and append my name to end of file.
I tried the below...but it is appending to each line where GROUP A is there.

sed '/GROUP A/s/$/ vkk/' $i > $i.new
sed '/^GROUP A/!d;s/$/ vkk/;q' $i >$i.new

Thanks. But that is deleting all my other lines and displaying only that line alone. I need the file as such. :frowning:

Is that because of "!d" in the command??

Pls post the output you intend

Please find the output below i want my name to be there without deleting any line of the file.

#GROUP A belongs to Asia
GROUP A jojh hans local admin vkk
GROUP A gege fans michel jing jong

#GROUP U belongs to USA
GROUP U jeff goal hello world

Try this,

sed  '0,/^\(GROUP A\)\(.*\)/s//\1\2 vkk/' infile

Should use awk instead of sed.

smthg like

nawk '/^GROUP A/&&!f{sub(/$/," vkk",$0);f=1}1' $i > $i.new

No change pravin...

@ctsgnb: How using awk???

I updated my previous post :
Use something like :

nawk '/^GROUP A/&&!f{sub(/$/," vkk",$0);f=1}1' $i > $i.new

(or gawk or mawk or just awk depending on what is available and what works on your machine)

Excellent!!! That worked...Thanks for your effort :slight_smile:

A bit variation in technique:

awk -v c=0 '/^GROUP/&&c==0 { print $0" vvk";c=1;next}1' input_File

Hello, ctsgnb: That's just crazy talk. This situation clearly calls for [s]ed :wink:

 printf '/^GROUP A/s/$/ vkk/\nw\nq\n' | ed -s file
ed -s file <<EOED
/^GROUP A/s/$/ vkk/
w
q
EOED
sed '
/^GROUP A/ {
    s/$/ vkk/
    :loop
    n
    b loop
}' file

Regards,
Alister

1 Like

@Alister :

Yes , i am crazy, that's not a scoop :smiley:

And you're true, i should have written "Could use awk"
and not "Should use awk instead of sed"

I just don't masterise [s]ed as well as you do

Thanks for your lights ! :wink: