Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
07-05-2023, 06:47 PM
#1
Member
Registered: Aug 2014
Distribution: Devuan
Posts: 345
Rep:
Setting 2 files for grep search in 1 unique variable
Hi everyone , i am stuck here with a doubt , i want to set 2 files for grep search but i want to use them as an unique variable of both .
What i need :
Quote:
log1="/var/log/nginx/access.log"
log2="/var/log/nginx/access.log.1"
weblog="$log1" "$log2"
some command in grep next :
grep "something" "$weblog"
I get an error of file missing , but file is there .
I know that i can use them like this and it works :
Quote:
grep "something" "$log1" "$log2"
however if i set those 2 logs in 1 single variable i get an error .
I was thinking in setting just 1 log variable as :
log="/var/log/nginx/access.log*"
but this will include also the gzip files inside nginx logs directory , witch are access.log.2.gz , etc... , and i do not want that .
Any ideas how can i manage to set those 2 logs in one unique variable ?
Thank you
07-05-2023, 07:53 PM
#2
LQ Veteran
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,728
It would be helpful to see the error you got.
Code:
weblog="$log1" "$log2"
returns “no such file…” for log.1 ($log2) because that statement tries to run it after putting $log1 into weblog. I think what you want is
Code:
weblog="$log1 $log2"
1 members found this post helpful.
07-05-2023, 08:33 PM
#3
Moderator
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,267
What scasey says is right - it is trying to execute log2. Put both inside the quotes.
Additionally, you
must not quote $weblog in the grep statement or it will treat the entire string as one filename, "log1 log2". Your grep must be something like this:
Code:
log1="/var/log/nginx/access.log"
log2="/var/log/nginx/access.log.1"
weblog="$log1 $log2"
...
grep "something" $weblog
... which grep will see as two separate filename arguments.
Last edited by astrogeek; 07-06-2023 at 12:48 AM .
Reason: Fix typo, add underline, complete code
07-05-2023, 08:48 PM
#4
Moderator
Registered: Aug 2002
Posts: 25,714
Quote:
grep "something" /var/log/nginx/access.log{,.1}
If you use brace expansion you do not need extra variables and grep will only search access.log and access.log.1
Last edited by michaelk; 07-05-2023 at 08:51 PM .
1 members found this post helpful.
07-05-2023, 09:46 PM
#5
Member
Registered: Aug 2014
Distribution: Devuan
Posts: 345
Original Poster
Rep:
Thank you for the replies on this subject .
using this variable :
weblog="$log1 $log2"
i get :
Quote:
grep: /var/log/nginx/access.log /var/log/nginx/access.log.1: No such file or dir ectory
I think grep is considering the 2 variables inside weblog an unique file instead of 2 , not sure .
Using :
weblog="$log1 $log2"
i get :
grep: : No such file or directory
And using mikaelk idea of :
log1="/var/log/nginx/access.log{,.1}"
does not work either because this script first checks if those files exists before launching the grep statement .
what i mean in mikael statement to not work is this :
Code:
log1="/var/log/nginx/access.log{,.1}"
if [[ ! -f "$log1" ]]
then
echo "Nginx log does not exist"
exit 0
fi
grep "something" "$log1"
Last edited by pedropt; 07-05-2023 at 09:55 PM .
07-05-2023, 11:23 PM
#6
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
In some shells (eg bash) you can use arrays:
Code:
weblog=(file1 "file 2")
grep -- "something" "${weblog[@]}"
07-05-2023, 11:38 PM
#7
Moderator
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,267
Quote:
Originally Posted by
pedropt
using this variable :
weblog="$log1 $log2"
i get :
Code:
grep: /var/log/nginx/access.log /var/log/nginx/access.log.1: No such file or dir ectory
I think grep is considering the 2 variables inside weblog an unique file instead of 2 , not sure .
It would be helpful if you showed us the exact grep command you used. Did you quote $weblog in the grep command?
It should work if unquoted as noted above.
1 members found this post helpful.
07-06-2023, 03:37 AM
#8
Member
Registered: Aug 2014
Distribution: Devuan
Posts: 345
Original Poster
Rep:
Astrogeek
Quote:
It would be helpful if you showed us the exact grep command you used. Did you quote $weblog in the grep command?
yes i used .
the weblog variable in grep command also double quoted again like :
Quote:
log1="/var/log/nginx/access.log"
log2="/var/log/nginx/access.log.1"
weblog="$log1" "$log2"
grep "Something" "$weblog"
However i did not yet test it without double quotes in grep , going to do it now to check output .
07-06-2023, 03:44 AM
#9
Member
Registered: Aug 2014
Distribution: Devuan
Posts: 345
Original Poster
Rep:
Hello again guys , Thank you for the perspective you gave in your last comment astrogeek .
I found the solution .
To work it must be like this :
Quote:
log1="/var/log/nginx/access.log"
log2="/var/log/nginx/access.log.1"
weblog="$log1 $log2"
grep "Something" $weblog
I just have test it now .
07-06-2023, 04:11 AM
#10
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Be careful not to have spaces in your filenames.
07-06-2023, 04:23 AM
#11
Member
Registered: Aug 2014
Distribution: Devuan
Posts: 345
Original Poster
Rep:
Quote:
Be careful not to have spaces in your filenames.
yes , i know , or grep will consider for every space a single file .
07-06-2023, 04:59 AM
#12
Senior Member
Registered: Dec 2011
Location: Simplicity
Posts: 2,798
The standard shell has only your unsafe solution.
bash/ksh/zsh have arrays for a safe solution:
Code:
log1="/var/log/nginx/access.log"
log2="/var/log/nginx/access.log.1"
logs=( "$log1" "$log2" )
grep "Something" "${logs[@]}"
It is important to have @ (not *), then the quotes allow splitting into array members but protect the members against expansions (word splitting and filename generation).
07-06-2023, 05:19 AM
#13
Member
Registered: Aug 2014
Distribution: Devuan
Posts: 345
Original Poster
Rep:
i could do it differently , since i am parsing the log files for anomalies i could create 1 unique temporary file with the content of access.log and access.log.1 and parse that temporary file instead going directly to nginx live logs .
This is what i mean .
Code:
log1="/var/log/nginx/access.log"
log2="/var/log/nginx/access.log.1"
weblog="/tmp/temp.log"
cat "$log1" "$log2" > /tmp/temp.log
grep "something" "$weblog"
However for now i do not think it is necessary for my needs here .
Last edited by pedropt; 07-06-2023 at 05:21 AM .
07-06-2023, 05:25 AM
#14
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Quote:
Originally Posted by
pedropt
... grep will consider for every space a single file .
Not `grep`, the shell.
In your second sample, you could spare the temporal file:
Code:
(set "/var/log/nginx/access.log" "/var/log/nginx/access.log.1"
grep -h "something" "$@")
Last edited by NevemTeve; 07-06-2023 at 05:31 AM .
07-06-2023, 06:51 AM
#15
Member
Registered: Aug 2014
Distribution: Devuan
Posts: 345
Original Poster
Rep:
Quote:
Originally Posted by
NevemTeve
Not `grep`, the shell.
In your second sample, you could spare the temporal file:
Code:
(set "/var/log/nginx/access.log" "/var/log/nginx/access.log.1"
grep -h "something" "$@")
better than that would be setting a variable with all that content , like :
Quote:
weblog=$(cat "$log1" "$log2")
This way i could use it everywhere in script ahead , i only had to change the way grep parses the var like :
Quote:
echo "$weblog" | grep "something"
But i do not need it , we are just speaking about possible alternatives available for the same output .
All times are GMT -5. The time now is 10:10 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News