Skip to main content
added 3 characters in body
Source Link
user232326
user232326

Reading again your description I understand that you want the first match of pattern B from the bottom up until (going up) the first match of pattern A. But the resulting sections should be in the order that the file has.

That requires a lot of logic. The following shell script does it all. Will place the results in the correct internal order in files E and some number, first file (E1) will have the first match from the top, last file will have the last match section.

#!/bin/bash

rm -rf resE* E*

tac ../example_file.txt |
    awk 'BEGIN{i=1}
         /^AK5\*R.*/{p=1}
         {if(p==1){f="resE" i;print($0)>>f;close(f)}}
         /^AK2.*/{if(p==1){i++};p=0}
        '
set -- resE* 
c=$#
for (( i=1;i<=$c;i++)); do
    pos=$(($c-$i+1))
    [ -f "$1" ] && tac "$1" > "E$pos"
    shift
done

The resulting ranges will be:

$ cat E1
AK2*777*7777777
AK3*S6*5**3
AK3*A2*5**3
AK4*3*6969*4
AK4*7*6969*4
AK5*R*5

$ cat E2
AK2*777*7777777
AK3*J7*5**3
AK4*3*6969*4
AK5*R*5

Reading again your description I understand that you want the first match of pattern B from the bottom up until (going up) the first match of pattern A. But the resulting sections should be in the order that the file has.

That requires a lot of logic. The following shell script does it all. Will place the results in the correct internal order in files E and some number, first file (E1) will have the first match from the top, last file will have the last match section.

#!/bin/bash

rm -rf resE*

tac ../example_file.txt |
    awk 'BEGIN{i=1}
         /^AK5\*R.*/{p=1}
         {if(p==1){f="resE" i;print($0)>>f;close(f)}}
         /^AK2.*/{if(p==1){i++};p=0}
        '
set -- resE* 
c=$#
for (( i=1;i<=$c;i++)); do
    pos=$(($c-$i+1))
    [ -f "$1" ] && tac "$1" > "E$pos"
    shift
done

The resulting ranges will be:

$ cat E1
AK2*777*7777777
AK3*S6*5**3
AK3*A2*5**3
AK4*3*6969*4
AK4*7*6969*4
AK5*R*5

$ cat E2
AK2*777*7777777
AK3*J7*5**3
AK4*3*6969*4
AK5*R*5

Reading again your description I understand that you want the first match of pattern B from the bottom up until (going up) the first match of pattern A. But the resulting sections should be in the order that the file has.

That requires a lot of logic. The following shell script does it all. Will place the results in the correct internal order in files E and some number, first file (E1) will have the first match from the top, last file will have the last match section.

#!/bin/bash

rm -rf resE* E*

tac ../example_file.txt |
    awk 'BEGIN{i=1}
         /^AK5\*R.*/{p=1}
         {if(p==1){f="resE" i;print($0)>>f;close(f)}}
         /^AK2.*/{if(p==1){i++};p=0}
        '
set -- resE* 
c=$#
for (( i=1;i<=$c;i++)); do
    pos=$(($c-$i+1))
    [ -f "$1" ] && tac "$1" > "E$pos"
    shift
done

The resulting ranges will be:

$ cat E1
AK2*777*7777777
AK3*S6*5**3
AK3*A2*5**3
AK4*3*6969*4
AK4*7*6969*4
AK5*R*5

$ cat E2
AK2*777*7777777
AK3*J7*5**3
AK4*3*6969*4
AK5*R*5
Adapted script to new requirements of match from bottom up.
Source Link
user232326
user232326

AssumingReading again your description I understand that you really want to extract fromthe first match of pattern B from the bottom up until (going up) the first match of pattern A this awk. But the resulting sections should be in the order that the file has.

That requires a lot of logic. The following shell script willdoes it all. Will place each rangethe results in file E1the correct internal order in files E and some number, E2first file (E1) will have the first match from the top, … etclast file will have the last match section.

$#!/bin/bash

rm -rf resE*

tac ../example_file.txt |
    awk 'BEGIN{i=1};
         /^AK2^AK5\*R.*/{p=1};
         {if(p==1){f="E"f="resE" i;print($0)>>f;close(f)}};
         /^AK5\*R^AK2.*/{p=0;i++if(p==1){i++};p=0}
        '
