sed - searching token in certain order

Hello.
I would like to write a bash function which would return "true" if the search succeed else return anything else.

something like

if [[ "$(my_funct  a_file_name  a_token1  a_token2  a_token3 a_token4)" != "true" ]] ; then

    exit 1

fi

function my_funct () {

    find first occurrence $2 in $1
    if not found return "false"
    from that position, find first occurrence $3 in any following line
           but before founding $5
           if not found return "false"
    from that position, find first occurrence $4 in the same line
    if not found return "false"
    return "true"

}

$1 : The file, where to search in, is a text file.

$2 : must start at the beginning of a line and can contains / or ' like :

 ### /a/path/name ###
    or
menuentry  'software version.number'

$3 : must begin at the beginning of a line, or begin with a tab, or begin with a space like :

-->a_string
 --> a_string
--> a_string

$4 : is any single word or init 3

$5 : is a single character but could be }

I find your description cryptic. Please provide a few sample input files, sample arguments to your function for those input files, and the output you want to have produced by your function for each of those samples.

You have sed in the title of this thread, but it sounds like this might be easier using awk. Will you accept a solution using something other than sed?

I know little about sed, I know nothing about awk.

I have try this :

my_function () {
sed -n '/^$1/,$p  $2 | sed -n '/^$3/,p' | sed '/$5/ q'
}

In the following text sample :

I find your description cryptic.
Please provide a few sample input files, sample arguments to your function for those input files, and the output you want to have produced by your function for each of those samples.
You have sed in the title of this thread, but it sounds like this might be easier using awk. Will you accept a solution using something other than sed?
systemd is a system management daemon designed exclusively for the Linux kernel.
In the Linux startup process, it is the first process to execute in user land; therefore, it is also the parent process of all child processes in user land.
systemd was developed for Linux to replace the init system inherited from UNIX System V and Berkeley Software Distribution (BSD) operating systems.
Like init, systemd is a daemon that manages other daemons.
All daemons, including systemd, are background processes.
systemd is the first daemon to start (during booting) and the last daemon to terminate (during shutdown).
Lennart Poettering and Kay Sievers, the software engineers who initially developed systemd,[1] sought to surpass the efficiency of the init daemon in several ways.
They wanted to improve the software framework for expressing dependencies; to allow more processing to be done concurrently or in parallel during system booting; and to reduce the computational overhead of the shell.

Now calling the function with these parameters

my_function "In the Linux startup process" "./file_text.txt" "Like init" "manages" "ways"

This find $1 in file $2, and then find $3 before $5.
This give a few lines beginning with $3 and last line ending with $5

Like init, systemd is a daemon that manages other daemons.
All daemons, including systemd, are background processes.
systemd is the first daemon to start (during booting) and the last daemon to terminate (during shutdown).
Lennart Poettering and Kay Sievers, the software engineers who initially developed systemd,[1] sought to surpass the efficiency of the init daemon in several ways

Now searching with $4 = manages in the first line will return true
and searching with $4 = background in the first line will return false

Thank you for taking time to help me.

That didn't answer the question. Will you accept a solution for this problem that uses awk?

There are a few problems here:

  1. You're using the wrong quotes to do what you want. (Positional parameters are not expanded inside single-quoted strings.)
  2. Your quotes aren't matched. (You have 5 single quotes in these 3 sed commands.)
  3. In the 1st message in this thread you said the 1st argument to your function was the file name; here it is the 2nd argument. Which should it be?
  4. In the 1st message in this thread you said the 5th argument was a single character; "ways" is not a single character. What is the real requirement?
  5. In the 1st message in this thread you said the 3rd argument had to appear at the start of a line, after a <space> character, or after a <tab> character. Your script seems to only allow for the 1st of these three options. Is this still a requirement?

Do you really need to return the strings "true" and "false"; or are exit codes zero and non-zero, respectively, sufficient.

yes of course.

---------- Post updated at 19:29 ---------- Previous update was at 19:21 ----------

Is there something more clean but readable :

my_function () {
CMD="COUNT=\$(sed -n '/^$1/,\$p'  $2 | sed -n '/^$3/,\$p' | sed '/$5/ q'  | sed -n '1p' | grep -c '$4'   )"
eval $CMD
if [[ $COUNT -gt 0 ]] ; then
    return 0
else
    return 1
fi
}

asus:~ # my_function  "In the Linux startup process"  "/root/Documents/file_text.txt" "Like init" "manage" "ways"
asus:~ # if [[ $? -eq 0 ]] ; then 
> echo "success"
> else 
> echo "failed"
> fi
success
asus:~ # 

asus:~ # my_function  "In the Linux startup process"  "/root/Documents/file_text.txt" "Like init" "blablabla" "ways"
asus:~ # if [[ $? -eq 0 ]] ; then 
> echo "success"
> else 
> echo "failed"
> fi
failed
asus:~ # 

This is the set of requirements you stated in your first message in this thread. Since then you have switched the 1st two arguments to your function (ignoring my question about whether or not that was intentional). The requirements in red above are completely ignored in your recent shell functions.

Do you still care about these any of these original requirements?

My last post show what I have tried before I post my question.

And the pseudo code show exactly the problem:
1�) There is one parameter ($1) for the file name : "/root/Documents/file_text.txt" in the example
2�) There is one parameter ($2) for the first token : "In the Linux startup process" in the example
So we can ignore the begin of the text file
3�) There is one parameter ($3) for the second token : "Like init"
So we can search safely from this second token because we are protected by the first token and because we know that the second token appear only once after the first token. So we can ignore all the text until this second token after having found the first token.
4�) There is one parameter ($5) for the last token : "ways" in the example
So we stop searching beyond this token.
As I have no idea how to pass the character " \ " or " } " or " ' " or " . " as token, I have make my test with a single word.
5�) There is one parameter ($4) for the token to search for : "manage" in the example.
This token could start at the very beginning of a line, after a blank or after a tab ( some thing like [ ^ | \t | ]).
As I don't know how to represent this ORed expression, I give my example with a simple word.

As you can see, my pseudo code is unchanged.

What I would like is :
1�) make simpler

my_function () {
CMD="COUNT=\$(sed -n '/^$1/,\$p'  $2 | sed -n '/^$3/,\$p' | sed '/$5/ q'  | sed -n '1p' | grep -c '$4'   )"
eval $CMD
if [[ $COUNT -gt 0 ]] ; then
    return 0
else
    return 1
fi
}

2�) how to pass a \, a }, a ' as $5
3�) how to pass [^ | \t | space ]token_to_pass as $4