I want to save some simple info of images in a directory(like the image shown below):
But how can I write the matched parts into a file?
I have tried appending >> img_info.txt
but it wrote the whole output instead of being selective.
Try grep's option -o
as described in Can grep show only words that match search pattern?.
Cited from the other answer:
-o, --only-matching
Print only the matched (non-empty) parts of a matching line,
with each such part on a separate output line.
Aside from that grep is probably not the best tool to search and extract strings. This is rather the job of sed
or awk
.