Grep is printing “grep:” - How to ignore it [on hold]











up vote
0
down vote

favorite












I am looking to get port number in below filesunder directory Variable. When the file's port line is empty, I am getting grep: printed out. Is there a way to ignore it, and Print nothing?



if ls $directory/*.domain.*.properties 1>/dev/null 2>&1; then
port=$(grep "domain.http.listener.port=" $directory/*.domain.*.properties |
awk -F "=" 'NR==1{print $NF}' | tr -d "r")
fi









share|improve this question















put on hold as unclear what you're asking by Ipor Sircer, RalfFriedl, G-Man, Stephen Harris, Thomas yesterday


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.















  • perhaps you should grep for your condition before assuming it's there?
    – Jeff Schaller
    2 days ago










  • Did you try redirecting standard error to /dev/null, like you are doing in your ls command?
    – Lewis M
    2 days ago










  • Please edit your question and show us i) an example of your input file; ii) the output you want to get and iii) the output you are actually getting. We can't help you parse data you don't show us.
    – terdon
    2 days ago










  • Jeff, Can you please share the example command, to grep the condition ?
    – Bek
    2 days ago






  • 1




    @Bek (please remember to use @terdon if you want to reply to me, I don't get notified otherwise) we really can't help if you don't show us what you are doing. You have chosen a very complex approach and if you simply edit your post and add your input file and your desired output, we can give you a much simpler solution. But without the input, we would have to guess and waste both your time and ours.
    – terdon
    2 days ago















up vote
0
down vote

favorite












I am looking to get port number in below filesunder directory Variable. When the file's port line is empty, I am getting grep: printed out. Is there a way to ignore it, and Print nothing?



if ls $directory/*.domain.*.properties 1>/dev/null 2>&1; then
port=$(grep "domain.http.listener.port=" $directory/*.domain.*.properties |
awk -F "=" 'NR==1{print $NF}' | tr -d "r")
fi









share|improve this question















put on hold as unclear what you're asking by Ipor Sircer, RalfFriedl, G-Man, Stephen Harris, Thomas yesterday


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.















  • perhaps you should grep for your condition before assuming it's there?
    – Jeff Schaller
    2 days ago










  • Did you try redirecting standard error to /dev/null, like you are doing in your ls command?
    – Lewis M
    2 days ago










  • Please edit your question and show us i) an example of your input file; ii) the output you want to get and iii) the output you are actually getting. We can't help you parse data you don't show us.
    – terdon
    2 days ago










  • Jeff, Can you please share the example command, to grep the condition ?
    – Bek
    2 days ago






  • 1




    @Bek (please remember to use @terdon if you want to reply to me, I don't get notified otherwise) we really can't help if you don't show us what you are doing. You have chosen a very complex approach and if you simply edit your post and add your input file and your desired output, we can give you a much simpler solution. But without the input, we would have to guess and waste both your time and ours.
    – terdon
    2 days ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am looking to get port number in below filesunder directory Variable. When the file's port line is empty, I am getting grep: printed out. Is there a way to ignore it, and Print nothing?



if ls $directory/*.domain.*.properties 1>/dev/null 2>&1; then
port=$(grep "domain.http.listener.port=" $directory/*.domain.*.properties |
awk -F "=" 'NR==1{print $NF}' | tr -d "r")
fi









share|improve this question















I am looking to get port number in below filesunder directory Variable. When the file's port line is empty, I am getting grep: printed out. Is there a way to ignore it, and Print nothing?



if ls $directory/*.domain.*.properties 1>/dev/null 2>&1; then
port=$(grep "domain.http.listener.port=" $directory/*.domain.*.properties |
awk -F "=" 'NR==1{print $NF}' | tr -d "r")
fi






shell-script text-processing grep






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









terdon

127k31245422




127k31245422










asked 2 days ago









Bek

65




65




put on hold as unclear what you're asking by Ipor Sircer, RalfFriedl, G-Man, Stephen Harris, Thomas yesterday


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.






put on hold as unclear what you're asking by Ipor Sircer, RalfFriedl, G-Man, Stephen Harris, Thomas yesterday


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.














  • perhaps you should grep for your condition before assuming it's there?
    – Jeff Schaller
    2 days ago










  • Did you try redirecting standard error to /dev/null, like you are doing in your ls command?
    – Lewis M
    2 days ago










  • Please edit your question and show us i) an example of your input file; ii) the output you want to get and iii) the output you are actually getting. We can't help you parse data you don't show us.
    – terdon
    2 days ago










  • Jeff, Can you please share the example command, to grep the condition ?
    – Bek
    2 days ago






  • 1




    @Bek (please remember to use @terdon if you want to reply to me, I don't get notified otherwise) we really can't help if you don't show us what you are doing. You have chosen a very complex approach and if you simply edit your post and add your input file and your desired output, we can give you a much simpler solution. But without the input, we would have to guess and waste both your time and ours.
    – terdon
    2 days ago


















  • perhaps you should grep for your condition before assuming it's there?
    – Jeff Schaller
    2 days ago










  • Did you try redirecting standard error to /dev/null, like you are doing in your ls command?
    – Lewis M
    2 days ago










  • Please edit your question and show us i) an example of your input file; ii) the output you want to get and iii) the output you are actually getting. We can't help you parse data you don't show us.
    – terdon
    2 days ago










  • Jeff, Can you please share the example command, to grep the condition ?
    – Bek
    2 days ago






  • 1




    @Bek (please remember to use @terdon if you want to reply to me, I don't get notified otherwise) we really can't help if you don't show us what you are doing. You have chosen a very complex approach and if you simply edit your post and add your input file and your desired output, we can give you a much simpler solution. But without the input, we would have to guess and waste both your time and ours.
    – terdon
    2 days ago
















perhaps you should grep for your condition before assuming it's there?
– Jeff Schaller
2 days ago




perhaps you should grep for your condition before assuming it's there?
– Jeff Schaller
2 days ago












Did you try redirecting standard error to /dev/null, like you are doing in your ls command?
– Lewis M
2 days ago




Did you try redirecting standard error to /dev/null, like you are doing in your ls command?
– Lewis M
2 days ago












Please edit your question and show us i) an example of your input file; ii) the output you want to get and iii) the output you are actually getting. We can't help you parse data you don't show us.
– terdon
2 days ago




Please edit your question and show us i) an example of your input file; ii) the output you want to get and iii) the output you are actually getting. We can't help you parse data you don't show us.
– terdon
2 days ago












Jeff, Can you please share the example command, to grep the condition ?
– Bek
2 days ago




Jeff, Can you please share the example command, to grep the condition ?
– Bek
2 days ago




1




1




@Bek (please remember to use @terdon if you want to reply to me, I don't get notified otherwise) we really can't help if you don't show us what you are doing. You have chosen a very complex approach and if you simply edit your post and add your input file and your desired output, we can give you a much simpler solution. But without the input, we would have to guess and waste both your time and ours.
– terdon
2 days ago




@Bek (please remember to use @terdon if you want to reply to me, I don't get notified otherwise) we really can't help if you don't show us what you are doing. You have chosen a very complex approach and if you simply edit your post and add your input file and your desired output, we can give you a much simpler solution. But without the input, we would have to guess and waste both your time and ours.
– terdon
2 days ago










1 Answer
1






active

oldest

votes

















up vote
1
down vote













Your code seems to look for the expression domain.http.listener.port= in multiple files with grep, and your awk command would print the last =-delimited field from the first line of that result (only), and then you delete any carriage return in the result of that.



The ls is not needed. You seem to be using it to test whether the filename globbing pattern $directory/*.domain.*.properties matches anything (also possibly splitting the value of $directory on whitespace and performing filename globbing on those bits too).



To test whether a filename globbing pattern matches any filename, you may use



set -- "$directory"/*.domain.*.properties


(I've quoted the expansion of the directory variable because I assume it's supposed to contain the name or pathname of a single directory only) followed by



if [ "$#" -gt 1 ] || [ -e "$1" ]; then ...; fi


The set command sets the positional parameters ($1, $2, $3, etc.), and if we get more than one of these ($# is greater than 1), then the pattern matches more than one thing. Also if the pattern matched a single thing, we test the existence of that thing with -e. That last test will distinguish between a single match and no match (the pattern remains unexpanded).



awk can do the work of grep, and of tr, so there really is no need for the pipeline that you have:



awk -F '=' '/domain.http.listener.port=/ { sub("r","", $NF); print $NF; exit }'


I'm escaping the dots in the regular expression as they would otherwise match any character. I'm using only sub() here, rather than gsub(), as I assume you want to delete the carriage return at the end of the line and nowhere else.



All together:



#!/bin/sh

set -- "$directory"/*.domain.*.properties

if [ "$#" -gt 1 ] || [ -f "$1" ]; then
awk -F '=' '/domain.http.listener.port=/ { sub("r","", $NF); print $NF; exit }' "$@"
fi


I changed the -e test to a -f test as it makes more sense to run awk on a regular file (or a symbolic link to one), than on anything else (but I don't test the file types of any of the files if there are multiple matches).



The "$@" will expand to all the names (individually quoted) that pattern in the set command managed to match.



Would you want the pathname of the file that contained the first match, then you may modify the awk to something that also prints out the special variable FILENAME (e.g. print FILENAME, $NF). Would you want all the matches (not just the first one in the first file), then remove the exit.



Note that this solution would fail if there are many hundreds or thousands of files matching the filename globbing pattern (as would yours). In that case, you would do a shell loop instead:



for pathname in "$directory"/*.domain.*.properties; do
if [ -f "$pathname" ]; then
awk -F '=' '
/domain.http.listener.port=/ {
sub("r","", $NF); print $NF; found=1; exit
}
END { exit !found }' "$pathname" && break
fi
done


Here I've opted to let awk exit with a zero exit status when the wanted text has been printed, which causes the loop to terminate.






share|improve this answer























  • Thank you for the detailed Knowledge. It's working fine, and getting what I need, But for some apps, it's not pulling up the Port number. Is there a way, to ignore the grep: output, so I can just add that, option to my original script, and get this done. Rather then completely going for another way? Thank you again. You are great.
    – Bek
    2 days ago




















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













Your code seems to look for the expression domain.http.listener.port= in multiple files with grep, and your awk command would print the last =-delimited field from the first line of that result (only), and then you delete any carriage return in the result of that.



The ls is not needed. You seem to be using it to test whether the filename globbing pattern $directory/*.domain.*.properties matches anything (also possibly splitting the value of $directory on whitespace and performing filename globbing on those bits too).



To test whether a filename globbing pattern matches any filename, you may use



set -- "$directory"/*.domain.*.properties


(I've quoted the expansion of the directory variable because I assume it's supposed to contain the name or pathname of a single directory only) followed by



if [ "$#" -gt 1 ] || [ -e "$1" ]; then ...; fi


The set command sets the positional parameters ($1, $2, $3, etc.), and if we get more than one of these ($# is greater than 1), then the pattern matches more than one thing. Also if the pattern matched a single thing, we test the existence of that thing with -e. That last test will distinguish between a single match and no match (the pattern remains unexpanded).



awk can do the work of grep, and of tr, so there really is no need for the pipeline that you have:



awk -F '=' '/domain.http.listener.port=/ { sub("r","", $NF); print $NF; exit }'


I'm escaping the dots in the regular expression as they would otherwise match any character. I'm using only sub() here, rather than gsub(), as I assume you want to delete the carriage return at the end of the line and nowhere else.



All together:



#!/bin/sh

set -- "$directory"/*.domain.*.properties

if [ "$#" -gt 1 ] || [ -f "$1" ]; then
awk -F '=' '/domain.http.listener.port=/ { sub("r","", $NF); print $NF; exit }' "$@"
fi


I changed the -e test to a -f test as it makes more sense to run awk on a regular file (or a symbolic link to one), than on anything else (but I don't test the file types of any of the files if there are multiple matches).



The "$@" will expand to all the names (individually quoted) that pattern in the set command managed to match.



Would you want the pathname of the file that contained the first match, then you may modify the awk to something that also prints out the special variable FILENAME (e.g. print FILENAME, $NF). Would you want all the matches (not just the first one in the first file), then remove the exit.



Note that this solution would fail if there are many hundreds or thousands of files matching the filename globbing pattern (as would yours). In that case, you would do a shell loop instead:



for pathname in "$directory"/*.domain.*.properties; do
if [ -f "$pathname" ]; then
awk -F '=' '
/domain.http.listener.port=/ {
sub("r","", $NF); print $NF; found=1; exit
}
END { exit !found }' "$pathname" && break
fi
done


Here I've opted to let awk exit with a zero exit status when the wanted text has been printed, which causes the loop to terminate.






share|improve this answer























  • Thank you for the detailed Knowledge. It's working fine, and getting what I need, But for some apps, it's not pulling up the Port number. Is there a way, to ignore the grep: output, so I can just add that, option to my original script, and get this done. Rather then completely going for another way? Thank you again. You are great.
    – Bek
    2 days ago

















up vote
1
down vote













Your code seems to look for the expression domain.http.listener.port= in multiple files with grep, and your awk command would print the last =-delimited field from the first line of that result (only), and then you delete any carriage return in the result of that.



The ls is not needed. You seem to be using it to test whether the filename globbing pattern $directory/*.domain.*.properties matches anything (also possibly splitting the value of $directory on whitespace and performing filename globbing on those bits too).



To test whether a filename globbing pattern matches any filename, you may use



set -- "$directory"/*.domain.*.properties


(I've quoted the expansion of the directory variable because I assume it's supposed to contain the name or pathname of a single directory only) followed by



if [ "$#" -gt 1 ] || [ -e "$1" ]; then ...; fi


The set command sets the positional parameters ($1, $2, $3, etc.), and if we get more than one of these ($# is greater than 1), then the pattern matches more than one thing. Also if the pattern matched a single thing, we test the existence of that thing with -e. That last test will distinguish between a single match and no match (the pattern remains unexpanded).



awk can do the work of grep, and of tr, so there really is no need for the pipeline that you have:



awk -F '=' '/domain.http.listener.port=/ { sub("r","", $NF); print $NF; exit }'


I'm escaping the dots in the regular expression as they would otherwise match any character. I'm using only sub() here, rather than gsub(), as I assume you want to delete the carriage return at the end of the line and nowhere else.



All together:



#!/bin/sh

set -- "$directory"/*.domain.*.properties

if [ "$#" -gt 1 ] || [ -f "$1" ]; then
awk -F '=' '/domain.http.listener.port=/ { sub("r","", $NF); print $NF; exit }' "$@"
fi


I changed the -e test to a -f test as it makes more sense to run awk on a regular file (or a symbolic link to one), than on anything else (but I don't test the file types of any of the files if there are multiple matches).



The "$@" will expand to all the names (individually quoted) that pattern in the set command managed to match.



Would you want the pathname of the file that contained the first match, then you may modify the awk to something that also prints out the special variable FILENAME (e.g. print FILENAME, $NF). Would you want all the matches (not just the first one in the first file), then remove the exit.



Note that this solution would fail if there are many hundreds or thousands of files matching the filename globbing pattern (as would yours). In that case, you would do a shell loop instead:



for pathname in "$directory"/*.domain.*.properties; do
if [ -f "$pathname" ]; then
awk -F '=' '
/domain.http.listener.port=/ {
sub("r","", $NF); print $NF; found=1; exit
}
END { exit !found }' "$pathname" && break
fi
done


Here I've opted to let awk exit with a zero exit status when the wanted text has been printed, which causes the loop to terminate.






share|improve this answer























  • Thank you for the detailed Knowledge. It's working fine, and getting what I need, But for some apps, it's not pulling up the Port number. Is there a way, to ignore the grep: output, so I can just add that, option to my original script, and get this done. Rather then completely going for another way? Thank you again. You are great.
    – Bek
    2 days ago















up vote
1
down vote










up vote
1
down vote









Your code seems to look for the expression domain.http.listener.port= in multiple files with grep, and your awk command would print the last =-delimited field from the first line of that result (only), and then you delete any carriage return in the result of that.



The ls is not needed. You seem to be using it to test whether the filename globbing pattern $directory/*.domain.*.properties matches anything (also possibly splitting the value of $directory on whitespace and performing filename globbing on those bits too).



To test whether a filename globbing pattern matches any filename, you may use



set -- "$directory"/*.domain.*.properties


(I've quoted the expansion of the directory variable because I assume it's supposed to contain the name or pathname of a single directory only) followed by



if [ "$#" -gt 1 ] || [ -e "$1" ]; then ...; fi


The set command sets the positional parameters ($1, $2, $3, etc.), and if we get more than one of these ($# is greater than 1), then the pattern matches more than one thing. Also if the pattern matched a single thing, we test the existence of that thing with -e. That last test will distinguish between a single match and no match (the pattern remains unexpanded).



awk can do the work of grep, and of tr, so there really is no need for the pipeline that you have:



awk -F '=' '/domain.http.listener.port=/ { sub("r","", $NF); print $NF; exit }'


I'm escaping the dots in the regular expression as they would otherwise match any character. I'm using only sub() here, rather than gsub(), as I assume you want to delete the carriage return at the end of the line and nowhere else.



All together:



#!/bin/sh

set -- "$directory"/*.domain.*.properties

if [ "$#" -gt 1 ] || [ -f "$1" ]; then
awk -F '=' '/domain.http.listener.port=/ { sub("r","", $NF); print $NF; exit }' "$@"
fi


I changed the -e test to a -f test as it makes more sense to run awk on a regular file (or a symbolic link to one), than on anything else (but I don't test the file types of any of the files if there are multiple matches).



The "$@" will expand to all the names (individually quoted) that pattern in the set command managed to match.



Would you want the pathname of the file that contained the first match, then you may modify the awk to something that also prints out the special variable FILENAME (e.g. print FILENAME, $NF). Would you want all the matches (not just the first one in the first file), then remove the exit.



Note that this solution would fail if there are many hundreds or thousands of files matching the filename globbing pattern (as would yours). In that case, you would do a shell loop instead:



for pathname in "$directory"/*.domain.*.properties; do
if [ -f "$pathname" ]; then
awk -F '=' '
/domain.http.listener.port=/ {
sub("r","", $NF); print $NF; found=1; exit
}
END { exit !found }' "$pathname" && break
fi
done


Here I've opted to let awk exit with a zero exit status when the wanted text has been printed, which causes the loop to terminate.






share|improve this answer














Your code seems to look for the expression domain.http.listener.port= in multiple files with grep, and your awk command would print the last =-delimited field from the first line of that result (only), and then you delete any carriage return in the result of that.



The ls is not needed. You seem to be using it to test whether the filename globbing pattern $directory/*.domain.*.properties matches anything (also possibly splitting the value of $directory on whitespace and performing filename globbing on those bits too).



To test whether a filename globbing pattern matches any filename, you may use



set -- "$directory"/*.domain.*.properties


(I've quoted the expansion of the directory variable because I assume it's supposed to contain the name or pathname of a single directory only) followed by



if [ "$#" -gt 1 ] || [ -e "$1" ]; then ...; fi


The set command sets the positional parameters ($1, $2, $3, etc.), and if we get more than one of these ($# is greater than 1), then the pattern matches more than one thing. Also if the pattern matched a single thing, we test the existence of that thing with -e. That last test will distinguish between a single match and no match (the pattern remains unexpanded).



awk can do the work of grep, and of tr, so there really is no need for the pipeline that you have:



awk -F '=' '/domain.http.listener.port=/ { sub("r","", $NF); print $NF; exit }'


I'm escaping the dots in the regular expression as they would otherwise match any character. I'm using only sub() here, rather than gsub(), as I assume you want to delete the carriage return at the end of the line and nowhere else.



All together:



#!/bin/sh

set -- "$directory"/*.domain.*.properties

if [ "$#" -gt 1 ] || [ -f "$1" ]; then
awk -F '=' '/domain.http.listener.port=/ { sub("r","", $NF); print $NF; exit }' "$@"
fi


I changed the -e test to a -f test as it makes more sense to run awk on a regular file (or a symbolic link to one), than on anything else (but I don't test the file types of any of the files if there are multiple matches).



The "$@" will expand to all the names (individually quoted) that pattern in the set command managed to match.



Would you want the pathname of the file that contained the first match, then you may modify the awk to something that also prints out the special variable FILENAME (e.g. print FILENAME, $NF). Would you want all the matches (not just the first one in the first file), then remove the exit.



Note that this solution would fail if there are many hundreds or thousands of files matching the filename globbing pattern (as would yours). In that case, you would do a shell loop instead:



for pathname in "$directory"/*.domain.*.properties; do
if [ -f "$pathname" ]; then
awk -F '=' '
/domain.http.listener.port=/ {
sub("r","", $NF); print $NF; found=1; exit
}
END { exit !found }' "$pathname" && break
fi
done


Here I've opted to let awk exit with a zero exit status when the wanted text has been printed, which causes the loop to terminate.







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









Kusalananda

119k16225367




119k16225367












  • Thank you for the detailed Knowledge. It's working fine, and getting what I need, But for some apps, it's not pulling up the Port number. Is there a way, to ignore the grep: output, so I can just add that, option to my original script, and get this done. Rather then completely going for another way? Thank you again. You are great.
    – Bek
    2 days ago




















  • Thank you for the detailed Knowledge. It's working fine, and getting what I need, But for some apps, it's not pulling up the Port number. Is there a way, to ignore the grep: output, so I can just add that, option to my original script, and get this done. Rather then completely going for another way? Thank you again. You are great.
    – Bek
    2 days ago


















Thank you for the detailed Knowledge. It's working fine, and getting what I need, But for some apps, it's not pulling up the Port number. Is there a way, to ignore the grep: output, so I can just add that, option to my original script, and get this done. Rather then completely going for another way? Thank you again. You are great.
– Bek
2 days ago






Thank you for the detailed Knowledge. It's working fine, and getting what I need, But for some apps, it's not pulling up the Port number. Is there a way, to ignore the grep: output, so I can just add that, option to my original script, and get this done. Rather then completely going for another way? Thank you again. You are great.
– Bek
2 days ago





Popular posts from this blog

Accessing regular linux commands in Huawei's Dopra Linux

Can't connect RFCOMM socket: Host is down

Kernel panic - not syncing: Fatal Exception in Interrupt