# File editing using builtin commands

**URL:** <https://forum.ansible.com/t/file-editing-using-builtin-commands/8153>\
**Category:** Get Help\
**Created:** [August 28, 2024, 10:58pm UTC](https://forum.ansible.com/t/file-editing-using-builtin-commands/8153 "2024-08-28T22:58:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lb483](https://avatars.discourse-cdn.com/v4/letter/l/3ab097/32.png) [@lb483](https://forum.ansible.com/u/lb483)\
**Post date:** [August 28, 2024, 10:58pm UTC](https://forum.ansible.com/t/file-editing-using-builtin-commands/8153/1 "2024-08-28T22:58:39Z")

</div>

I have a requirement to update a file on a Linux box and as I am performing a number of other updates using ansible, I’m hoping to use the same methodology. However… I can’t work out if what I’m trying to do is possible.

The file format is similar to:

```auto
<numerous lines of code>
# arrays
for x in \
    a \
    b \
    c
do
    ...
done

# integers
for x in \
    d \
    e \
    f
do
    ...
done

# strings
for x in \
    g \
    h \
    i
do
    ...
done

```

What I require is:

- after: ‘^# integer’
- before: ‘^do’
- regex: ‘$’
- replace: ’ \’

and:

- after: ‘^# integer’
- insertbefore: ‘^do’
- line: ’ j’

This would result in:

```auto
<numerous lines of code>
# arrays
for x in \
    a \
    b \
    c
do
    ...
done

# integers
for x in \
    d \
    e \
    f \
    j
do
    ...
done

# strings
for x in \
    g \
    h \
    i
do
    ...
done

```

Is anything like this possible? At the moment my solution is to run the “ex” command with input from a command file. This works but isn’t self-contained.

Thanks.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [August 29, 2024, 12:22am UTC](https://forum.ansible.com/t/file-editing-using-builtin-commands/8153/2 "2024-08-29T00:22:34Z")

</div>

> [@lb483](#):
>
> Is anything like this possible?

[Mu](http://www.catb.org/esr/jargon/html/M/mu.html). By which I mean, if you want to solve the problem in an Ansible way, you’ll need to change the nature, the context, of the problem. Otherwise, your `ex` command file is going to be at least as elegant as any pure Ansible solution you might come up with.

What comes to mind is that you have three arrays, currently containing `[a b c]`, `[d e f]`, and` [g h i]`. That’s data, not code. Separate your data from code, and manage the data through Ansible. Template out the data into three files that your shell script (I mean, it _looks like_ a shell script to me) can ingest with

```sh
mapfile -t arrays < arrays.lst
mapfile -t integers < integers.lst
mapfile -t strings < strings.lst

```

Then your loops become

```sh
for x in "${arrays[@]}"
do
    …
done

```

But this requires you to take ownership of the data, and thus changes the nature and context of the problem. But the solution changes from being, frankly, ugly regardless of the tool(s) employed to something quite straightforward to address with Ansible.

If this or some other change along those lines is not an option, then congratulations on your `ex` command file solution. I don’t think you’ll do better.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [September 28, 2024, 12:23am UTC](https://forum.ansible.com/t/file-editing-using-builtin-commands/8153/3 "2024-09-28T00:23:16Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
