awk: print text between two patterns + x lines followed by first match
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
Following Input File:
#Report Nr. 2343215
#Errors 3243
#Date: (Timestampt)
#Informaiton
#
# Headers
# Specs
DLSLWD 0 0 0 0 Jun 22 01:51:16PM 2018
#List of Objects
#
# Headers
# Paths
Files not found /var/xxxxx
Files not found /etc/xxxxx
Files not found /mnt/xxxxx
Files not found /safd/xxxxx
#
#Reports
#
Error-Number 123
Error Number 12345
#
What i need is an awk that pipes the "List of Objects" into a new file:
#List of Objects
#
# Headers
# Paths
Files not found /var/xxxxx
Files not found /etc/xxxxx
Files not found /mnt/xxxxx
Files not found /safd/xxxxx
#
And the " Reports"into a differnt file:
#Reports
#
Error-Number 123
Error Number 12345
#
It's a match for #List of Objects + 3 lines until "first" #.
Same for the Reports: Match #Reports + 1 line until "first" #.
At first i tried something like:
awk '/#List of Objects/,/#Reports/'
For the list of objects followed by:
awk '/#Reports/,0'
To get the data from #Reports until EOF.
But because #Reports
and #List
of Objects are both OPTIONAL and not in every input file I can't use #Reports as the "END-Pattern". so, I have to match on the # but ignore the first X occurrences of # after the matching pattern.
awk
add a comment |Â
up vote
3
down vote
favorite
Following Input File:
#Report Nr. 2343215
#Errors 3243
#Date: (Timestampt)
#Informaiton
#
# Headers
# Specs
DLSLWD 0 0 0 0 Jun 22 01:51:16PM 2018
#List of Objects
#
# Headers
# Paths
Files not found /var/xxxxx
Files not found /etc/xxxxx
Files not found /mnt/xxxxx
Files not found /safd/xxxxx
#
#Reports
#
Error-Number 123
Error Number 12345
#
What i need is an awk that pipes the "List of Objects" into a new file:
#List of Objects
#
# Headers
# Paths
Files not found /var/xxxxx
Files not found /etc/xxxxx
Files not found /mnt/xxxxx
Files not found /safd/xxxxx
#
And the " Reports"into a differnt file:
#Reports
#
Error-Number 123
Error Number 12345
#
It's a match for #List of Objects + 3 lines until "first" #.
Same for the Reports: Match #Reports + 1 line until "first" #.
At first i tried something like:
awk '/#List of Objects/,/#Reports/'
For the list of objects followed by:
awk '/#Reports/,0'
To get the data from #Reports until EOF.
But because #Reports
and #List
of Objects are both OPTIONAL and not in every input file I can't use #Reports as the "END-Pattern". so, I have to match on the # but ignore the first X occurrences of # after the matching pattern.
awk
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
Following Input File:
#Report Nr. 2343215
#Errors 3243
#Date: (Timestampt)
#Informaiton
#
# Headers
# Specs
DLSLWD 0 0 0 0 Jun 22 01:51:16PM 2018
#List of Objects
#
# Headers
# Paths
Files not found /var/xxxxx
Files not found /etc/xxxxx
Files not found /mnt/xxxxx
Files not found /safd/xxxxx
#
#Reports
#
Error-Number 123
Error Number 12345
#
What i need is an awk that pipes the "List of Objects" into a new file:
#List of Objects
#
# Headers
# Paths
Files not found /var/xxxxx
Files not found /etc/xxxxx
Files not found /mnt/xxxxx
Files not found /safd/xxxxx
#
And the " Reports"into a differnt file:
#Reports
#
Error-Number 123
Error Number 12345
#
It's a match for #List of Objects + 3 lines until "first" #.
Same for the Reports: Match #Reports + 1 line until "first" #.
At first i tried something like:
awk '/#List of Objects/,/#Reports/'
For the list of objects followed by:
awk '/#Reports/,0'
To get the data from #Reports until EOF.
But because #Reports
and #List
of Objects are both OPTIONAL and not in every input file I can't use #Reports as the "END-Pattern". so, I have to match on the # but ignore the first X occurrences of # after the matching pattern.
awk
Following Input File:
#Report Nr. 2343215
#Errors 3243
#Date: (Timestampt)
#Informaiton
#
# Headers
# Specs
DLSLWD 0 0 0 0 Jun 22 01:51:16PM 2018
#List of Objects
#
# Headers
# Paths
Files not found /var/xxxxx
Files not found /etc/xxxxx
Files not found /mnt/xxxxx
Files not found /safd/xxxxx
#
#Reports
#
Error-Number 123
Error Number 12345
#
What i need is an awk that pipes the "List of Objects" into a new file:
#List of Objects
#
# Headers
# Paths
Files not found /var/xxxxx
Files not found /etc/xxxxx
Files not found /mnt/xxxxx
Files not found /safd/xxxxx
#
And the " Reports"into a differnt file:
#Reports
#
Error-Number 123
Error Number 12345
#
It's a match for #List of Objects + 3 lines until "first" #.
Same for the Reports: Match #Reports + 1 line until "first" #.
At first i tried something like:
awk '/#List of Objects/,/#Reports/'
For the list of objects followed by:
awk '/#Reports/,0'
To get the data from #Reports until EOF.
But because #Reports
and #List
of Objects are both OPTIONAL and not in every input file I can't use #Reports as the "END-Pattern". so, I have to match on the # but ignore the first X occurrences of # after the matching pattern.
awk
edited Jun 27 at 15:13
SivaPrasath
3,88611737
3,88611737
asked Jun 27 at 14:15
T-One
336
336
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
Awk script:
extract_pat_space.awk
contents:
$0 ~ "^#" pat f = 1; hash = 0
f print
NF == 1 && $1 == "#"
if (++hash == 2) f = hash = 0
Usage (for both patterns):
$ awk -f extract_pat_space.awk -v pat="List" file > list_of_objects.txt
$ awk -f extract_pat_space.awk -v pat="Reports" file > reports.txt
Results:
$ cat list_of_objects.txt
#List of Objects
#
# Headers
# Paths
Files not found /var/xxxxx
Files not found /etc/xxxxx
Files not found /mnt/xxxxx
Files not found /safd/xxxxx
#
$ cat reports.txt
#Reports
#
Error-Number 123
Error Number 12345
#
i have no idea what you are doing with this awk, i'm trying to get my head around it, thank you! is it also possible to get rid of all the lines with a # in the output? I would just do a grep -ve '#" on it but would be nice to have this in the awk scrip too.
â T-One
Jun 27 at 15:10
@T-One, extend your question according to the mentioned moment
â RomanPerekhrest
Jun 27 at 15:13
add a comment |Â
up vote
1
down vote
awk '
/#List of Objects/ f = "objects.txt"
/#Reports/ f = "reports.txt"
f print > f
' file
When you see one of the key headers, set the output filename.
If the variable has been set, print to that file.
This looks extremly simple, how does awk know where to stop?
â T-One
Jun 28 at 7:27
Stop? At the end of file. Are there other criteria I missed?
â glenn jackman
Jun 28 at 10:16
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Awk script:
extract_pat_space.awk
contents:
$0 ~ "^#" pat f = 1; hash = 0
f print
NF == 1 && $1 == "#"
if (++hash == 2) f = hash = 0
Usage (for both patterns):
$ awk -f extract_pat_space.awk -v pat="List" file > list_of_objects.txt
$ awk -f extract_pat_space.awk -v pat="Reports" file > reports.txt
Results:
$ cat list_of_objects.txt
#List of Objects
#
# Headers
# Paths
Files not found /var/xxxxx
Files not found /etc/xxxxx
Files not found /mnt/xxxxx
Files not found /safd/xxxxx
#
$ cat reports.txt
#Reports
#
Error-Number 123
Error Number 12345
#
i have no idea what you are doing with this awk, i'm trying to get my head around it, thank you! is it also possible to get rid of all the lines with a # in the output? I would just do a grep -ve '#" on it but would be nice to have this in the awk scrip too.
â T-One
Jun 27 at 15:10
@T-One, extend your question according to the mentioned moment
â RomanPerekhrest
Jun 27 at 15:13
add a comment |Â
up vote
1
down vote
accepted
Awk script:
extract_pat_space.awk
contents:
$0 ~ "^#" pat f = 1; hash = 0
f print
NF == 1 && $1 == "#"
if (++hash == 2) f = hash = 0
Usage (for both patterns):
$ awk -f extract_pat_space.awk -v pat="List" file > list_of_objects.txt
$ awk -f extract_pat_space.awk -v pat="Reports" file > reports.txt
Results:
$ cat list_of_objects.txt
#List of Objects
#
# Headers
# Paths
Files not found /var/xxxxx
Files not found /etc/xxxxx
Files not found /mnt/xxxxx
Files not found /safd/xxxxx
#
$ cat reports.txt
#Reports
#
Error-Number 123
Error Number 12345
#
i have no idea what you are doing with this awk, i'm trying to get my head around it, thank you! is it also possible to get rid of all the lines with a # in the output? I would just do a grep -ve '#" on it but would be nice to have this in the awk scrip too.
â T-One
Jun 27 at 15:10
@T-One, extend your question according to the mentioned moment
â RomanPerekhrest
Jun 27 at 15:13
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Awk script:
extract_pat_space.awk
contents:
$0 ~ "^#" pat f = 1; hash = 0
f print
NF == 1 && $1 == "#"
if (++hash == 2) f = hash = 0
Usage (for both patterns):
$ awk -f extract_pat_space.awk -v pat="List" file > list_of_objects.txt
$ awk -f extract_pat_space.awk -v pat="Reports" file > reports.txt
Results:
$ cat list_of_objects.txt
#List of Objects
#
# Headers
# Paths
Files not found /var/xxxxx
Files not found /etc/xxxxx
Files not found /mnt/xxxxx
Files not found /safd/xxxxx
#
$ cat reports.txt
#Reports
#
Error-Number 123
Error Number 12345
#
Awk script:
extract_pat_space.awk
contents:
$0 ~ "^#" pat f = 1; hash = 0
f print
NF == 1 && $1 == "#"
if (++hash == 2) f = hash = 0
Usage (for both patterns):
$ awk -f extract_pat_space.awk -v pat="List" file > list_of_objects.txt
$ awk -f extract_pat_space.awk -v pat="Reports" file > reports.txt
Results:
$ cat list_of_objects.txt
#List of Objects
#
# Headers
# Paths
Files not found /var/xxxxx
Files not found /etc/xxxxx
Files not found /mnt/xxxxx
Files not found /safd/xxxxx
#
$ cat reports.txt
#Reports
#
Error-Number 123
Error Number 12345
#
answered Jun 27 at 14:37
RomanPerekhrest
22.4k12144
22.4k12144
i have no idea what you are doing with this awk, i'm trying to get my head around it, thank you! is it also possible to get rid of all the lines with a # in the output? I would just do a grep -ve '#" on it but would be nice to have this in the awk scrip too.
â T-One
Jun 27 at 15:10
@T-One, extend your question according to the mentioned moment
â RomanPerekhrest
Jun 27 at 15:13
add a comment |Â
i have no idea what you are doing with this awk, i'm trying to get my head around it, thank you! is it also possible to get rid of all the lines with a # in the output? I would just do a grep -ve '#" on it but would be nice to have this in the awk scrip too.
â T-One
Jun 27 at 15:10
@T-One, extend your question according to the mentioned moment
â RomanPerekhrest
Jun 27 at 15:13
i have no idea what you are doing with this awk, i'm trying to get my head around it, thank you! is it also possible to get rid of all the lines with a # in the output? I would just do a grep -ve '#" on it but would be nice to have this in the awk scrip too.
â T-One
Jun 27 at 15:10
i have no idea what you are doing with this awk, i'm trying to get my head around it, thank you! is it also possible to get rid of all the lines with a # in the output? I would just do a grep -ve '#" on it but would be nice to have this in the awk scrip too.
â T-One
Jun 27 at 15:10
@T-One, extend your question according to the mentioned moment
â RomanPerekhrest
Jun 27 at 15:13
@T-One, extend your question according to the mentioned moment
â RomanPerekhrest
Jun 27 at 15:13
add a comment |Â
up vote
1
down vote
awk '
/#List of Objects/ f = "objects.txt"
/#Reports/ f = "reports.txt"
f print > f
' file
When you see one of the key headers, set the output filename.
If the variable has been set, print to that file.
This looks extremly simple, how does awk know where to stop?
â T-One
Jun 28 at 7:27
Stop? At the end of file. Are there other criteria I missed?
â glenn jackman
Jun 28 at 10:16
add a comment |Â
up vote
1
down vote
awk '
/#List of Objects/ f = "objects.txt"
/#Reports/ f = "reports.txt"
f print > f
' file
When you see one of the key headers, set the output filename.
If the variable has been set, print to that file.
This looks extremly simple, how does awk know where to stop?
â T-One
Jun 28 at 7:27
Stop? At the end of file. Are there other criteria I missed?
â glenn jackman
Jun 28 at 10:16
add a comment |Â
up vote
1
down vote
up vote
1
down vote
awk '
/#List of Objects/ f = "objects.txt"
/#Reports/ f = "reports.txt"
f print > f
' file
When you see one of the key headers, set the output filename.
If the variable has been set, print to that file.
awk '
/#List of Objects/ f = "objects.txt"
/#Reports/ f = "reports.txt"
f print > f
' file
When you see one of the key headers, set the output filename.
If the variable has been set, print to that file.
answered Jun 27 at 16:47
glenn jackman
45.6k264100
45.6k264100
This looks extremly simple, how does awk know where to stop?
â T-One
Jun 28 at 7:27
Stop? At the end of file. Are there other criteria I missed?
â glenn jackman
Jun 28 at 10:16
add a comment |Â
This looks extremly simple, how does awk know where to stop?
â T-One
Jun 28 at 7:27
Stop? At the end of file. Are there other criteria I missed?
â glenn jackman
Jun 28 at 10:16
This looks extremly simple, how does awk know where to stop?
â T-One
Jun 28 at 7:27
This looks extremly simple, how does awk know where to stop?
â T-One
Jun 28 at 7:27
Stop? At the end of file. Are there other criteria I missed?
â glenn jackman
Jun 28 at 10:16
Stop? At the end of file. Are there other criteria I missed?
â glenn jackman
Jun 28 at 10:16
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f452234%2fawk-print-text-between-two-patterns-x-lines-followed-by-first-match%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password