Content should be filtered within brackets

Hello all,

I have a string and would like to extract the content of the text within the brackets.

Here is the string:
Desc="file[4009821_737498.out] from to line[12965, 12355]"

What I would like to have is the following:

Filename="4009821_737498.out"
FromLine=12965
ToLine=12355

Maybe I have to do this with 'sed' and with a reg expression. Could you tell me how such a command will look like?

CU,
API

Hello API,

Following may help you in same.

 awk '{A=B=$0;match($0,/file\[.*\] +/);file=substr($0,RSTART+5,RLENGTH-7);match(A,/line\[.*\,/);from=substr(A,RSTART+5,RLENGTH-6);match(B,/\, .*/);to=substr(B,RSTART+2,RLENGTH-4)}END{print "Filename=" s1 file s1 ORS "FromLine=" s1 from s1 ORS "ToLine=" s1 from s1}' s1="\""  Input_file
 

Output will be as follows.

Filename="4009821_737498.out"
FromLine="12965"
ToLine="12965"
 

EDIT: Adding a non one-liner form of solution as follows, where test34 is the input file.

 cat test34.ksh
awk '{
        A=B=$0;
        match($0,/file\[.*\] +/);
        file=substr($0,RSTART+5,RLENGTH-7);
        match(A,/line\[.*\,/);
        from=substr(A,RSTART+5,RLENGTH-6);
        match(B,/\, .*/);
        to=substr(B,RSTART+2,RLENGTH-4)
     }
        END{
                print "Filename=" s1 file s1 ORS "FromLine=" s1 from s1 ORS "ToLine=" s1 from s1
           }
    ' s1="\"" test34 

Thanks,
R. Singh

Try also

sed 's/^.*file\[/Filename="/; s/]/"\n/; s/ from to line\[/FromLine=/;s/, /\nToLine=/;s/]"//' file

Hello RavinderSingh13,

thanks for this...

This solution works well except for the second value "ToLine" - there I have the same value as for "FromLine"?

Is it possible to get there the 2nd value?

CU,API

Hello API,

I am sorry for using the same variable in both the places, following may help you in same.

 cat test34.ksh
awk '{
        A=B=$0;
        match($0,/file\[.*\] +/);
        file=substr($0,RSTART+5,RLENGTH-7);
        match(A,/line\[.*\,/);
        from=substr(A,RSTART+5,RLENGTH-6);
        match(B,/\, .*/);
        to=substr(B,RSTART+2,RLENGTH-4)
     }
        END{
                print "Filename=" s1 file s1 ORS "FromLine=" s1 from s1 ORS "ToLine=" s1 to s1
           }
    ' s1="\"" test34
 

Output is as follows.

Filename="4009821_737498.out"
FromLine="12965"
ToLine="12355"
 

Thanks,
R. Singh

1 Like

something along these lines:

awk -F'[][,]' '{printf("Filename=%s%s%s\nFromLine=%s%s%s\nToLine=%s%s%s\n", qq,$2,qq,qq,$(NF-2),qq,qq,$(NF-1),qq)}' qq='"' myFile
1 Like

Hello RavinderSingh13,

many thanks for this. Now it works fine...

CU,
API

---------- Post updated at 03:24 PM ---------- Previous update was at 03:21 PM ----------

Hello vgersh99,

also I quite good solution. It works, but I have to look into it a bit deeper - some features I does not understand. But it works...

CU,
API

---------- Post updated at 03:29 PM ---------- Previous update was at 03:24 PM ----------

Hello RudiC,

at the moment I have a working solution - thanks for you help anyway.

But I have question regarding your suggestion. When I tried it, I got the following output:

Filename="4009821_737498.out"nFromLine=12965nToLine=12355]

The line-end (\n) does not work this time. And the bracket at the end is not necessary.

To split the line at (n) or maybe another sign is not the problem, but why the bracket appears?

CU,
API