LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-14-2019, 09:48 AM   #1
oliveoyl
Member
 
Registered: Sep 2016
Posts: 42

Rep: Reputation: Disabled
Bracket wildcard usage command line


Hi,

I'm trying to use bracket wildcard on the command line like this:

grep SOMETHING /var/log/node/2019/02/[01-07]/messages


Here is the layout to give you an idea:

## pwd
/var/log/node/2019/01

## ls
01 03 05 07 09 11 13 15 17 19 21 23 25 27 29 31
02 04 06 08 10 12 14 16 18 20 22 24 26 28 30

What is the correct syntax to use bracket wildcard like I want to above?

Thank you.
 
Old 02-14-2019, 09:53 AM   #2
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Code:
grep "SOMETHING" /var/log/node/2019/02/{01..07}/messages
 
1 members found this post helpful.
Old 02-14-2019, 01:15 PM   #3
oliveoyl
Member
 
Registered: Sep 2016
Posts: 42

Original Poster
Rep: Reputation: Disabled
That worked great from the command line!

Now I tried to put it in a script with 01 and 07 as variables but it won't read it:



## DAY1=01; DAY7=07
## echo $DAY1
01
## echo $DAY7
07
## grep "SOMETHING" /var/log/node/2019/02/{$DAY1..$DAY7}/messages
grep: /var/log/node/2019/02/{01..07}/messages: No such file or directory
 
Old 02-14-2019, 01:50 PM   #4
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Code:
echo {01..07}
01 02 03 04 05 06 07
Code:
a=01
b=07
echo {"$a".."$b"}
{01..07}
Code:
c=({01..07})
for i in "${c[@]}"; do echo "$i"; done
01
02
03
04
05
06
07
Code:
c=({01..07})
for i in "${c[@]}"; do
    echo "grep "SOMETHING" /var/log/node/2019/02/"$i"/messages"
done
grep SOMETHING /var/log/node/2019/02/01/messages
grep SOMETHING /var/log/node/2019/02/02/messages
grep SOMETHING /var/log/node/2019/02/03/messages
grep SOMETHING /var/log/node/2019/02/04/messages
grep SOMETHING /var/log/node/2019/02/05/messages
grep SOMETHING /var/log/node/2019/02/06/messages
grep SOMETHING /var/log/node/2019/02/07/messages
Code:
for i in /var/log/node/2019/01/{01..30}/messages; do
    echo "grep 'SOMETHING' "$i""
done
 
Old 02-14-2019, 02:05 PM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by oliveoyl View Post
## DAY1=01; DAY7=07
## echo $DAY1
01
## echo $DAY7
07
## grep "SOMETHING" /var/log/node/2019/02/{$DAY1..$DAY7}/messages
grep: /var/log/node/2019/02/{01..07}/messages: No such file or directory
The problem is with the order in which bash does expansions. Brace expansion is performed before parameter expansion, and those literal characters "$DAY1..$DAY7" are not a legitimate brace expression. There is no simple way to do what you want in bash. You need to do one of:
  1. write a loop to generate the set of strings,
  2. use an external command like seq with its --format option, or
  3. perhaps use zsh, where "{$Var1..$Var2}" does what you want here.
 
1 members found this post helpful.
Old 02-14-2019, 03:47 PM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,792

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Better check for existence in the loop and skip non-existent files
Code:
for f in /var/log/node/2019/01/{01..30}/messages; do
    [ -f "$f" ] || continue
    echo grep 'SOMETHING' "$f"
done
 
Old 02-15-2019, 06:50 AM   #7
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Since OP has requested a solution with variables, I suggest the following:
Code:
#!/bin/bash
startVar=01
endVar=07
for ((i=startVar;i<=endVar;i++))
do
	if ((i<10))
	then 
		[[ -f /var/log/node/2019/02/0$i/messages ]] && grep "SOMETHING" "/var/log/node/2019/02/0$i/messages"
	else 
		[[ -f /var/log/node/2019/02/$i/messages ]] && grep "SOMETHING" "/var/log/node/2019/02/$i/messages"
	fi
done
 
Old 02-15-2019, 01:00 PM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,792

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
But that looks a bit, well, sub-optimal, doesn't it?

Regarding post#1
The "bracket wildcard" is a "character set". Represents one character. So 0[0-7] or 1[0-7] works. But if the range spans over a decade then this method fails.

BTW the character set is really a "wildcard" like * because it is matched against files, and generates what it finds.
In contrast, { } generates a range unconditionally, without matching files.
 
Old 02-16-2019, 04:30 AM   #9
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
^ I agree with everything you said.
Actually, I suggested #7 because that's the only solution that came to my mind to satisfy the initial OP's request, namely to define range with variables inside a script...
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
wildcard usage in find in the input and output assistance needed helpneededquick Programming 3 03-02-2018 06:01 PM
[SOLVED] What am I doing wrong with the square bracket wildcard? new2lnx Linux - Newbie 3 08-08-2016 01:24 PM
command syntax with bracket tanveer Linux - General 4 12-03-2014 11:57 AM
[SOLVED] cp command wildcard asterisk usage at the end of the destination path eNux eLin Linux - Newbie 27 10-20-2012 07:50 AM
Tcl/Tk Bracket Problems in Command Kenji Miyamoto Programming 0 06-26-2005 11:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:12 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