Conditional grepping
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a config file and its content looks something like below:
Jobname|Type|Silo|Description
#comment1
#comment2
job1|me|silo1|test_job1
job1|me|silo1|test_job2
job1|prod|silo1|test_job3
Now I need the conditional content of the file, say the content with TYPE =me.
For this I am using a grep with me:
job_detail=$((cat config_file | grep me | awk 'print $4'))
In this case, I am getting the first line also , as JOBNAME is getting matching character me.
I escaped comments with -v options. I can't comment the first line of the config file, as it is used by other unknown processes.
Is there a way i can grep the whole word match? would be better if there is a way to grep whole word with a particular character as condition.
A way to divide the line with '|' and then grep?
shell-script awk scripting grep pipe
add a comment |
up vote
0
down vote
favorite
I have a config file and its content looks something like below:
Jobname|Type|Silo|Description
#comment1
#comment2
job1|me|silo1|test_job1
job1|me|silo1|test_job2
job1|prod|silo1|test_job3
Now I need the conditional content of the file, say the content with TYPE =me.
For this I am using a grep with me:
job_detail=$((cat config_file | grep me | awk 'print $4'))
In this case, I am getting the first line also , as JOBNAME is getting matching character me.
I escaped comments with -v options. I can't comment the first line of the config file, as it is used by other unknown processes.
Is there a way i can grep the whole word match? would be better if there is a way to grep whole word with a particular character as condition.
A way to divide the line with '|' and then grep?
shell-script awk scripting grep pipe
5
Why not justawk -F| '$2=="me" print $4' config_file
?
– steeldriver
Dec 4 '15 at 4:01
@steeldriver... thanx... this works, but suppose i need "me" to be resolved from a variable,say $region, how this should be applied with above?
– NishantM
Dec 7 '15 at 8:32
awk -F| -v region="$region" '$2==region print $4' config_file
– steeldriver
Dec 7 '15 at 12:49
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a config file and its content looks something like below:
Jobname|Type|Silo|Description
#comment1
#comment2
job1|me|silo1|test_job1
job1|me|silo1|test_job2
job1|prod|silo1|test_job3
Now I need the conditional content of the file, say the content with TYPE =me.
For this I am using a grep with me:
job_detail=$((cat config_file | grep me | awk 'print $4'))
In this case, I am getting the first line also , as JOBNAME is getting matching character me.
I escaped comments with -v options. I can't comment the first line of the config file, as it is used by other unknown processes.
Is there a way i can grep the whole word match? would be better if there is a way to grep whole word with a particular character as condition.
A way to divide the line with '|' and then grep?
shell-script awk scripting grep pipe
I have a config file and its content looks something like below:
Jobname|Type|Silo|Description
#comment1
#comment2
job1|me|silo1|test_job1
job1|me|silo1|test_job2
job1|prod|silo1|test_job3
Now I need the conditional content of the file, say the content with TYPE =me.
For this I am using a grep with me:
job_detail=$((cat config_file | grep me | awk 'print $4'))
In this case, I am getting the first line also , as JOBNAME is getting matching character me.
I escaped comments with -v options. I can't comment the first line of the config file, as it is used by other unknown processes.
Is there a way i can grep the whole word match? would be better if there is a way to grep whole word with a particular character as condition.
A way to divide the line with '|' and then grep?
shell-script awk scripting grep pipe
shell-script awk scripting grep pipe
edited Nov 17 at 20:54
Rui F Ribeiro
38.2k1475123
38.2k1475123
asked Dec 4 '15 at 3:54
NishantM
37
37
5
Why not justawk -F| '$2=="me" print $4' config_file
?
– steeldriver
Dec 4 '15 at 4:01
@steeldriver... thanx... this works, but suppose i need "me" to be resolved from a variable,say $region, how this should be applied with above?
– NishantM
Dec 7 '15 at 8:32
awk -F| -v region="$region" '$2==region print $4' config_file
– steeldriver
Dec 7 '15 at 12:49
add a comment |
5
Why not justawk -F| '$2=="me" print $4' config_file
?
– steeldriver
Dec 4 '15 at 4:01
@steeldriver... thanx... this works, but suppose i need "me" to be resolved from a variable,say $region, how this should be applied with above?
– NishantM
Dec 7 '15 at 8:32
awk -F| -v region="$region" '$2==region print $4' config_file
– steeldriver
Dec 7 '15 at 12:49
5
5
Why not just
awk -F| '$2=="me" print $4' config_file
?– steeldriver
Dec 4 '15 at 4:01
Why not just
awk -F| '$2=="me" print $4' config_file
?– steeldriver
Dec 4 '15 at 4:01
@steeldriver... thanx... this works, but suppose i need "me" to be resolved from a variable,say $region, how this should be applied with above?
– NishantM
Dec 7 '15 at 8:32
@steeldriver... thanx... this works, but suppose i need "me" to be resolved from a variable,say $region, how this should be applied with above?
– NishantM
Dec 7 '15 at 8:32
awk -F| -v region="$region" '$2==region print $4' config_file
– steeldriver
Dec 7 '15 at 12:49
awk -F| -v region="$region" '$2==region print $4' config_file
– steeldriver
Dec 7 '15 at 12:49
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
try
awk -F| -v select="$var" '$2 == select print $4;' config_file
where
$var
contains the field you want to select- -F| tell awk to use | as separator, | (pipr) must be escaped.
- -v select="$var" transfer $var to awk variable (select)
$2 == select
select line whose second arg is "$var" or select.print $4;
print fourth field.
wow.... thanxs @archemar.... - v select worked like a charm for me... thanxs again
– NishantM
Dec 7 '15 at 8:35
add a comment |
up vote
0
down vote
man grep
will show you the -w
flag:
-w, --word-regexp Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore.
Or, stick | egrep -v Jobname
early in your pipline.
add a comment |
up vote
0
down vote
A variation of Archemar's solution which assumes that the me
that you want to search for is $LOGNAME
, i.e. the username of the current user:
awk -F '|' '$2 == ENVIRON["LOGNAME"] print $4 ' <config_file
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
try
awk -F| -v select="$var" '$2 == select print $4;' config_file
where
$var
contains the field you want to select- -F| tell awk to use | as separator, | (pipr) must be escaped.
- -v select="$var" transfer $var to awk variable (select)
$2 == select
select line whose second arg is "$var" or select.print $4;
print fourth field.
wow.... thanxs @archemar.... - v select worked like a charm for me... thanxs again
– NishantM
Dec 7 '15 at 8:35
add a comment |
up vote
2
down vote
accepted
try
awk -F| -v select="$var" '$2 == select print $4;' config_file
where
$var
contains the field you want to select- -F| tell awk to use | as separator, | (pipr) must be escaped.
- -v select="$var" transfer $var to awk variable (select)
$2 == select
select line whose second arg is "$var" or select.print $4;
print fourth field.
wow.... thanxs @archemar.... - v select worked like a charm for me... thanxs again
– NishantM
Dec 7 '15 at 8:35
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
try
awk -F| -v select="$var" '$2 == select print $4;' config_file
where
$var
contains the field you want to select- -F| tell awk to use | as separator, | (pipr) must be escaped.
- -v select="$var" transfer $var to awk variable (select)
$2 == select
select line whose second arg is "$var" or select.print $4;
print fourth field.
try
awk -F| -v select="$var" '$2 == select print $4;' config_file
where
$var
contains the field you want to select- -F| tell awk to use | as separator, | (pipr) must be escaped.
- -v select="$var" transfer $var to awk variable (select)
$2 == select
select line whose second arg is "$var" or select.print $4;
print fourth field.
answered Dec 4 '15 at 7:19
Archemar
19.4k93468
19.4k93468
wow.... thanxs @archemar.... - v select worked like a charm for me... thanxs again
– NishantM
Dec 7 '15 at 8:35
add a comment |
wow.... thanxs @archemar.... - v select worked like a charm for me... thanxs again
– NishantM
Dec 7 '15 at 8:35
wow.... thanxs @archemar.... - v select worked like a charm for me... thanxs again
– NishantM
Dec 7 '15 at 8:35
wow.... thanxs @archemar.... - v select worked like a charm for me... thanxs again
– NishantM
Dec 7 '15 at 8:35
add a comment |
up vote
0
down vote
man grep
will show you the -w
flag:
-w, --word-regexp Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore.
Or, stick | egrep -v Jobname
early in your pipline.
add a comment |
up vote
0
down vote
man grep
will show you the -w
flag:
-w, --word-regexp Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore.
Or, stick | egrep -v Jobname
early in your pipline.
add a comment |
up vote
0
down vote
up vote
0
down vote
man grep
will show you the -w
flag:
-w, --word-regexp Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore.
Or, stick | egrep -v Jobname
early in your pipline.
man grep
will show you the -w
flag:
-w, --word-regexp Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore.
Or, stick | egrep -v Jobname
early in your pipline.
answered Dec 4 '15 at 7:03
waltinator
71048
71048
add a comment |
add a comment |
up vote
0
down vote
A variation of Archemar's solution which assumes that the me
that you want to search for is $LOGNAME
, i.e. the username of the current user:
awk -F '|' '$2 == ENVIRON["LOGNAME"] print $4 ' <config_file
add a comment |
up vote
0
down vote
A variation of Archemar's solution which assumes that the me
that you want to search for is $LOGNAME
, i.e. the username of the current user:
awk -F '|' '$2 == ENVIRON["LOGNAME"] print $4 ' <config_file
add a comment |
up vote
0
down vote
up vote
0
down vote
A variation of Archemar's solution which assumes that the me
that you want to search for is $LOGNAME
, i.e. the username of the current user:
awk -F '|' '$2 == ENVIRON["LOGNAME"] print $4 ' <config_file
A variation of Archemar's solution which assumes that the me
that you want to search for is $LOGNAME
, i.e. the username of the current user:
awk -F '|' '$2 == ENVIRON["LOGNAME"] print $4 ' <config_file
answered Nov 17 at 20:58
Kusalananda
116k15218352
116k15218352
add a comment |
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f247279%2fconditional-grepping%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
Why not just
awk -F| '$2=="me" print $4' config_file
?– steeldriver
Dec 4 '15 at 4:01
@steeldriver... thanx... this works, but suppose i need "me" to be resolved from a variable,say $region, how this should be applied with above?
– NishantM
Dec 7 '15 at 8:32
awk -F| -v region="$region" '$2==region print $4' config_file
– steeldriver
Dec 7 '15 at 12:49