Print value of 2nd pattern if 1st pattern matches username [closed]
Clash Royale CLAN TAG#URR8PPP
up vote
-1
down vote
favorite
I have been trying to write a script with awk
to print the name of a users rolegroup(s), if the username matches an input variable.
The input file is similar to the following
sssss
xxxname
trevor
xxxage
21
xxxrolegroups
activedir
sssss
xxxname
gus
xxxage30
xxxrolegroups
sssss
As you can see, the file is separated by sssss
. I know that the name I am looking for is trevor
and I want the script to print the value activedir
.
The fields aren't always in the same order (there are multiple fields, this just being an example).
I tried the following...
USERNAME=`cat trevor.txt`
awk 'BEGIN RS="sssss"; FS="n"; if username=$USERNAME then awk '/xxrolegroups/ getline; print '
Please excuse the pseudo-code, just trying to convey what I'm trying to do :-)
Thanks
linux awk wildcards
closed as unclear what you're asking by ñÃÂsýù÷, Jesse_b, Kiwy, vonbrand, G-Man May 4 at 21:11
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |Â
up vote
-1
down vote
favorite
I have been trying to write a script with awk
to print the name of a users rolegroup(s), if the username matches an input variable.
The input file is similar to the following
sssss
xxxname
trevor
xxxage
21
xxxrolegroups
activedir
sssss
xxxname
gus
xxxage30
xxxrolegroups
sssss
As you can see, the file is separated by sssss
. I know that the name I am looking for is trevor
and I want the script to print the value activedir
.
The fields aren't always in the same order (there are multiple fields, this just being an example).
I tried the following...
USERNAME=`cat trevor.txt`
awk 'BEGIN RS="sssss"; FS="n"; if username=$USERNAME then awk '/xxrolegroups/ getline; print '
Please excuse the pseudo-code, just trying to convey what I'm trying to do :-)
Thanks
linux awk wildcards
closed as unclear what you're asking by ñÃÂsýù÷, Jesse_b, Kiwy, vonbrand, G-Man May 4 at 21:11
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1
Hello and welcome to U&L. Please add an example of the desired output to your question.
â roaima
May 3 at 15:15
Also, please differentiate whether your input file is thetrevor.txt
from your example, and if not, whattrevor.txt
contains. (my assumption is it simply containstrevor
, if I understand your question)
â cunninghamp3
May 3 at 15:39
1
Should the script printactivedir
when it's foundtrevor
, or only if it finds this keyword intrevor
's block? Should it print anything if it cannot findactivedir
in the block containingtrevor
?
â roaima
May 3 at 16:17
I'd use a more capable language for such a task, like Python or Perl.
â vonbrand
May 4 at 13:55
add a comment |Â
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have been trying to write a script with awk
to print the name of a users rolegroup(s), if the username matches an input variable.
The input file is similar to the following
sssss
xxxname
trevor
xxxage
21
xxxrolegroups
activedir
sssss
xxxname
gus
xxxage30
xxxrolegroups
sssss
As you can see, the file is separated by sssss
. I know that the name I am looking for is trevor
and I want the script to print the value activedir
.
The fields aren't always in the same order (there are multiple fields, this just being an example).
I tried the following...
USERNAME=`cat trevor.txt`
awk 'BEGIN RS="sssss"; FS="n"; if username=$USERNAME then awk '/xxrolegroups/ getline; print '
Please excuse the pseudo-code, just trying to convey what I'm trying to do :-)
Thanks
linux awk wildcards
I have been trying to write a script with awk
to print the name of a users rolegroup(s), if the username matches an input variable.
The input file is similar to the following
sssss
xxxname
trevor
xxxage
21
xxxrolegroups
activedir
sssss
xxxname
gus
xxxage30
xxxrolegroups
sssss
As you can see, the file is separated by sssss
. I know that the name I am looking for is trevor
and I want the script to print the value activedir
.
The fields aren't always in the same order (there are multiple fields, this just being an example).
I tried the following...
USERNAME=`cat trevor.txt`
awk 'BEGIN RS="sssss"; FS="n"; if username=$USERNAME then awk '/xxrolegroups/ getline; print '
Please excuse the pseudo-code, just trying to convey what I'm trying to do :-)
Thanks
linux awk wildcards
edited May 3 at 16:16
roaima
39.4k545106
39.4k545106
asked May 3 at 15:05
nbeaton
1
1
closed as unclear what you're asking by ñÃÂsýù÷, Jesse_b, Kiwy, vonbrand, G-Man May 4 at 21:11
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by ñÃÂsýù÷, Jesse_b, Kiwy, vonbrand, G-Man May 4 at 21:11
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1
Hello and welcome to U&L. Please add an example of the desired output to your question.
â roaima
May 3 at 15:15
Also, please differentiate whether your input file is thetrevor.txt
from your example, and if not, whattrevor.txt
contains. (my assumption is it simply containstrevor
, if I understand your question)
â cunninghamp3
May 3 at 15:39
1
Should the script printactivedir
when it's foundtrevor
, or only if it finds this keyword intrevor
's block? Should it print anything if it cannot findactivedir
in the block containingtrevor
?
â roaima
May 3 at 16:17
I'd use a more capable language for such a task, like Python or Perl.
â vonbrand
May 4 at 13:55
add a comment |Â
1
Hello and welcome to U&L. Please add an example of the desired output to your question.
â roaima
May 3 at 15:15
Also, please differentiate whether your input file is thetrevor.txt
from your example, and if not, whattrevor.txt
contains. (my assumption is it simply containstrevor
, if I understand your question)
â cunninghamp3
May 3 at 15:39
1
Should the script printactivedir
when it's foundtrevor
, or only if it finds this keyword intrevor
's block? Should it print anything if it cannot findactivedir
in the block containingtrevor
?
â roaima
May 3 at 16:17
I'd use a more capable language for such a task, like Python or Perl.
â vonbrand
May 4 at 13:55
1
1
Hello and welcome to U&L. Please add an example of the desired output to your question.
â roaima
May 3 at 15:15
Hello and welcome to U&L. Please add an example of the desired output to your question.
â roaima
May 3 at 15:15
Also, please differentiate whether your input file is the
trevor.txt
from your example, and if not, what trevor.txt
contains. (my assumption is it simply contains trevor
, if I understand your question)â cunninghamp3
May 3 at 15:39
Also, please differentiate whether your input file is the
trevor.txt
from your example, and if not, what trevor.txt
contains. (my assumption is it simply contains trevor
, if I understand your question)â cunninghamp3
May 3 at 15:39
1
1
Should the script print
activedir
when it's found trevor
, or only if it finds this keyword in trevor
's block? Should it print anything if it cannot find activedir
in the block containing trevor
?â roaima
May 3 at 16:17
Should the script print
activedir
when it's found trevor
, or only if it finds this keyword in trevor
's block? Should it print anything if it cannot find activedir
in the block containing trevor
?â roaima
May 3 at 16:17
I'd use a more capable language for such a task, like Python or Perl.
â vonbrand
May 4 at 13:55
I'd use a more capable language for such a task, like Python or Perl.
â vonbrand
May 4 at 13:55
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
You can use a script like this. I've split it up to help with readability, but you can literally concatenate all the lines if you prefer:
awk -v key=trevor '
BEGIN RS="^sssss$"; RE="n" key "n"
$0 ~ RE && /activedir/ print "activedir"
' datafile.txt
It sets the record separator to a line consisting of sssss
and creates a Regular Expression that matches the key trevor
to a full line within that record. Then, if it can match the RE and it finds activedir
it prints the required text.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You can use a script like this. I've split it up to help with readability, but you can literally concatenate all the lines if you prefer:
awk -v key=trevor '
BEGIN RS="^sssss$"; RE="n" key "n"
$0 ~ RE && /activedir/ print "activedir"
' datafile.txt
It sets the record separator to a line consisting of sssss
and creates a Regular Expression that matches the key trevor
to a full line within that record. Then, if it can match the RE and it finds activedir
it prints the required text.
add a comment |Â
up vote
1
down vote
You can use a script like this. I've split it up to help with readability, but you can literally concatenate all the lines if you prefer:
awk -v key=trevor '
BEGIN RS="^sssss$"; RE="n" key "n"
$0 ~ RE && /activedir/ print "activedir"
' datafile.txt
It sets the record separator to a line consisting of sssss
and creates a Regular Expression that matches the key trevor
to a full line within that record. Then, if it can match the RE and it finds activedir
it prints the required text.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You can use a script like this. I've split it up to help with readability, but you can literally concatenate all the lines if you prefer:
awk -v key=trevor '
BEGIN RS="^sssss$"; RE="n" key "n"
$0 ~ RE && /activedir/ print "activedir"
' datafile.txt
It sets the record separator to a line consisting of sssss
and creates a Regular Expression that matches the key trevor
to a full line within that record. Then, if it can match the RE and it finds activedir
it prints the required text.
You can use a script like this. I've split it up to help with readability, but you can literally concatenate all the lines if you prefer:
awk -v key=trevor '
BEGIN RS="^sssss$"; RE="n" key "n"
$0 ~ RE && /activedir/ print "activedir"
' datafile.txt
It sets the record separator to a line consisting of sssss
and creates a Regular Expression that matches the key trevor
to a full line within that record. Then, if it can match the RE and it finds activedir
it prints the required text.
answered May 3 at 16:27
roaima
39.4k545106
39.4k545106
add a comment |Â
add a comment |Â
1
Hello and welcome to U&L. Please add an example of the desired output to your question.
â roaima
May 3 at 15:15
Also, please differentiate whether your input file is the
trevor.txt
from your example, and if not, whattrevor.txt
contains. (my assumption is it simply containstrevor
, if I understand your question)â cunninghamp3
May 3 at 15:39
1
Should the script print
activedir
when it's foundtrevor
, or only if it finds this keyword intrevor
's block? Should it print anything if it cannot findactivedir
in the block containingtrevor
?â roaima
May 3 at 16:17
I'd use a more capable language for such a task, like Python or Perl.
â vonbrand
May 4 at 13:55