2

Consider I have a command with some pattern, for example:

$ grep "some string" file-[0-99]

how can I write result into a file that will have its name based on the current value of a pattern, so that

$ grep "some string" file-0 > processed-0
$ grep "some string" file-1 > processed-1
...

Something like $ grep "some string" file-[0-100] > processed-[0-100] though this one doesn't work.

1 Answer 1

7

[0-99] is the same as [0-9], [0-100] means [01]. [...] defines a character class, you can't use longer strings there.

To solve your original question, you can use a loop:

for i in {0..99} ; do
    grep "some string" file-"$i" > processed-"$i"
done

Or, if not all the possible input files exist, you can extract the suffix from the file name:

for file in file-* ; do
    suffix=${file##*-}
    grep "some string" "$file" > processed-"$suffix"
done
1
  • Works great, thanx
    – xaxa
    Jul 3, 2015 at 17:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .