Search for a text between two strings in a file using regex

Here is my sample file data:

My requirement is to have a regex expression that is able to search for visible starting string "SSLInsecureRenegotiation Off" between strings "<VirtualHost " and "</VirtualHost>".

In the sample data two lines should be matched.

Below is what I tried but it does not match the two lines within the Virtual Tags.

(?<=<VirtualHost ).*^[^#]*SSLInsecureRenegotiation.*Off.*(?=</VirtualHost>)

Can you please suggest how ?

This would be easy if you did not try to make these text processing tasks "one liners" and just write
the (few lines) code to process the file one line at a time, using any programming language you like.

Basically, if you just processed this text in a loop, reading each line at a time, matching flags and setting patterns, you could have easily processed this file. (Or read the file into an array of lines of text.)

The issue, as I see it, is you (not only you, but many) are falling into the "trap" of looking for "one liners" instead of just writing a small program of a few lines which does the trick.

You are not the only person who falls in to the trap of thinking that everything has to be a "one liner" but this will cause you to waste time when you could write a few lines of code in any programming language and most shell scripts to:

  • Read the file into an array of lines.
  • Process each line and search for your beginning <VirtualHost tag and set a flag.
  • When the flag is set, search and match the other string(s) ( SSLInsecureRenegotiation ... blah blah ) and put the match(es) in an array.
  • Stop processing after the end tag </VirtualHost is matched.

This is only a few lines of code and is very easy for you (or anyone with minimal programming skills) to write and you could have easily written this code in the time it takes to search for a "one liners" to do the job.

I'm not trying to give you a hard time and I like your posts; but I'm just saying. For a guy with nearly 1000 posts here; you should just write a handful of lines of code and process this versus wasting your time searching for the perfect "one-liner" REGEX.

Cheers.

I can't see what program your regex was written for. Pls learn to mention ALL relevant details in your spec. For sed , try

sed -n '/<VirtualHost/,/<\/VirtualHost>/ {/^[^#]*SSLInsecureRenegotiation.*Off/p}' file
SSLInsecureRenegotiation Off
SSLInsecureRenegotiation Off

I need regex solution for grep and not sed as i m using a tool called Ansible which has a module called as "lineinfile" module which i guess uses underlying python to interpret the regex.

I use regex101.com to test the sample data before introducing it to my Ansible code.

Ansible could execute any shell command you like. You do not have to use lineinfile .

See: shell - Execute shell commands on targets - Ansible Documentation

Exactly, but frankly was too busy to keep going back and forth with a system admin who only sees things his way, right or wrong :slight_smile: making incorrect arguments to justify what he wants to do, versus all the possibilities which can be done, quicker and easier! So, I was trying to avoid days and days of questions and posting back and forth, making a simple task which could be coded in few minutes, more complex because of artificial constraints; instead of writing a few simple line of "easy code" and be done with the task in 10 minutes :slight_smile:

In the examples in the link stomp kindly shared, the script can be "anything" including calling python (for example) if someone liked python :slight_smile: ... or anything under the sun, just about.

name: Execute the command in remote shell; stdout goes to the specified file on the remote.
  shell: somescript.sh >> somelog.txt

- name: Change the working directory to somedir/ before executing the command.
  shell: somescript.sh >> somelog.txt
  args:
    chdir: somedir/

# You can also use the 'args' form to provide the options.
- name: This command will change the working directory to somedir/ and will only run when somedir/somelog.txt doesn't exist.
  shell: somescript.sh >> somelog.txt
  args:
    chdir: somedir/
    creates: somelog.txt

# You can also use the 'cmd' parameter instead of free form format.
- name: This command will change the working directory to somedir/.
  shell:
    cmd: ls -l | grep log
    chdir: somedir/

- name: Run a command that uses non-posix shell-isms (in this example /bin/sh doesn't handle redirection and wildcards together but bash does)
  shell: cat < /tmp/*txt
  args:
    executable: /bin/bash

- name: Run a command using a templated variable (always use quote filter to avoid injection)
  shell: cat {{ myfile|quote }}

# You can use shell to run other executables to perform actions inline
- name: Run expect to wait for a successful PXE boot via out-of-band CIMC
  shell: |
    set timeout 300
    spawn ssh admin@{{ cimc_host }}

    expect "password:"
    send "{{ cimc_password }}\n"

    expect "\n{{ cimc_name }}"
    send "connect host\n"

    expect "pxeboot.n12"
    send "\n"

    exit 0
  args:
    executable: /usr/bin/expect
  delegate_to: localhost

# Disabling warnings
- name: Using curl to connect to a host via SOCKS proxy (unsupported in uri). Ordinarily this would throw a warning.
  shell: curl --socks5 localhost:9000 http://www.ansible.com
  args:
    warn: no