Андрей Ка
Андрей Ка

Reputation: 784

Bash change number to another value on specific line

i'm new with bash scripting , and i looking for solution to change a number to another value on specific line. I have file named foo.config and in this file i have about 100 lines of configuration. For example i have

<UpdateInterval>2</UpdateInterval>

and i need to find this line on foo.config and replace number(this can be number for 0 to 10 and for my example is 2) for 0 as always.

Like this : <UpdateInterval>0</UpdateInterval>

How can i do it with sed ? please suggest

the part of lines:

<InstallUrl />
<TargetCulture>en</TargetCulture>
<ApplicationVersion>1.0.1.8</ApplicationVersion>
<AutoIncrementApplicationRevision>true</AutoIncrementApplicationRevision>
<UpdateEnabled>true</UpdateEnabled>
<UpdateInterval>2</UpdateInterval>
<UpdateIntervalUnits>hours</UpdateIntervalUnits>
<ProductName>xxxxxxxxxxxx</ProductName>
<PublisherName />
<SupportUrl />
<FriendlyName>xxxxxxxxxxxx</FriendlyName>
<OfficeApplicationDescription />
<LoadBehavior>3</LoadBehavior>

Upvotes: 1

Views: 818

Answers (3)

Javier Elices
Javier Elices

Reputation: 2154

In a very simple way, you may try:

sed -E 's/^<UpdateInterval>[0-9]+/<UpdateInterval>0/' foo.config

This will search for <UpdateInterval> at the beginning of a line (note the ^) and then a number ([0-9] stands for a digit and + for a repetition of one or more). This bit will be replaced with <UpdateInterval>0. The / characters separate what you search and what will replace it. The s command is a search and replace.

It will take the file foo.config as input and you will get the output on standard output. If you want your output on the same file, you may do:

sed -E 's/^<UpdateInterval>[0-9]+/<UpdateInterval>0/' foo.config >foo.temp
mv foo.temp foo.config

Or more simply:

sed -i -E 's/^<UpdateInterval>[0-9]+/<UpdateInterval>0/' foo.config

Note that this is not a good way to do the substitution if your config file contains general XML. It will only work in the simplest of cases (but will do for your example.) If your XML bit may be in the middle of a line, remove the ^ character. The search and replace expression assumes that there is no whitespace around the XML tags.

Upvotes: 1

RomanPerekhrest
RomanPerekhrest

Reputation: 92894

sed and others(grep, awk) never be a good tools for parsing xml/html data. Use a proper xml/html parsers, like xmlstarlet:

xmlstarlet ed -L -O -u "//UpdateInterval" -v 0 foo.config
  • ed - edit mode

  • -L - edit the file inplace

  • -O - omit xml declaration
  • -u - update action
  • "//UpdateInterval" - xpath expression
  • -v 0 - the new value of the element to be updated

The final (exemplary) foo.config contents:

<root>
  <InstallUrl/>
  <TargetCulture>en</TargetCulture>
  <ApplicationVersion>1.0.1.8</ApplicationVersion>
  <AutoIncrementApplicationRevision>true</AutoIncrementApplicationRevision>
  <UpdateEnabled>true</UpdateEnabled>
  <UpdateInterval>0</UpdateInterval>
  <UpdateIntervalUnits>hours</UpdateIntervalUnits>
  <ProductName>xxxxxxxxxxxx</ProductName>
  <PublisherName/>
  <SupportUrl/>
  <FriendlyName>xxxxxxxxxxxx</FriendlyName>
  <OfficeApplicationDescription/>
  <LoadBehavior>3</LoadBehavior>
</root>

The <root> tag was specified for demonstration purpose, your xml/html structure should have its own "root"(most parent) tag

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 247200

A solution using an XML parsing tool:

{ echo '<root>'; cat foo.config; echo '</root>'; } |
  xmlstarlet ed -O -P -u //UpdateInterval -v 0 |
  sed '1d;$d' |
  sponge foo.config

The first line is to make the config file into a proper XML file.
The second line updates the value.
The third line removes the root tags.
The last line rewrites the config file. Need to install the moreutils package.

Upvotes: 0

Related Questions