Insert some lines before a specific line with sed
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
I've the following file
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
</fontconfig>
and I've to add the following lines:
<dir>/usr/local/texlive/texmf-local</dir>
<dir>/usr/local/share/fonts</dir>
before the closing tag /fontconfig>. I'm not sure that it's always on 7th line, so I must look for it as a string. I've some troubles in these strings with <> and / ... How can I solve with sed? thanx
sed xml
add a comment |Â
up vote
4
down vote
favorite
I've the following file
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
</fontconfig>
and I've to add the following lines:
<dir>/usr/local/texlive/texmf-local</dir>
<dir>/usr/local/share/fonts</dir>
before the closing tag /fontconfig>. I'm not sure that it's always on 7th line, so I must look for it as a string. I've some troubles in these strings with <> and / ... How can I solve with sed? thanx
sed xml
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I've the following file
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
</fontconfig>
and I've to add the following lines:
<dir>/usr/local/texlive/texmf-local</dir>
<dir>/usr/local/share/fonts</dir>
before the closing tag /fontconfig>. I'm not sure that it's always on 7th line, so I must look for it as a string. I've some troubles in these strings with <> and / ... How can I solve with sed? thanx
sed xml
I've the following file
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
</fontconfig>
and I've to add the following lines:
<dir>/usr/local/texlive/texmf-local</dir>
<dir>/usr/local/share/fonts</dir>
before the closing tag /fontconfig>. I'm not sure that it's always on 7th line, so I must look for it as a string. I've some troubles in these strings with <> and / ... How can I solve with sed? thanx
sed xml
edited Feb 25 at 18:28
RomanPerekhrest
22.4k12144
22.4k12144
asked Feb 25 at 18:07
user41063
1234
1234
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
7
down vote
accepted
Don't use sed
, awk
and alike for parsing XML/HMTL data - it'll never come to robust and scalable result. Use a proper XML/HTML processors.
The right way with xmlstarlet
tool:
xmlstarlet ed -s '//fontconfig' -t elem -n 'dir' -v '/usr/local/texlive/texmf-local'
-s '//fontconfig' -t elem -n 'dir' -v '/usr/local/share/fonts' input.xml
The output:
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
<dir>/usr/local/texlive/texmf-local</dir>
<dir>/usr/local/share/fonts</dir>
</fontconfig>
To modify/edit the file in-place - add -L
option: xmlstarlet ed -L ....
For more details type: xmlstarlet ed --help
Thanx, it works very nice. It's the first time I meet xmlstartlet...
â user41063
Feb 25 at 18:29
@user41063, you're welcome
â RomanPerekhrest
Feb 25 at 18:57
add a comment |Â
up vote
1
down vote
You can overcome the issues of /
in the pattern text by using a different delimiter such as #
e.g.
sed '#^</fontconfig>#i
<dir>/usr/local/texlive/texmf-local</dir>
<dir>/usr/local/share/fonts</dir>
' file
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
<dir>/usr/local/texlive/texmf-local</dir>
<dir>/usr/local/share/fonts</dir>
</fontconfig>
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
accepted
Don't use sed
, awk
and alike for parsing XML/HMTL data - it'll never come to robust and scalable result. Use a proper XML/HTML processors.
The right way with xmlstarlet
tool:
xmlstarlet ed -s '//fontconfig' -t elem -n 'dir' -v '/usr/local/texlive/texmf-local'
-s '//fontconfig' -t elem -n 'dir' -v '/usr/local/share/fonts' input.xml
The output:
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
<dir>/usr/local/texlive/texmf-local</dir>
<dir>/usr/local/share/fonts</dir>
</fontconfig>
To modify/edit the file in-place - add -L
option: xmlstarlet ed -L ....
For more details type: xmlstarlet ed --help
Thanx, it works very nice. It's the first time I meet xmlstartlet...
â user41063
Feb 25 at 18:29
@user41063, you're welcome
â RomanPerekhrest
Feb 25 at 18:57
add a comment |Â
up vote
7
down vote
accepted
Don't use sed
, awk
and alike for parsing XML/HMTL data - it'll never come to robust and scalable result. Use a proper XML/HTML processors.
The right way with xmlstarlet
tool:
xmlstarlet ed -s '//fontconfig' -t elem -n 'dir' -v '/usr/local/texlive/texmf-local'
-s '//fontconfig' -t elem -n 'dir' -v '/usr/local/share/fonts' input.xml
The output:
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
<dir>/usr/local/texlive/texmf-local</dir>
<dir>/usr/local/share/fonts</dir>
</fontconfig>
To modify/edit the file in-place - add -L
option: xmlstarlet ed -L ....
For more details type: xmlstarlet ed --help
Thanx, it works very nice. It's the first time I meet xmlstartlet...
â user41063
Feb 25 at 18:29
@user41063, you're welcome
â RomanPerekhrest
Feb 25 at 18:57
add a comment |Â
up vote
7
down vote
accepted
up vote
7
down vote
accepted
Don't use sed
, awk
and alike for parsing XML/HMTL data - it'll never come to robust and scalable result. Use a proper XML/HTML processors.
The right way with xmlstarlet
tool:
xmlstarlet ed -s '//fontconfig' -t elem -n 'dir' -v '/usr/local/texlive/texmf-local'
-s '//fontconfig' -t elem -n 'dir' -v '/usr/local/share/fonts' input.xml
The output:
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
<dir>/usr/local/texlive/texmf-local</dir>
<dir>/usr/local/share/fonts</dir>
</fontconfig>
To modify/edit the file in-place - add -L
option: xmlstarlet ed -L ....
For more details type: xmlstarlet ed --help
Don't use sed
, awk
and alike for parsing XML/HMTL data - it'll never come to robust and scalable result. Use a proper XML/HTML processors.
The right way with xmlstarlet
tool:
xmlstarlet ed -s '//fontconfig' -t elem -n 'dir' -v '/usr/local/texlive/texmf-local'
-s '//fontconfig' -t elem -n 'dir' -v '/usr/local/share/fonts' input.xml
The output:
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
<dir>/usr/local/texlive/texmf-local</dir>
<dir>/usr/local/share/fonts</dir>
</fontconfig>
To modify/edit the file in-place - add -L
option: xmlstarlet ed -L ....
For more details type: xmlstarlet ed --help
edited Feb 25 at 18:24
answered Feb 25 at 18:19
RomanPerekhrest
22.4k12144
22.4k12144
Thanx, it works very nice. It's the first time I meet xmlstartlet...
â user41063
Feb 25 at 18:29
@user41063, you're welcome
â RomanPerekhrest
Feb 25 at 18:57
add a comment |Â
Thanx, it works very nice. It's the first time I meet xmlstartlet...
â user41063
Feb 25 at 18:29
@user41063, you're welcome
â RomanPerekhrest
Feb 25 at 18:57
Thanx, it works very nice. It's the first time I meet xmlstartlet...
â user41063
Feb 25 at 18:29
Thanx, it works very nice. It's the first time I meet xmlstartlet...
â user41063
Feb 25 at 18:29
@user41063, you're welcome
â RomanPerekhrest
Feb 25 at 18:57
@user41063, you're welcome
â RomanPerekhrest
Feb 25 at 18:57
add a comment |Â
up vote
1
down vote
You can overcome the issues of /
in the pattern text by using a different delimiter such as #
e.g.
sed '#^</fontconfig>#i
<dir>/usr/local/texlive/texmf-local</dir>
<dir>/usr/local/share/fonts</dir>
' file
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
<dir>/usr/local/texlive/texmf-local</dir>
<dir>/usr/local/share/fonts</dir>
</fontconfig>
add a comment |Â
up vote
1
down vote
You can overcome the issues of /
in the pattern text by using a different delimiter such as #
e.g.
sed '#^</fontconfig>#i
<dir>/usr/local/texlive/texmf-local</dir>
<dir>/usr/local/share/fonts</dir>
' file
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
<dir>/usr/local/texlive/texmf-local</dir>
<dir>/usr/local/share/fonts</dir>
</fontconfig>
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You can overcome the issues of /
in the pattern text by using a different delimiter such as #
e.g.
sed '#^</fontconfig>#i
<dir>/usr/local/texlive/texmf-local</dir>
<dir>/usr/local/share/fonts</dir>
' file
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
<dir>/usr/local/texlive/texmf-local</dir>
<dir>/usr/local/share/fonts</dir>
</fontconfig>
You can overcome the issues of /
in the pattern text by using a different delimiter such as #
e.g.
sed '#^</fontconfig>#i
<dir>/usr/local/texlive/texmf-local</dir>
<dir>/usr/local/share/fonts</dir>
' file
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/opentype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/truetype</dir>
<dir>/usr/local/texlive/2017/texmf-dist/fonts/type1</dir>
<dir>/usr/local/texlive/texmf-local</dir>
<dir>/usr/local/share/fonts</dir>
</fontconfig>
answered Feb 25 at 18:16
steeldriver
31.5k34978
31.5k34978
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f426505%2finsert-some-lines-before-a-specific-line-with-sed%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password