sed command to rplace a line with multiple lines without changing the format of the file [closed]
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have the below xml file
cat example.xml
<?xml version="1.0" encoding="UTF-8"?>
<TestConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Text>
<Checking state="BOOT" />
<function name="configure" type="test" username="user1" num="1" Mode="NA" Type="OPEN" txt="NA-TO" TextType="guest" who="BANANNA"/>
<function name="configure" type="test" username="user2" num="2" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA" />
<function name="configure" type="test" username="user3" num="3" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA"/>
<function name="configure" type="test" username="user4" num="4" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user5" num="5" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user6" num="7" Mode="YA" Type="WPA2-PSK" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
</Text>
</TestConfig>
In this file, I would like to change the line 5 from
<function name="configure" type="test" username="user1" num="1" Mode="NA" Type="OPEN" txt="NA-TO" TextType="guest" who="BANANNA"/>
to
<function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
text-processing awk sed
closed as unclear what you're asking by Jeff Schaller, jayhendren, mdpc, Archemar, G-Man Jan 4 at 6:58
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |Â
up vote
0
down vote
favorite
I have the below xml file
cat example.xml
<?xml version="1.0" encoding="UTF-8"?>
<TestConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Text>
<Checking state="BOOT" />
<function name="configure" type="test" username="user1" num="1" Mode="NA" Type="OPEN" txt="NA-TO" TextType="guest" who="BANANNA"/>
<function name="configure" type="test" username="user2" num="2" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA" />
<function name="configure" type="test" username="user3" num="3" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA"/>
<function name="configure" type="test" username="user4" num="4" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user5" num="5" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user6" num="7" Mode="YA" Type="WPA2-PSK" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
</Text>
</TestConfig>
In this file, I would like to change the line 5 from
<function name="configure" type="test" username="user1" num="1" Mode="NA" Type="OPEN" txt="NA-TO" TextType="guest" who="BANANNA"/>
to
<function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
text-processing awk sed
closed as unclear what you're asking by Jeff Schaller, jayhendren, mdpc, Archemar, G-Man Jan 4 at 6:58
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
I missed it or you forgot to paste the file ? If I understand your needs you want to change line 5 whatever it contains ? sed -i '5s/pattern/newpattern/' file.xml would be ok then .
â francois P
Jan 3 at 21:47
How are you selecting line 5? Is it always line 5? or are you looking for a pattern on line 5? Do you want to change all lines with<function .* />
?
â Michael Daffin
Jan 3 at 22:06
Sorry for the confusion, in this example it is line 5.
â Shravankumar Daddolu
Jan 3 at 22:24
2
Can you use an XML editor, which is tailor-made to edit XML, rather than some shell pipeline, which are notoriously poor at parsing XML/XHTML?
â jayhendren
Jan 4 at 0:20
Your question title mentions âÂÂwithout changing the format of the fileâÂÂ, but your question body doesnâÂÂt elaborate.âÂÂIs your concern that the new lines should dynamically match the indentation of the existing line(s)?âÂÂIf so, see How can I tag text based on its indentation?
â G-Man
Jan 4 at 6:57
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have the below xml file
cat example.xml
<?xml version="1.0" encoding="UTF-8"?>
<TestConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Text>
<Checking state="BOOT" />
<function name="configure" type="test" username="user1" num="1" Mode="NA" Type="OPEN" txt="NA-TO" TextType="guest" who="BANANNA"/>
<function name="configure" type="test" username="user2" num="2" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA" />
<function name="configure" type="test" username="user3" num="3" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA"/>
<function name="configure" type="test" username="user4" num="4" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user5" num="5" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user6" num="7" Mode="YA" Type="WPA2-PSK" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
</Text>
</TestConfig>
In this file, I would like to change the line 5 from
<function name="configure" type="test" username="user1" num="1" Mode="NA" Type="OPEN" txt="NA-TO" TextType="guest" who="BANANNA"/>
to
<function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
text-processing awk sed
I have the below xml file
cat example.xml
<?xml version="1.0" encoding="UTF-8"?>
<TestConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Text>
<Checking state="BOOT" />
<function name="configure" type="test" username="user1" num="1" Mode="NA" Type="OPEN" txt="NA-TO" TextType="guest" who="BANANNA"/>
<function name="configure" type="test" username="user2" num="2" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA" />
<function name="configure" type="test" username="user3" num="3" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA"/>
<function name="configure" type="test" username="user4" num="4" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user5" num="5" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user6" num="7" Mode="YA" Type="WPA2-PSK" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
</Text>
</TestConfig>
In this file, I would like to change the line 5 from
<function name="configure" type="test" username="user1" num="1" Mode="NA" Type="OPEN" txt="NA-TO" TextType="guest" who="BANANNA"/>
to
<function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
text-processing awk sed
edited Jan 3 at 21:50
ñÃÂsýù÷
15.3k92462
15.3k92462
asked Jan 3 at 21:45
Shravankumar Daddolu
82
82
closed as unclear what you're asking by Jeff Schaller, jayhendren, mdpc, Archemar, G-Man Jan 4 at 6:58
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by Jeff Schaller, jayhendren, mdpc, Archemar, G-Man Jan 4 at 6:58
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
I missed it or you forgot to paste the file ? If I understand your needs you want to change line 5 whatever it contains ? sed -i '5s/pattern/newpattern/' file.xml would be ok then .
â francois P
Jan 3 at 21:47
How are you selecting line 5? Is it always line 5? or are you looking for a pattern on line 5? Do you want to change all lines with<function .* />
?
â Michael Daffin
Jan 3 at 22:06
Sorry for the confusion, in this example it is line 5.
â Shravankumar Daddolu
Jan 3 at 22:24
2
Can you use an XML editor, which is tailor-made to edit XML, rather than some shell pipeline, which are notoriously poor at parsing XML/XHTML?
â jayhendren
Jan 4 at 0:20
Your question title mentions âÂÂwithout changing the format of the fileâÂÂ, but your question body doesnâÂÂt elaborate.âÂÂIs your concern that the new lines should dynamically match the indentation of the existing line(s)?âÂÂIf so, see How can I tag text based on its indentation?
â G-Man
Jan 4 at 6:57
add a comment |Â
I missed it or you forgot to paste the file ? If I understand your needs you want to change line 5 whatever it contains ? sed -i '5s/pattern/newpattern/' file.xml would be ok then .
â francois P
Jan 3 at 21:47
How are you selecting line 5? Is it always line 5? or are you looking for a pattern on line 5? Do you want to change all lines with<function .* />
?
â Michael Daffin
Jan 3 at 22:06
Sorry for the confusion, in this example it is line 5.
â Shravankumar Daddolu
Jan 3 at 22:24
2
Can you use an XML editor, which is tailor-made to edit XML, rather than some shell pipeline, which are notoriously poor at parsing XML/XHTML?
â jayhendren
Jan 4 at 0:20
Your question title mentions âÂÂwithout changing the format of the fileâÂÂ, but your question body doesnâÂÂt elaborate.âÂÂIs your concern that the new lines should dynamically match the indentation of the existing line(s)?âÂÂIf so, see How can I tag text based on its indentation?
â G-Man
Jan 4 at 6:57
I missed it or you forgot to paste the file ? If I understand your needs you want to change line 5 whatever it contains ? sed -i '5s/pattern/newpattern/' file.xml would be ok then .
â francois P
Jan 3 at 21:47
I missed it or you forgot to paste the file ? If I understand your needs you want to change line 5 whatever it contains ? sed -i '5s/pattern/newpattern/' file.xml would be ok then .
â francois P
Jan 3 at 21:47
How are you selecting line 5? Is it always line 5? or are you looking for a pattern on line 5? Do you want to change all lines with
<function .* />
?â Michael Daffin
Jan 3 at 22:06
How are you selecting line 5? Is it always line 5? or are you looking for a pattern on line 5? Do you want to change all lines with
<function .* />
?â Michael Daffin
Jan 3 at 22:06
Sorry for the confusion, in this example it is line 5.
â Shravankumar Daddolu
Jan 3 at 22:24
Sorry for the confusion, in this example it is line 5.
â Shravankumar Daddolu
Jan 3 at 22:24
2
2
Can you use an XML editor, which is tailor-made to edit XML, rather than some shell pipeline, which are notoriously poor at parsing XML/XHTML?
â jayhendren
Jan 4 at 0:20
Can you use an XML editor, which is tailor-made to edit XML, rather than some shell pipeline, which are notoriously poor at parsing XML/XHTML?
â jayhendren
Jan 4 at 0:20
Your question title mentions âÂÂwithout changing the format of the fileâÂÂ, but your question body doesnâÂÂt elaborate.âÂÂIs your concern that the new lines should dynamically match the indentation of the existing line(s)?âÂÂIf so, see How can I tag text based on its indentation?
â G-Man
Jan 4 at 6:57
Your question title mentions âÂÂwithout changing the format of the fileâÂÂ, but your question body doesnâÂÂt elaborate.âÂÂIs your concern that the new lines should dynamically match the indentation of the existing line(s)?âÂÂIf so, see How can I tag text based on its indentation?
â G-Man
Jan 4 at 6:57
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
0
down vote
accepted
create the new file newcontent.txt. putting the content which you want to replace with.
Cat new content.txt
<function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
Use below command to get the content replaced as per your requirement
sed '5r newcontent.txt' example2.txt | sed âÂÂ5d'.
example2.txt==> original input file
newcontent.txt==> Consists of content which need to replaced
Output
<?xml version="1.0" encoding="UTF-8"?>
<TestConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Text>
<Checking state="BOOT" />
<function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user2" num="2" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA" />
<function name="configure" type="test" username="user3" num="3" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA"/>
<function name="configure" type="test" username="user4" num="4" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user5" num="5" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user6" num="7" Mode="YA" Type="WPA2-PSK" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
</Text>
</TestConfig>
add a comment |Â
up vote
1
down vote
awk 'NR!=5print NR==5 print " <function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">"; print " <parameters name="_key" value="password" isit="true" />"; print ""' /path/to/some.xml
Can we do this generic, irrespective of line number as 5. .
â Shravankumar Daddolu
Jan 3 at 22:25
Based on what set of criteria?
â DopeGhoti
Jan 3 at 22:42
let the same line be on line 6 or 7 not specifically at line 5, just a random line number, but the content will be same and only one entry per file
â Shravankumar Daddolu
Jan 4 at 1:45
In the OP, you ask specifically for how to change the fifth line with no reference to its content or the other similar lines in your example input file. Ask for what you want rather than telling people who give you what you ask for that they did not give you what you wanted.
â DopeGhoti
Jan 4 at 15:26
add a comment |Â
up vote
1
down vote
Don't use regular expressions to parse or edit XML. It doesn't work reliably, and can not be made to work reliably - even if you do hack up something that "works", even tiny changes or unexpected variations in the input XML file can break any regex-only script.
Instead, use either:
- a programming language such as
perl
orpython
(or many others) that have XML parsing libraries. - a command-line tool like xmlstarlet, which is available pre-packaged for most linux distributions and other unix-like systems.
If you really must use sed
or awk
, then use xml2 to convert the XML data into a line-oriented format suitable for processing with such tools, and then 2xml
to convert it back to properly formatted XML afterwards. e.g.
xml2 < yourxmlfile | your sed/awk script here | 2xml > newxmlfile
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
create the new file newcontent.txt. putting the content which you want to replace with.
Cat new content.txt
<function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
Use below command to get the content replaced as per your requirement
sed '5r newcontent.txt' example2.txt | sed âÂÂ5d'.
example2.txt==> original input file
newcontent.txt==> Consists of content which need to replaced
Output
<?xml version="1.0" encoding="UTF-8"?>
<TestConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Text>
<Checking state="BOOT" />
<function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user2" num="2" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA" />
<function name="configure" type="test" username="user3" num="3" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA"/>
<function name="configure" type="test" username="user4" num="4" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user5" num="5" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user6" num="7" Mode="YA" Type="WPA2-PSK" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
</Text>
</TestConfig>
add a comment |Â
up vote
0
down vote
accepted
create the new file newcontent.txt. putting the content which you want to replace with.
Cat new content.txt
<function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
Use below command to get the content replaced as per your requirement
sed '5r newcontent.txt' example2.txt | sed âÂÂ5d'.
example2.txt==> original input file
newcontent.txt==> Consists of content which need to replaced
Output
<?xml version="1.0" encoding="UTF-8"?>
<TestConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Text>
<Checking state="BOOT" />
<function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user2" num="2" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA" />
<function name="configure" type="test" username="user3" num="3" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA"/>
<function name="configure" type="test" username="user4" num="4" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user5" num="5" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user6" num="7" Mode="YA" Type="WPA2-PSK" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
</Text>
</TestConfig>
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
create the new file newcontent.txt. putting the content which you want to replace with.
Cat new content.txt
<function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
Use below command to get the content replaced as per your requirement
sed '5r newcontent.txt' example2.txt | sed âÂÂ5d'.
example2.txt==> original input file
newcontent.txt==> Consists of content which need to replaced
Output
<?xml version="1.0" encoding="UTF-8"?>
<TestConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Text>
<Checking state="BOOT" />
<function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user2" num="2" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA" />
<function name="configure" type="test" username="user3" num="3" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA"/>
<function name="configure" type="test" username="user4" num="4" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user5" num="5" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user6" num="7" Mode="YA" Type="WPA2-PSK" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
</Text>
</TestConfig>
create the new file newcontent.txt. putting the content which you want to replace with.
Cat new content.txt
<function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
Use below command to get the content replaced as per your requirement
sed '5r newcontent.txt' example2.txt | sed âÂÂ5d'.
example2.txt==> original input file
newcontent.txt==> Consists of content which need to replaced
Output
<?xml version="1.0" encoding="UTF-8"?>
<TestConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Text>
<Checking state="BOOT" />
<function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user2" num="2" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA" />
<function name="configure" type="test" username="user3" num="3" Mode="YA" Type="DYNAMIC-WEP" txt="NA-TO" TextType="employee" who="BANANNA"/>
<function name="configure" type="test" username="user4" num="4" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user5" num="5" Mode="YA" Type="WPA2-PSK" txt="CA-TO" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
<function name="configure" type="test" username="user6" num="7" Mode="YA" Type="WPA2-PSK" TextType="employee" who="BANANNA">
<parameters name="_key" value="password" isit="true" />
</function>
</Text>
</TestConfig>
answered Jan 4 at 3:05
Praveen Kumar BS
1,010128
1,010128
add a comment |Â
add a comment |Â
up vote
1
down vote
awk 'NR!=5print NR==5 print " <function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">"; print " <parameters name="_key" value="password" isit="true" />"; print ""' /path/to/some.xml
Can we do this generic, irrespective of line number as 5. .
â Shravankumar Daddolu
Jan 3 at 22:25
Based on what set of criteria?
â DopeGhoti
Jan 3 at 22:42
let the same line be on line 6 or 7 not specifically at line 5, just a random line number, but the content will be same and only one entry per file
â Shravankumar Daddolu
Jan 4 at 1:45
In the OP, you ask specifically for how to change the fifth line with no reference to its content or the other similar lines in your example input file. Ask for what you want rather than telling people who give you what you ask for that they did not give you what you wanted.
â DopeGhoti
Jan 4 at 15:26
add a comment |Â
up vote
1
down vote
awk 'NR!=5print NR==5 print " <function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">"; print " <parameters name="_key" value="password" isit="true" />"; print ""' /path/to/some.xml
Can we do this generic, irrespective of line number as 5. .
â Shravankumar Daddolu
Jan 3 at 22:25
Based on what set of criteria?
â DopeGhoti
Jan 3 at 22:42
let the same line be on line 6 or 7 not specifically at line 5, just a random line number, but the content will be same and only one entry per file
â Shravankumar Daddolu
Jan 4 at 1:45
In the OP, you ask specifically for how to change the fifth line with no reference to its content or the other similar lines in your example input file. Ask for what you want rather than telling people who give you what you ask for that they did not give you what you wanted.
â DopeGhoti
Jan 4 at 15:26
add a comment |Â
up vote
1
down vote
up vote
1
down vote
awk 'NR!=5print NR==5 print " <function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">"; print " <parameters name="_key" value="password" isit="true" />"; print ""' /path/to/some.xml
awk 'NR!=5print NR==5 print " <function name="configure" type="test" username="user1" num="1" Mode="YA" Type="WPA2-PSK" txt="NA-TO" TextType="guest" who="BANANNA">"; print " <parameters name="_key" value="password" isit="true" />"; print ""' /path/to/some.xml
answered Jan 3 at 22:08
DopeGhoti
40.5k54979
40.5k54979
Can we do this generic, irrespective of line number as 5. .
â Shravankumar Daddolu
Jan 3 at 22:25
Based on what set of criteria?
â DopeGhoti
Jan 3 at 22:42
let the same line be on line 6 or 7 not specifically at line 5, just a random line number, but the content will be same and only one entry per file
â Shravankumar Daddolu
Jan 4 at 1:45
In the OP, you ask specifically for how to change the fifth line with no reference to its content or the other similar lines in your example input file. Ask for what you want rather than telling people who give you what you ask for that they did not give you what you wanted.
â DopeGhoti
Jan 4 at 15:26
add a comment |Â
Can we do this generic, irrespective of line number as 5. .
â Shravankumar Daddolu
Jan 3 at 22:25
Based on what set of criteria?
â DopeGhoti
Jan 3 at 22:42
let the same line be on line 6 or 7 not specifically at line 5, just a random line number, but the content will be same and only one entry per file
â Shravankumar Daddolu
Jan 4 at 1:45
In the OP, you ask specifically for how to change the fifth line with no reference to its content or the other similar lines in your example input file. Ask for what you want rather than telling people who give you what you ask for that they did not give you what you wanted.
â DopeGhoti
Jan 4 at 15:26
Can we do this generic, irrespective of line number as 5. .
â Shravankumar Daddolu
Jan 3 at 22:25
Can we do this generic, irrespective of line number as 5. .
â Shravankumar Daddolu
Jan 3 at 22:25
Based on what set of criteria?
â DopeGhoti
Jan 3 at 22:42
Based on what set of criteria?
â DopeGhoti
Jan 3 at 22:42
let the same line be on line 6 or 7 not specifically at line 5, just a random line number, but the content will be same and only one entry per file
â Shravankumar Daddolu
Jan 4 at 1:45
let the same line be on line 6 or 7 not specifically at line 5, just a random line number, but the content will be same and only one entry per file
â Shravankumar Daddolu
Jan 4 at 1:45
In the OP, you ask specifically for how to change the fifth line with no reference to its content or the other similar lines in your example input file. Ask for what you want rather than telling people who give you what you ask for that they did not give you what you wanted.
â DopeGhoti
Jan 4 at 15:26
In the OP, you ask specifically for how to change the fifth line with no reference to its content or the other similar lines in your example input file. Ask for what you want rather than telling people who give you what you ask for that they did not give you what you wanted.
â DopeGhoti
Jan 4 at 15:26
add a comment |Â
up vote
1
down vote
Don't use regular expressions to parse or edit XML. It doesn't work reliably, and can not be made to work reliably - even if you do hack up something that "works", even tiny changes or unexpected variations in the input XML file can break any regex-only script.
Instead, use either:
- a programming language such as
perl
orpython
(or many others) that have XML parsing libraries. - a command-line tool like xmlstarlet, which is available pre-packaged for most linux distributions and other unix-like systems.
If you really must use sed
or awk
, then use xml2 to convert the XML data into a line-oriented format suitable for processing with such tools, and then 2xml
to convert it back to properly formatted XML afterwards. e.g.
xml2 < yourxmlfile | your sed/awk script here | 2xml > newxmlfile
add a comment |Â
up vote
1
down vote
Don't use regular expressions to parse or edit XML. It doesn't work reliably, and can not be made to work reliably - even if you do hack up something that "works", even tiny changes or unexpected variations in the input XML file can break any regex-only script.
Instead, use either:
- a programming language such as
perl
orpython
(or many others) that have XML parsing libraries. - a command-line tool like xmlstarlet, which is available pre-packaged for most linux distributions and other unix-like systems.
If you really must use sed
or awk
, then use xml2 to convert the XML data into a line-oriented format suitable for processing with such tools, and then 2xml
to convert it back to properly formatted XML afterwards. e.g.
xml2 < yourxmlfile | your sed/awk script here | 2xml > newxmlfile
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Don't use regular expressions to parse or edit XML. It doesn't work reliably, and can not be made to work reliably - even if you do hack up something that "works", even tiny changes or unexpected variations in the input XML file can break any regex-only script.
Instead, use either:
- a programming language such as
perl
orpython
(or many others) that have XML parsing libraries. - a command-line tool like xmlstarlet, which is available pre-packaged for most linux distributions and other unix-like systems.
If you really must use sed
or awk
, then use xml2 to convert the XML data into a line-oriented format suitable for processing with such tools, and then 2xml
to convert it back to properly formatted XML afterwards. e.g.
xml2 < yourxmlfile | your sed/awk script here | 2xml > newxmlfile
Don't use regular expressions to parse or edit XML. It doesn't work reliably, and can not be made to work reliably - even if you do hack up something that "works", even tiny changes or unexpected variations in the input XML file can break any regex-only script.
Instead, use either:
- a programming language such as
perl
orpython
(or many others) that have XML parsing libraries. - a command-line tool like xmlstarlet, which is available pre-packaged for most linux distributions and other unix-like systems.
If you really must use sed
or awk
, then use xml2 to convert the XML data into a line-oriented format suitable for processing with such tools, and then 2xml
to convert it back to properly formatted XML afterwards. e.g.
xml2 < yourxmlfile | your sed/awk script here | 2xml > newxmlfile
answered Jan 4 at 2:33
cas
37.7k44394
37.7k44394
add a comment |Â
add a comment |Â
I missed it or you forgot to paste the file ? If I understand your needs you want to change line 5 whatever it contains ? sed -i '5s/pattern/newpattern/' file.xml would be ok then .
â francois P
Jan 3 at 21:47
How are you selecting line 5? Is it always line 5? or are you looking for a pattern on line 5? Do you want to change all lines with
<function .* />
?â Michael Daffin
Jan 3 at 22:06
Sorry for the confusion, in this example it is line 5.
â Shravankumar Daddolu
Jan 3 at 22:24
2
Can you use an XML editor, which is tailor-made to edit XML, rather than some shell pipeline, which are notoriously poor at parsing XML/XHTML?
â jayhendren
Jan 4 at 0:20
Your question title mentions âÂÂwithout changing the format of the fileâÂÂ, but your question body doesnâÂÂt elaborate.âÂÂIs your concern that the new lines should dynamically match the indentation of the existing line(s)?âÂÂIf so, see How can I tag text based on its indentation?
â G-Man
Jan 4 at 6:57