Reputation: 2173
I'm using this istruction:
VAR=$(grep -r -i --color=always --exclude=\*.{sh,pdf,doc,docx} "$WORD" "$DIRECTORY")
this gives me an output like this:
/dir1NameWithSpaces/File1_05012004/file6.sml:file6 content .... with words and symbols
/dir1NameWithSpaces/File1_05012004/fil3.txt:file3 content .... with words and symbols
/dir1NameWithSpaces/File1_04092008/file.txt:file content .... with words and symbols
I need to extract each line (for example using a loop) and printf only the filename from grep on 1 line, and print the content of the grep command on onother line.
It has to be generic in order to use it everytime I make a search in multi files content.
Besides if you are thinking to use grep directly the line with content does't have to have the filename so I want to leave only the ":" symbol.
To be clear here is an example:
/dir1NameWithSpaces/File1_05012004/file6.sml:
:file6 content .... with words and symbols
/dir1NameWithSpaces/File1_05012004/fil3.txt:
file3 content .... with words and symbols
/dir1NameWithSpaces/File1_04092008/file.txt:
file content .... with words and symbols
In other words the output of my grep has to be tadily printed.
Thanks
Upvotes: 0
Views: 950
Reputation: 1
$ echo foo | tee bar.txt baz.txt qux.txt
foo
$ grep -r foo . | sed 'y/:/\n/'
./bar.txt
foo
./baz.txt
foo
./qux.txt
foo
Upvotes: 3