store a pattern1 line in buffer and use it to replace the pattern2

Viewed 55

file:

url: urlvalue
name: 
organisation:
word

How can i extract urlvalue and add it to name: so that final output will be.

url: urlvalue
name: urlvalue
organisation:
word

regex to find a pattern and make a duplicate but by editing the first word of pattern eg. in above find for line with ^url below that line add duplicate of the same line but change url: to name:

Also query regarding below: How can i negate multiple regex in awk or sed ?

eg. all lines that dont start with name, organisation, and url ---> apped pass: as prefix to those lines

Tried:

awk '$1 != "url*" && $1 != "name*" && $1 != "organisation*" {$1 = "pass"} {print}'

Also tried:

awk '$1 != "url*" && $1 != "name*" && $1 != "organisation*" {gsub(/^/, pass:, "g", $1) {print}'

Not working

Another Tried solution Works partially

awk '$1 != "url:" && $1 != "name:" && $1 != "organisation:" {print "pass"$0}'

But it outputs only

pass:word

Desired output :

url: urlvalue
name: urlvalue
organisation:
pass:word
2 Answers

Assuming that any existing lines beginning with name: can be ignored, and you simply want to duplicate every url: line with url: changed to name:, the following may be suitable:

$ sed -n '/^name:/!p; s/^url:/name:/p' file
url: urlvalue
name: urlvalue
organisation:

-n to disable automatic printing. print any lines that do not start with "name:". Then using the p flag with the s/// command, print lines that started with "url:" having changed it to start with "name:".

The same idea with awk:

$ awk '!/^name:/; sub(/^url:/, "name:")' file
url: urlvalue
name: urlvalue
organisation:

Finally got it worked

awk '$1 != "url:" && $1 != "name:" && $1 != "organisation:" {gsub(/^/,"pass:",$1)} {print}'
Related