tachomi
tachomi

Reputation: 109

Paste grep search from multiple files to a new one

I have multiple files such as this.

filename.log.50
filename.log.49
filename.log.48
...
...
filename.log.2
filename.log.1

All of them have content as

....connection established
....sending data "Text"
....return ok
....connection closed

How could I put into a new file all the 'sending data "Text" lines' from files ending from 30 to 50? I was doing line by line as this:

cat filename.log.50 |grep 'sending data' >> new_file.log
cat filename.log.49 |grep 'sending data' >> new_file.log
cat filename.log.48 |grep 'sending data' >> new_file.log
....
....

I was thinking something as this...

cat filename.log.5* |grep 'sending data' >> new_file.log
cat filename.log.4* |grep 'sending data' >> new_file.log
cat filename.log.3* |grep 'sending data' >> new_file.log

But that will include the filename.log.5, filename.log.4, filename.log.3

Upvotes: 0

Views: 464

Answers (3)

Vytenis Bivainis
Vytenis Bivainis

Reputation: 2376

Here's the simple way:

seq -f "filename.log.%.f" 50 30 | xargs grep "sending data" > new_file.log

Upvotes: 0

fedorqui
fedorqui

Reputation: 289755

You can for example use this:

grep 'sending data' file.log{[34][0-9],50} >> new_file.log

Which will match everything from 30 up to 50.

If that does not work to you, then split in two blocks:

grep 'sending data' file.log[34][0-9] >> new_file.log
grep 'sending data' file.log50 >> new_file.log

[34][0-9] matches from 30 to 49.

Upvotes: 1

thejh
thejh

Reputation: 45568

I'd use a loop instead:

for i in {30..50}; do
  grep 'sending data' "filename.log.$i"
done > new_file.log

Upvotes: 2

Related Questions