1

I have to print the content of specify files from list of file as below. Date in the begining files name is changeable and the part "_lol_lol.txt" is constans.

ls
2015-01-03_lol_lol.txt
2015-01-24_lol_lol.txt
...
2015-10-23_lol_lol.txt
2015-10-24_lol_lol.txt
...
2015-11-14_lol_lol.txt 
2015-11-15_lol_lol.txt 
2015-11-16_lol_lol.txt 
2015-11-17_lol_lol.txt 
2015-11-18_lol_lol.txt 
2015-11-19_lol_lol.txt
2015-11-20_lol_lol.txt 
2015-11-21_lol_lol.txt 
2015-11-22_lol_lol.txt 
2015-11-23_lol_lol.txt 
2015-11-24_lol_lol.txt 
2015-11-25_lol_lol.txt

Every file contains some data, for the purpose of case:

cat 2015-01-03_lol_lol.txt
2015-01-03
cat 2015-01-24_lol_lol.txt
2015-01-24
...
cat 2015-10-23_lol_lol.txt
2015-10-23
cat 2015-10-24_lol_lol.txt
2015-10-24
...
cat 2015-11-14_lol_lol.txt
2015-11-14
cat 2015-11-15_lol_lol.txt
2015-11-15
...
cat 2015-11-25_lol_lol.txt
2015-11-25

And I want to print content of fles from fange "from" "to" and both "from" and "to" is variable (defined by user, depending on the needs):

2015-11-15_lol_lol.txt
2015-11-16_lol_lol.txt 
2015-11-17_lol_lol.txt

Expectet result:

2015-11-15
2015-11-16
2015-11-17

But when I use command:

for i in {"2015-11-15".."2015-11-17"};do echo  $i_lol_lol.txt ;done

Bash returns:

{2015-11-15..2015-11-17}.txt

Because like @karoshi if I use "-" whole expression is recognize like string.

I tried:

y1=2015;
y2=2015;
m1=11;
m2=11;
d=15
z=17

#for i in {${y1}..${y2}}; do
   for j in {${m1}..${m2}}; do
           for k in {${d1}..${d2}}; do
                   cat ${i}-${j}-${k}_lol_lol.txt
           done
   done
done`

But result is simmilar.

Could you help?

3
  • What sort of ranges do you need to support here? What about if a given file in the range doesn't exist? Commented Nov 25, 2015 at 14:18
  • If you want to print content files, you probably want to say cat, not echo. Commented Nov 25, 2015 at 14:27
  • @fedorqui you're right. i want to use cat not echo. @Etan Reisner ranges is whole year. Commented Nov 26, 2015 at 7:06

2 Answers 2

2

If you want to loop through given months and days, there is way to use the {..} syntax. Why? Because this expansion occurs even before variables are being checked, so that no variables can be stored there.

Instead, use seq, which does admit variables.

$ a=12
$ b=23
$ for i in $(seq $a $b); do echo "$i"; done
12
13
...
21
22
23

In this case, you may want to say:

  • Get $min_month and $max_month
  • Get $min_day and $max_day

And then perform a double loop:

for month in $(seq "$min_month" "$max_month");
do
    for day in $(seq "$min_day""$max_day");
    do
         cat 2015-${month}-${day}_lol_lol.txt
    done
done
Sign up to request clarification or add additional context in comments.

7 Comments

Don't even need to do that much, for i in 2015-11-1{5,6,7}_lol_lol.txt;do echo $i;done
This data range is only example. Because in this case i have to cat dates from 11-15 to 11-17 but for two days i will have to cat dates from 11-17 to 11-19.
@DamianSilkowski then update your question with a more detailed example please!
@fedorqui done. I hope my description is enough for you now.
@DamianSilkowski it would be better to update explaining the logic of it ("I want to print those whose name contains X and Y") instead of pasting a full list of them. Could you edit and show that?
|
1

Well, you could just try

for i in {15..17};do echo  2015-11-${i}_lol_lol.txt ;done

The completion doesn't work with the '-' included, because bash doesn't recognize it as an integer.

1 Comment

So I have to change date format from 2015-11-15 to 2015.11.15. But what happens if the month changed, i.e. I will have to cat files from 2015-11-29 to 2015-12-03?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.