M
M
MichaelShav2020-12-08 20:13:49
linux
MichaelShav, 2020-12-08 20:13:49

How to change grep output?

Can't figure out grep output. You need to find files that contain a specific string, but make the output in the format Full path to the file, name and size.
at the moment I grep -r "something" /path
conclude: path/to/file:string
I'm trying to make Path: Path/to/file Name: Name Size: SizeKB
In general, I'm looking for an analogue of -printf in find, there it is done by adding -printf "Path: %p Name: %f Size: %kKB\n"
thanks in advance))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Saboteur, 2020-12-08
@MichaelShav

grep doesn't have these options, so you need to process grep's output with something else.
A simple example is here:

grep -r -l "something" | xargs stat --format="Name: %n Size: %B"

Well, you can still separate path and name, of course, but it will be a more complicated command like this:
while read; do
  MYPATH="${REPLY%/*}"
  MYNAME="${REPLY##*/}"
  SIZE="$(stat --format='%s' ${REPLY})"
   && MYPATH=""

  echo "Path: ${MYPATH}/ Name: ${NAME} Size: ${SIZE}"
done<<<$(grep -r -l "alias")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question