0

Let consider this string:

00x\00x\00x\00x\00x\00x\00x\00x\00x\g09x\t20x\00x\00x\00x\

What I want to retrieve is this:

00x\00x\00x\00x\00x\00x\00x\00x\00x\g09x\t20x\

Basically, the logic is:

  1. As long as it's 00x\ keep reading the remaining of the string.

  2. As long as it's not 00x\ keep reading the remaining of the string. Split there.

How can this be achieved in bash? Pay attention that there is a "9" in the middle, and a "t". So there might be "garbage" between 2 00x\ tokens. So I can't just split the string into tokens, not I can use cut (not fixed length). Any magic I can do with awk or sed?

Thanks.

Edit: The input string can after other sings after the 00x\. Like this: 00x\00x\00x\00x\00x\00x\00x\00x\00x\g09x\t20x\00x\00x\00x\00x\00x\00x\00x\00x\00x\GL7Dx\00x\00x\00x\00x\00x\00x\00x\00x\00x\00x\BCx\V6Ax\00x\00x\00x\00x\00x\00x\00x\00x\00x\00x\H50x\ where what I want is still 00x\00x\00x\00x\00x\00x\00x\00x\00x\g09x\t20x\

jmspaggi
  • 113
  • 1
  • 7

1 Answers1

1

Something in awk:

$ awk '
BEGIN {
    FS=ORS="\\"
}
{
    for(i=1;i<=NF;i++)
        if(($i=="00x")&&p!="00x"&&p!="") {
            printf "\n"
            exit
        } else {
            p=$i
            print $i
        }
}' file

Output on the updated data

00x\00x\00x\00x\00x\00x\00x\00x\00x\g09x\t20x\

In perl using negative lookbehind:

$ perl -ne 's/(?<!00x)\\00x.*/\\/g;print' file
00x\00x\00x\00x\00x\00x\00x\00x\00x\g09x\t20x\
James Brown
  • 36,089
  • 7
  • 43
  • 59