set file-- resE* 
c=$#
for (( i=1;i<=$c;i++)); do
    pos=$(($c-$i+1))
    [ -f "$1" ] && tac "$1" > "E$pos"
    shift
done

The resulting ranges will be:

$ cat E1
AK2*777*6666666
AK5*A
AK2*777*7777777
AK3*S6*5**3
AK3*A2*5**3
AK4*3*6969*4
AK4*7*6969*4
AK5*R*5

$ cat E2
AK2*777*6666666
AK5*A
AK2*777*7777777
AK3*J7*5**3
AK4*3*6969*4
AK5*R*5

Assuming you really want to extract from pattern B until pattern A this awk script will place each range in file E1, E2, … etc

$ awk 'BEGIN{i=1};/^AK2.*/{p=1};{if(p==1){f="E" i;print($0)>>f;close(f)}};/^AK5\*R.*/{p=0;i++}' file

$ cat E1
AK2*777*6666666
AK5*A
AK2*777*7777777
AK3*S6*5**3
AK3*A2*5**3
AK4*3*6969*4
AK4*7*6969*4
AK5*R*5

$ cat E2
AK2*777*6666666
AK5*A
AK2*777*7777777
AK3*J7*5**3
AK4*3*6969*4
AK5*R*5

Reading again your description I understand that you want the first match of pattern B from the bottom up until (going up) the first match of pattern A. But the resulting sections should be in the order that the file has.

That requires a lot of logic. The following shell script does it all. Will place the results in the correct internal order in files E and some number, first file (E1) will have the first match from the top, last file will have the last match section.

#!/bin/bash

rm -rf resE*

tac ../example_file.txt |
    awk 'BEGIN{i=1}
         /^AK5\*R.*/{p=1}
         {if(p==1){f="resE" i;print($0)>>f;close(f)}}
         /^AK2.*/{if(p==1){i++};p=0}
        '
set -- resE* 
c=$#
for (( i=1;i<=$c;i++)); do
    pos=$(($c-$i+1))
    [ -f "$1" ] && tac "$1" > "E$pos"
    shift
done

The resulting ranges will be:

$ cat E1
AK2*777*7777777
AK3*S6*5**3
AK3*A2*5**3
AK4*3*6969*4
AK4*7*6969*4
AK5*R*5

$ cat E2
AK2*777*7777777
AK3*J7*5**3
AK4*3*6969*4
AK5*R*5
Added cat of files generated by awk.
Source Link
user232326
user232326

Assuming you really want to extract from pattern B until pattern A this awk script will place each range in file E1, E2, … etc

$ awk 'BEGIN{i=1};/^AK2.*/{p=1};{if(p==1){f="E" i;print($0)>>f;close(f)}};/^AK5\*R.*/{p=0;i++}' file

$ cat E1
AK2*777*6666666
AK5*A
AK2*777*7777777
AK3*S6*5**3
AK3*A2*5**3
AK4*3*6969*4
AK4*7*6969*4
AK5*R*5

$ cat E2
AK2*777*6666666
AK5*A
AK2*777*7777777
AK3*J7*5**3
AK4*3*6969*4
AK5*R*5

Assuming you really want to extract from pattern B until pattern A this awk script will place each range in file E1, E2, … etc

awk 'BEGIN{i=1};/^AK2.*/{p=1};{if(p==1){f="E" i;print($0)>>f;close(f)}};/^AK5\*R.*/{p=0;i++}' file

Assuming you really want to extract from pattern B until pattern A this awk script will place each range in file E1, E2, … etc

$ awk 'BEGIN{i=1};/^AK2.*/{p=1};{if(p==1){f="E" i;print($0)>>f;close(f)}};/^AK5\*R.*/{p=0;i++}' file

$ cat E1
AK2*777*6666666
AK5*A
AK2*777*7777777
AK3*S6*5**3
AK3*A2*5**3
AK4*3*6969*4
AK4*7*6969*4
AK5*R*5

$ cat E2
AK2*777*6666666
AK5*A
AK2*777*7777777
AK3*J7*5**3
AK4*3*6969*4
AK5*R*5
Source Link
user232326
user232326
Loading