Home > Enterprise >  How to use sed/grep to extract text between two diffrent strings ( the 2 strings) that contain &quo
How to use sed/grep to extract text between two diffrent strings ( the 2 strings) that contain &quo

Time:12-25

I am trying to extract the text between two strings that contain the symbols / and *. I believe this is the reason the regex commands break. So my starting string is "/25043*" and my ending string is "*25043/". I have the extract the text between these two strings (including these 2 strings) from a text file.

I've tried this using : grep -o -P '(?<="/25043*").*(?="*25043/")' code.txt

which gives me an error : "grep: lookbehind assertion is not fixed length"

My input file:

#include <stdio.h>
#include <stdlib.h>

#define MAX 256
/25043/definiramo konstanto MAX

int main(){
    /25043*Vstopna tocka programa
    Ne uporabljamo parameterov pri main, kjer jih ne potrebujemo

    *25043/
    printf("");/25043/izpis vrednosti konstante MAX

    return 0;
}
/25043/Koncamo z izvajanjem programa

My wanted output:

/25043*Vstopna tocka programa
        Ne uporabljamo parameterov pri main, kjer jih ne potrebujemo
    
*25043/

CodePudding user response:

have you tried to do it without grouping? like so :

example assumption of code.txt:

asdf asdfasd /25403* asdf *25043/ asdfa asdfa

command:

grep -o '/25403\* .* \*25043/' code.txt

result is :

/25403* asdf *25043/

CodePudding user response:

The solution to the problem was using sed instead of grep.

Used answer: sed -n '/\/25043\*/,/\*25043\//{p}' code.txt

  • Related