LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 07-05-2023, 06:47 PM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Rep: Reputation: Disabled
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
 
Old 07-05-2023, 07:53 PM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,728

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
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.
Old 07-05-2023, 08:33 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,267
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
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
 
Old 07-05-2023, 08:48 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,714

Rep: Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899
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.
Old 07-05-2023, 09:46 PM   #5
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-05-2023, 11:23 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
In some shells (eg bash) you can use arrays:
Code:
weblog=(file1 "file 2")
grep -- "something" "${weblog[@]}"
 
Old 07-05-2023, 11:38 PM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,267
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
Quote:
Originally Posted by pedropt View Post
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.
Old 07-06-2023, 03:37 AM   #8
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
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 .
 
Old 07-06-2023, 03:44 AM   #9
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
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 .
 
Old 07-06-2023, 04:11 AM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Be careful not to have spaces in your filenames.
 
Old 07-06-2023, 04:23 AM   #11
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Quote:
Be careful not to have spaces in your filenames.
yes , i know , or grep will consider for every space a single file .
 
Old 07-06-2023, 04:59 AM   #12
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,798

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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).
 
Old 07-06-2023, 05:19 AM   #13
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-06-2023, 05:25 AM   #14
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Quote:
Originally Posted by pedropt View Post
... 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.
 
Old 07-06-2023, 06:51 AM   #15
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
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 .
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Search Directory full of txt files for a unique string imkornhulio Programming 8 12-13-2013 10:23 AM
[SOLVED] assign full name to variable then grep a file for the variable socalheel Programming 3 09-16-2013 11:04 AM
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
How to use grep to search for a specific variable.. memo007 Linux - Software 4 02-24-2007 05:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration