'sed search and replace with 2 conditions/patterns
I have a file:
<a href="http://www.gnu.org/software/coreutils/">http://www.gnu.org/software/coreutils/</a>
<a href="../../3/glob.html" rel="nofollow">glob</a>
<a href="lxc-ls.html" rel="nofollow">lxc-ls</a>
I need to only replace below:
<a href="lxc-ls.html" rel="nofollow">lxc-ls</a>
with
<a href="../lxc-ls.html" rel="nofollow">lxc-ls</a>
lxc-ls can be any word as I have multiple such links in several files which I need to replace.
I do not want to make any changes to the other 2 links. i.e.
<a href="http://www.gnu.org/software/coreutils/">http://www.gnu.org/software/coreutils/</a>
<a href="../../3/glob.html" rel="nofollow">glob</a>
What I have tried until is:
$ sed '/html/ i\
..' file
But this appends to the start of the line, also the other condition of excluding 2 URLs is also not full filled.
Here is a more realistic example from one of the file.
<b><a href="../../1/echoping.html" rel="nofollow">echoping</a></b>(1),
<b><a href="../../3/getaddrinfo.html" rel="nofollow">getaddrinfo</a></b>(3),
<b><a href="../../3/getaddrinfo_a.html" rel="nofollow">getaddrinfo_a</a></b>(3),
<b><a href="../../2/getpeername.html" rel="nofollow">getpeername</a></b>(2),
<b><a href="../../2/getsockname.html" rel="nofollow">getsockname</a></b>(2),
<b><a href="../../3/ping_setopt.html" rel="nofollow">ping_setopt</a></b>(3),
<b><a href="../../5/proc.html" rel="nofollow">proc</a></b>(5),
<b><a href="rds.html" rel="nofollow">rds</a></b>(7),
<b><a href="../../2/recv.html" rel="nofollow">recv</a></b>(2),
<b><a href="rtnetlink.html" rel="nofollow">rtnetlink</a></b>(7),
<b><a href="sctp.html" rel="nofollow">sctp</a></b>(7),
<b><a href="../../3/sctp_connectx.html" rel="nofollow">sctp_connectx</a></b>(3),
<b><a href="../../2/send.html" rel="nofollow">send</a></b>(2),
<b><a href="udplite.html" rel="nofollow">udplite</a></b>(7)
<a href="http://gnu.org/licenses/gpl.html">http://gnu.org/licenses/gpl.html</a>
<a href="http://translationproject.org/team/">http://translationproject.org/team/</a>
Here I only need to replace:
<b><a href="rds.html" rel="nofollow">rds</a></b>(7),
<b><a href="rtnetlink.html" rel="nofollow">rtnetlink</a></b>(7),
<b><a href="sctp.html" rel="nofollow">sctp</a></b>(7),
<b><a href="udplite.html" rel="nofollow">udplite</a></b>(7)
with:
<b><a href="../rds.html" rel="nofollow">rds</a></b>(7),
<b><a href="../rtnetlink.html" rel="nofollow">rtnetlink</a></b>(7),
<b><a href="../sctp.html" rel="nofollow">sctp</a></b>(7),
<b><a href="../udplite.html" rel="nofollow">udplite</a></b>(7)
Solution 1:[1]
Normally you should have classes with properties that link the instances together:
class News {
public ICollection<Image> Images {get;set;} = new()(
...
class Image {
public News News {get;set;}
}
In this case one News has many Image
You can just build the entire related graph of objects and ask EF to save it:
var n = new News { ... }
n.Images.Add(new Image {...});
context.News.Add(n);
context.SaveChanges();
EF will be able to see the new Image and know which News it is related to because it is stored in the n.Images list. When it saves the data, it will automatically get the IDs and make the linkages. You don't have to do all this manually, saving one thing at a time, pulling its ID out, setting it somewhere else, saving again..
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Caius Jard |
