Limit grep output to short lines
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
up vote
6
down vote
favorite
I often use grep to find files having a certain entry like this:
grep -R 'MyClassName'
The good thing is that it returns the files, their contents and marks the found string in red. The bad thing is that I also have huge files where the entire text is written in one big single line. Now grep outputs too much when finding text within those big files. Is there a way to limit the output to for instance 5 words to the left and to the right? Or maybe limit the output to 30 letters to the left and to the right?
command-line text-processing grep
 |Â
show 4 more comments
up vote
6
down vote
favorite
I often use grep to find files having a certain entry like this:
grep -R 'MyClassName'
The good thing is that it returns the files, their contents and marks the found string in red. The bad thing is that I also have huge files where the entire text is written in one big single line. Now grep outputs too much when finding text within those big files. Is there a way to limit the output to for instance 5 words to the left and to the right? Or maybe limit the output to 30 letters to the left and to the right?
command-line text-processing grep
3
Pipe your results thrucut
â Rinzwind
Apr 18 at 8:26
So, let's say the pattern you're looking for is at position 50, but you said you only want 30 letters.What do you want to do then ? Ignore that line or also include it into output but trim it ? What exactly do you want to limit - the search or the lines themselves ?
â Sergiy Kolodyazhnyy
Apr 18 at 8:48
1
@Rinzwind I don't quite understand what you want to achieve withcut
, as it only splits by delimiter or by count of characters. Though when I find a line withMyClassName
it may be anywhere in the line and not always at the same position. Furthermore, there may be a variation of characters in the front and the back of it, which breaks the possibility to split by delimiter.
â Socrates
Apr 18 at 9:16
1
@SergiyKolodyazhnyy When a positive line withMyClassName
has been found, I want to get as a result the file name and the x characters to the left and to the right. x is any number I provide, for instance 30. The rest of the file contents shall be ignored. This is to get a context to the matching files and limit the overload.
â Socrates
Apr 18 at 9:19
1
@Rinzwind What type of custom delimiter would you suggest withcut
if there are three files with the following input:oiadfaosuoianavMyClassNameionaernaldfajd
and/(/&%%§%/(§(/MyClassName&((/$/$/(§/$&
andpublic class MyClassName public static void main(String args)
?
â Socrates
Apr 18 at 10:12
 |Â
show 4 more comments
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I often use grep to find files having a certain entry like this:
grep -R 'MyClassName'
The good thing is that it returns the files, their contents and marks the found string in red. The bad thing is that I also have huge files where the entire text is written in one big single line. Now grep outputs too much when finding text within those big files. Is there a way to limit the output to for instance 5 words to the left and to the right? Or maybe limit the output to 30 letters to the left and to the right?
command-line text-processing grep
I often use grep to find files having a certain entry like this:
grep -R 'MyClassName'
The good thing is that it returns the files, their contents and marks the found string in red. The bad thing is that I also have huge files where the entire text is written in one big single line. Now grep outputs too much when finding text within those big files. Is there a way to limit the output to for instance 5 words to the left and to the right? Or maybe limit the output to 30 letters to the left and to the right?
command-line text-processing grep
command-line text-processing grep
edited Apr 18 at 8:25
muru
130k19273463
130k19273463
asked Apr 18 at 8:19
Socrates
588824
588824
3
Pipe your results thrucut
â Rinzwind
Apr 18 at 8:26
So, let's say the pattern you're looking for is at position 50, but you said you only want 30 letters.What do you want to do then ? Ignore that line or also include it into output but trim it ? What exactly do you want to limit - the search or the lines themselves ?
â Sergiy Kolodyazhnyy
Apr 18 at 8:48
1
@Rinzwind I don't quite understand what you want to achieve withcut
, as it only splits by delimiter or by count of characters. Though when I find a line withMyClassName
it may be anywhere in the line and not always at the same position. Furthermore, there may be a variation of characters in the front and the back of it, which breaks the possibility to split by delimiter.
â Socrates
Apr 18 at 9:16
1
@SergiyKolodyazhnyy When a positive line withMyClassName
has been found, I want to get as a result the file name and the x characters to the left and to the right. x is any number I provide, for instance 30. The rest of the file contents shall be ignored. This is to get a context to the matching files and limit the overload.
â Socrates
Apr 18 at 9:19
1
@Rinzwind What type of custom delimiter would you suggest withcut
if there are three files with the following input:oiadfaosuoianavMyClassNameionaernaldfajd
and/(/&%%§%/(§(/MyClassName&((/$/$/(§/$&
andpublic class MyClassName public static void main(String args)
?
â Socrates
Apr 18 at 10:12
 |Â
show 4 more comments
3
Pipe your results thrucut
â Rinzwind
Apr 18 at 8:26
So, let's say the pattern you're looking for is at position 50, but you said you only want 30 letters.What do you want to do then ? Ignore that line or also include it into output but trim it ? What exactly do you want to limit - the search or the lines themselves ?
â Sergiy Kolodyazhnyy
Apr 18 at 8:48
1
@Rinzwind I don't quite understand what you want to achieve withcut
, as it only splits by delimiter or by count of characters. Though when I find a line withMyClassName
it may be anywhere in the line and not always at the same position. Furthermore, there may be a variation of characters in the front and the back of it, which breaks the possibility to split by delimiter.
â Socrates
Apr 18 at 9:16
1
@SergiyKolodyazhnyy When a positive line withMyClassName
has been found, I want to get as a result the file name and the x characters to the left and to the right. x is any number I provide, for instance 30. The rest of the file contents shall be ignored. This is to get a context to the matching files and limit the overload.
â Socrates
Apr 18 at 9:19
1
@Rinzwind What type of custom delimiter would you suggest withcut
if there are three files with the following input:oiadfaosuoianavMyClassNameionaernaldfajd
and/(/&%%§%/(§(/MyClassName&((/$/$/(§/$&
andpublic class MyClassName public static void main(String args)
?
â Socrates
Apr 18 at 10:12
3
3
Pipe your results thru
cut
â Rinzwind
Apr 18 at 8:26
Pipe your results thru
cut
â Rinzwind
Apr 18 at 8:26
So, let's say the pattern you're looking for is at position 50, but you said you only want 30 letters.What do you want to do then ? Ignore that line or also include it into output but trim it ? What exactly do you want to limit - the search or the lines themselves ?
â Sergiy Kolodyazhnyy
Apr 18 at 8:48
So, let's say the pattern you're looking for is at position 50, but you said you only want 30 letters.What do you want to do then ? Ignore that line or also include it into output but trim it ? What exactly do you want to limit - the search or the lines themselves ?
â Sergiy Kolodyazhnyy
Apr 18 at 8:48
1
1
@Rinzwind I don't quite understand what you want to achieve with
cut
, as it only splits by delimiter or by count of characters. Though when I find a line with MyClassName
it may be anywhere in the line and not always at the same position. Furthermore, there may be a variation of characters in the front and the back of it, which breaks the possibility to split by delimiter.â Socrates
Apr 18 at 9:16
@Rinzwind I don't quite understand what you want to achieve with
cut
, as it only splits by delimiter or by count of characters. Though when I find a line with MyClassName
it may be anywhere in the line and not always at the same position. Furthermore, there may be a variation of characters in the front and the back of it, which breaks the possibility to split by delimiter.â Socrates
Apr 18 at 9:16
1
1
@SergiyKolodyazhnyy When a positive line with
MyClassName
has been found, I want to get as a result the file name and the x characters to the left and to the right. x is any number I provide, for instance 30. The rest of the file contents shall be ignored. This is to get a context to the matching files and limit the overload.â Socrates
Apr 18 at 9:19
@SergiyKolodyazhnyy When a positive line with
MyClassName
has been found, I want to get as a result the file name and the x characters to the left and to the right. x is any number I provide, for instance 30. The rest of the file contents shall be ignored. This is to get a context to the matching files and limit the overload.â Socrates
Apr 18 at 9:19
1
1
@Rinzwind What type of custom delimiter would you suggest with
cut
if there are three files with the following input: oiadfaosuoianavMyClassNameionaernaldfajd
and /(/&%%§%/(§(/MyClassName&((/$/$/(§/$&
and public class MyClassName public static void main(String args)
?â Socrates
Apr 18 at 10:12
@Rinzwind What type of custom delimiter would you suggest with
cut
if there are three files with the following input: oiadfaosuoianavMyClassNameionaernaldfajd
and /(/&%%§%/(§(/MyClassName&((/$/$/(§/$&
and public class MyClassName public static void main(String args)
?â Socrates
Apr 18 at 10:12
 |Â
show 4 more comments
2 Answers
2
active
oldest
votes
up vote
14
down vote
accepted
grep
itself only has options for context based on lines. An alternative is suggested by this SU post:
A workaround is to enable the option 'only-matching' and then to use
RegExp's power to grep a bit more than your text:grep -o ".0,50WHAT_I_M_SEARCHING.0,50" ./filepath
Of course, if you use color highlighting, you can always grep again to
only color the real match:grep -o ".0,50WHAT_I_M_SEARCHING.0,50" ./filepath | grep "WHAT_I_M_SEARCHING"
As another alternative, I'd suggest fold
ing the text and then grepping it, for example:
fold -sw 80 input.txt | grep ...
The -s
option will make fold
push words to the next line instead of breaking in between.
Or use some other way to split the input in lines based on the structure of your input. (The SU post, for example, dealt with JSON, so using jq
etc. to pretty-print and grep
... or just using jq
to do the filtering by itself ... would be better than either of the two alternatives given above.)
This GNU awk method might be faster:
gawk -v n=50 -v RS='MyClassName' '
FNR > 1 printf "%s: %sn",FILENAME, p prt substr($0, 0, n)
p = substr($0, length - n); prt = RT
' input.txt
- Tell awk to split records on the pattern we're interested in (
-v RS=...
), and the number of characters in context (-v n=...
) - Each record after the first record (
FNR > 1
) is one where awk found a match for the pattern. - So we print
n
trailing characters from the previous line (p
) andn
leading characters from the current line (substr($0, 0, n)
), along with the matched text for the previous line (which isprt
)- we set
p
andprt
after printing, so the value we set is used by the next line RT
is a GNUism, that's why this is GNU awk-specific.
- we set
For recursive search, maybe:
find . -type f -exec gawk -v n=50 -v RS='MyClassName' 'FNR>1printf "%s: %sn",FILENAME, p prt substr($0, 0, n) p = substr($0, length-n); prt = RT' +
2
Ok, it works. Seems Regex is a valid approach, so thanks for that. The processing time is quite big though. Without Regex as in my above post it takes 4.912s and with Regex as in your post it takes 3m39.312s.
â Socrates
Apr 18 at 8:55
1
@Socrates see if the awk method I added above performs better
â muru
Apr 18 at 10:43
1
Thefold
method can be used only if you are sure that the searched string does not appear at the border, otherwise it would get hidden bygrep
.
â Melebius
Apr 18 at 10:45
1
@muru Thanks for your suggestion withgawk
. Unfortunately, the suggested command withfind
outputs random stuff and no file names, when executed on my system. Furthermore, I'm not fluent enough inawk
to properly analyse the command. Currently, Regex in combination withgrep
solves the matter maybe not fast, but reliable. Again, thanks a lot.
â Socrates
Apr 18 at 12:30
1
@Socrates I think I managed to fix the awk command. My mental model was wrong about which line'sRT
and prefix, etc. were to be used.
â muru
Apr 18 at 13:33
 |Â
show 2 more comments
up vote
1
down vote
Using only-matching in combination with some other options(see below), might be very close to what you are seeking, without the processing overhead of regex mentioned in the other answer
grep -RnHo 'MyClassName'
n numeric output, show the line number of the match
H filename, show the filename at the start of the line of the match
o only matches, only show the mathed string, not the whole line
While it is true that the result is found much faster, there is missing info. The file path is shown, the line number is shown, but the text output is only my initial searchMyClassName
. Hence, the context is missing.
â Socrates
Apr 18 at 9:36
grep -RnHo "MyClassName"
andgrep -Rno "MyClassName"
have the same output.
â Socrates
Apr 18 at 9:37
@Socrates output is not the same without H in the same directory
â Robert Riedl
Apr 18 at 9:44
The-o
flag might be interesting if the regex had some variable part. For a fixed string, itâÂÂs useless to print it each time. OP is most likely interested in the near context.
â Melebius
Apr 18 at 9:54
1
@Socrates, true - context is missing, but I thought that was the point ? Limit the output ? You can add context again by adding the lines before (-B 1
) or after (-A 1
). Sorry that I could not be of more help.
â Robert Riedl
Apr 18 at 10:52
 |Â
show 2 more comments
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
14
down vote
accepted
grep
itself only has options for context based on lines. An alternative is suggested by this SU post:
A workaround is to enable the option 'only-matching' and then to use
RegExp's power to grep a bit more than your text:grep -o ".0,50WHAT_I_M_SEARCHING.0,50" ./filepath
Of course, if you use color highlighting, you can always grep again to
only color the real match:grep -o ".0,50WHAT_I_M_SEARCHING.0,50" ./filepath | grep "WHAT_I_M_SEARCHING"
As another alternative, I'd suggest fold
ing the text and then grepping it, for example:
fold -sw 80 input.txt | grep ...
The -s
option will make fold
push words to the next line instead of breaking in between.
Or use some other way to split the input in lines based on the structure of your input. (The SU post, for example, dealt with JSON, so using jq
etc. to pretty-print and grep
... or just using jq
to do the filtering by itself ... would be better than either of the two alternatives given above.)
This GNU awk method might be faster:
gawk -v n=50 -v RS='MyClassName' '
FNR > 1 printf "%s: %sn",FILENAME, p prt substr($0, 0, n)
p = substr($0, length - n); prt = RT
' input.txt
- Tell awk to split records on the pattern we're interested in (
-v RS=...
), and the number of characters in context (-v n=...
) - Each record after the first record (
FNR > 1
) is one where awk found a match for the pattern. - So we print
n
trailing characters from the previous line (p
) andn
leading characters from the current line (substr($0, 0, n)
), along with the matched text for the previous line (which isprt
)- we set
p
andprt
after printing, so the value we set is used by the next line RT
is a GNUism, that's why this is GNU awk-specific.
- we set
For recursive search, maybe:
find . -type f -exec gawk -v n=50 -v RS='MyClassName' 'FNR>1printf "%s: %sn",FILENAME, p prt substr($0, 0, n) p = substr($0, length-n); prt = RT' +
2
Ok, it works. Seems Regex is a valid approach, so thanks for that. The processing time is quite big though. Without Regex as in my above post it takes 4.912s and with Regex as in your post it takes 3m39.312s.
â Socrates
Apr 18 at 8:55
1
@Socrates see if the awk method I added above performs better
â muru
Apr 18 at 10:43
1
Thefold
method can be used only if you are sure that the searched string does not appear at the border, otherwise it would get hidden bygrep
.
â Melebius
Apr 18 at 10:45
1
@muru Thanks for your suggestion withgawk
. Unfortunately, the suggested command withfind
outputs random stuff and no file names, when executed on my system. Furthermore, I'm not fluent enough inawk
to properly analyse the command. Currently, Regex in combination withgrep
solves the matter maybe not fast, but reliable. Again, thanks a lot.
â Socrates
Apr 18 at 12:30
1
@Socrates I think I managed to fix the awk command. My mental model was wrong about which line'sRT
and prefix, etc. were to be used.
â muru
Apr 18 at 13:33
 |Â
show 2 more comments
up vote
14
down vote
accepted
grep
itself only has options for context based on lines. An alternative is suggested by this SU post:
A workaround is to enable the option 'only-matching' and then to use
RegExp's power to grep a bit more than your text:grep -o ".0,50WHAT_I_M_SEARCHING.0,50" ./filepath
Of course, if you use color highlighting, you can always grep again to
only color the real match:grep -o ".0,50WHAT_I_M_SEARCHING.0,50" ./filepath | grep "WHAT_I_M_SEARCHING"
As another alternative, I'd suggest fold
ing the text and then grepping it, for example:
fold -sw 80 input.txt | grep ...
The -s
option will make fold
push words to the next line instead of breaking in between.
Or use some other way to split the input in lines based on the structure of your input. (The SU post, for example, dealt with JSON, so using jq
etc. to pretty-print and grep
... or just using jq
to do the filtering by itself ... would be better than either of the two alternatives given above.)
This GNU awk method might be faster:
gawk -v n=50 -v RS='MyClassName' '
FNR > 1 printf "%s: %sn",FILENAME, p prt substr($0, 0, n)
p = substr($0, length - n); prt = RT
' input.txt
- Tell awk to split records on the pattern we're interested in (
-v RS=...
), and the number of characters in context (-v n=...
) - Each record after the first record (
FNR > 1
) is one where awk found a match for the pattern. - So we print
n
trailing characters from the previous line (p
) andn
leading characters from the current line (substr($0, 0, n)
), along with the matched text for the previous line (which isprt
)- we set
p
andprt
after printing, so the value we set is used by the next line RT
is a GNUism, that's why this is GNU awk-specific.
- we set
For recursive search, maybe:
find . -type f -exec gawk -v n=50 -v RS='MyClassName' 'FNR>1printf "%s: %sn",FILENAME, p prt substr($0, 0, n) p = substr($0, length-n); prt = RT' +
2
Ok, it works. Seems Regex is a valid approach, so thanks for that. The processing time is quite big though. Without Regex as in my above post it takes 4.912s and with Regex as in your post it takes 3m39.312s.
â Socrates
Apr 18 at 8:55
1
@Socrates see if the awk method I added above performs better
â muru
Apr 18 at 10:43
1
Thefold
method can be used only if you are sure that the searched string does not appear at the border, otherwise it would get hidden bygrep
.
â Melebius
Apr 18 at 10:45
1
@muru Thanks for your suggestion withgawk
. Unfortunately, the suggested command withfind
outputs random stuff and no file names, when executed on my system. Furthermore, I'm not fluent enough inawk
to properly analyse the command. Currently, Regex in combination withgrep
solves the matter maybe not fast, but reliable. Again, thanks a lot.
â Socrates
Apr 18 at 12:30
1
@Socrates I think I managed to fix the awk command. My mental model was wrong about which line'sRT
and prefix, etc. were to be used.
â muru
Apr 18 at 13:33
 |Â
show 2 more comments
up vote
14
down vote
accepted
up vote
14
down vote
accepted
grep
itself only has options for context based on lines. An alternative is suggested by this SU post:
A workaround is to enable the option 'only-matching' and then to use
RegExp's power to grep a bit more than your text:grep -o ".0,50WHAT_I_M_SEARCHING.0,50" ./filepath
Of course, if you use color highlighting, you can always grep again to
only color the real match:grep -o ".0,50WHAT_I_M_SEARCHING.0,50" ./filepath | grep "WHAT_I_M_SEARCHING"
As another alternative, I'd suggest fold
ing the text and then grepping it, for example:
fold -sw 80 input.txt | grep ...
The -s
option will make fold
push words to the next line instead of breaking in between.
Or use some other way to split the input in lines based on the structure of your input. (The SU post, for example, dealt with JSON, so using jq
etc. to pretty-print and grep
... or just using jq
to do the filtering by itself ... would be better than either of the two alternatives given above.)
This GNU awk method might be faster:
gawk -v n=50 -v RS='MyClassName' '
FNR > 1 printf "%s: %sn",FILENAME, p prt substr($0, 0, n)
p = substr($0, length - n); prt = RT
' input.txt
- Tell awk to split records on the pattern we're interested in (
-v RS=...
), and the number of characters in context (-v n=...
) - Each record after the first record (
FNR > 1
) is one where awk found a match for the pattern. - So we print
n
trailing characters from the previous line (p
) andn
leading characters from the current line (substr($0, 0, n)
), along with the matched text for the previous line (which isprt
)- we set
p
andprt
after printing, so the value we set is used by the next line RT
is a GNUism, that's why this is GNU awk-specific.
- we set
For recursive search, maybe:
find . -type f -exec gawk -v n=50 -v RS='MyClassName' 'FNR>1printf "%s: %sn",FILENAME, p prt substr($0, 0, n) p = substr($0, length-n); prt = RT' +
grep
itself only has options for context based on lines. An alternative is suggested by this SU post:
A workaround is to enable the option 'only-matching' and then to use
RegExp's power to grep a bit more than your text:grep -o ".0,50WHAT_I_M_SEARCHING.0,50" ./filepath
Of course, if you use color highlighting, you can always grep again to
only color the real match:grep -o ".0,50WHAT_I_M_SEARCHING.0,50" ./filepath | grep "WHAT_I_M_SEARCHING"
As another alternative, I'd suggest fold
ing the text and then grepping it, for example:
fold -sw 80 input.txt | grep ...
The -s
option will make fold
push words to the next line instead of breaking in between.
Or use some other way to split the input in lines based on the structure of your input. (The SU post, for example, dealt with JSON, so using jq
etc. to pretty-print and grep
... or just using jq
to do the filtering by itself ... would be better than either of the two alternatives given above.)
This GNU awk method might be faster:
gawk -v n=50 -v RS='MyClassName' '
FNR > 1 printf "%s: %sn",FILENAME, p prt substr($0, 0, n)
p = substr($0, length - n); prt = RT
' input.txt
- Tell awk to split records on the pattern we're interested in (
-v RS=...
), and the number of characters in context (-v n=...
) - Each record after the first record (
FNR > 1
) is one where awk found a match for the pattern. - So we print
n
trailing characters from the previous line (p
) andn
leading characters from the current line (substr($0, 0, n)
), along with the matched text for the previous line (which isprt
)- we set
p
andprt
after printing, so the value we set is used by the next line RT
is a GNUism, that's why this is GNU awk-specific.
- we set
For recursive search, maybe:
find . -type f -exec gawk -v n=50 -v RS='MyClassName' 'FNR>1printf "%s: %sn",FILENAME, p prt substr($0, 0, n) p = substr($0, length-n); prt = RT' +
edited Apr 18 at 13:28
answered Apr 18 at 8:32
muru
130k19273463
130k19273463
2
Ok, it works. Seems Regex is a valid approach, so thanks for that. The processing time is quite big though. Without Regex as in my above post it takes 4.912s and with Regex as in your post it takes 3m39.312s.
â Socrates
Apr 18 at 8:55
1
@Socrates see if the awk method I added above performs better
â muru
Apr 18 at 10:43
1
Thefold
method can be used only if you are sure that the searched string does not appear at the border, otherwise it would get hidden bygrep
.
â Melebius
Apr 18 at 10:45
1
@muru Thanks for your suggestion withgawk
. Unfortunately, the suggested command withfind
outputs random stuff and no file names, when executed on my system. Furthermore, I'm not fluent enough inawk
to properly analyse the command. Currently, Regex in combination withgrep
solves the matter maybe not fast, but reliable. Again, thanks a lot.
â Socrates
Apr 18 at 12:30
1
@Socrates I think I managed to fix the awk command. My mental model was wrong about which line'sRT
and prefix, etc. were to be used.
â muru
Apr 18 at 13:33
 |Â
show 2 more comments
2
Ok, it works. Seems Regex is a valid approach, so thanks for that. The processing time is quite big though. Without Regex as in my above post it takes 4.912s and with Regex as in your post it takes 3m39.312s.
â Socrates
Apr 18 at 8:55
1
@Socrates see if the awk method I added above performs better
â muru
Apr 18 at 10:43
1
Thefold
method can be used only if you are sure that the searched string does not appear at the border, otherwise it would get hidden bygrep
.
â Melebius
Apr 18 at 10:45
1
@muru Thanks for your suggestion withgawk
. Unfortunately, the suggested command withfind
outputs random stuff and no file names, when executed on my system. Furthermore, I'm not fluent enough inawk
to properly analyse the command. Currently, Regex in combination withgrep
solves the matter maybe not fast, but reliable. Again, thanks a lot.
â Socrates
Apr 18 at 12:30
1
@Socrates I think I managed to fix the awk command. My mental model was wrong about which line'sRT
and prefix, etc. were to be used.
â muru
Apr 18 at 13:33
2
2
Ok, it works. Seems Regex is a valid approach, so thanks for that. The processing time is quite big though. Without Regex as in my above post it takes 4.912s and with Regex as in your post it takes 3m39.312s.
â Socrates
Apr 18 at 8:55
Ok, it works. Seems Regex is a valid approach, so thanks for that. The processing time is quite big though. Without Regex as in my above post it takes 4.912s and with Regex as in your post it takes 3m39.312s.
â Socrates
Apr 18 at 8:55
1
1
@Socrates see if the awk method I added above performs better
â muru
Apr 18 at 10:43
@Socrates see if the awk method I added above performs better
â muru
Apr 18 at 10:43
1
1
The
fold
method can be used only if you are sure that the searched string does not appear at the border, otherwise it would get hidden by grep
.â Melebius
Apr 18 at 10:45
The
fold
method can be used only if you are sure that the searched string does not appear at the border, otherwise it would get hidden by grep
.â Melebius
Apr 18 at 10:45
1
1
@muru Thanks for your suggestion with
gawk
. Unfortunately, the suggested command with find
outputs random stuff and no file names, when executed on my system. Furthermore, I'm not fluent enough in awk
to properly analyse the command. Currently, Regex in combination with grep
solves the matter maybe not fast, but reliable. Again, thanks a lot.â Socrates
Apr 18 at 12:30
@muru Thanks for your suggestion with
gawk
. Unfortunately, the suggested command with find
outputs random stuff and no file names, when executed on my system. Furthermore, I'm not fluent enough in awk
to properly analyse the command. Currently, Regex in combination with grep
solves the matter maybe not fast, but reliable. Again, thanks a lot.â Socrates
Apr 18 at 12:30
1
1
@Socrates I think I managed to fix the awk command. My mental model was wrong about which line's
RT
and prefix, etc. were to be used.â muru
Apr 18 at 13:33
@Socrates I think I managed to fix the awk command. My mental model was wrong about which line's
RT
and prefix, etc. were to be used.â muru
Apr 18 at 13:33
 |Â
show 2 more comments
up vote
1
down vote
Using only-matching in combination with some other options(see below), might be very close to what you are seeking, without the processing overhead of regex mentioned in the other answer
grep -RnHo 'MyClassName'
n numeric output, show the line number of the match
H filename, show the filename at the start of the line of the match
o only matches, only show the mathed string, not the whole line
While it is true that the result is found much faster, there is missing info. The file path is shown, the line number is shown, but the text output is only my initial searchMyClassName
. Hence, the context is missing.
â Socrates
Apr 18 at 9:36
grep -RnHo "MyClassName"
andgrep -Rno "MyClassName"
have the same output.
â Socrates
Apr 18 at 9:37
@Socrates output is not the same without H in the same directory
â Robert Riedl
Apr 18 at 9:44
The-o
flag might be interesting if the regex had some variable part. For a fixed string, itâÂÂs useless to print it each time. OP is most likely interested in the near context.
â Melebius
Apr 18 at 9:54
1
@Socrates, true - context is missing, but I thought that was the point ? Limit the output ? You can add context again by adding the lines before (-B 1
) or after (-A 1
). Sorry that I could not be of more help.
â Robert Riedl
Apr 18 at 10:52
 |Â
show 2 more comments
up vote
1
down vote
Using only-matching in combination with some other options(see below), might be very close to what you are seeking, without the processing overhead of regex mentioned in the other answer
grep -RnHo 'MyClassName'
n numeric output, show the line number of the match
H filename, show the filename at the start of the line of the match
o only matches, only show the mathed string, not the whole line
While it is true that the result is found much faster, there is missing info. The file path is shown, the line number is shown, but the text output is only my initial searchMyClassName
. Hence, the context is missing.
â Socrates
Apr 18 at 9:36
grep -RnHo "MyClassName"
andgrep -Rno "MyClassName"
have the same output.
â Socrates
Apr 18 at 9:37
@Socrates output is not the same without H in the same directory
â Robert Riedl
Apr 18 at 9:44
The-o
flag might be interesting if the regex had some variable part. For a fixed string, itâÂÂs useless to print it each time. OP is most likely interested in the near context.
â Melebius
Apr 18 at 9:54
1
@Socrates, true - context is missing, but I thought that was the point ? Limit the output ? You can add context again by adding the lines before (-B 1
) or after (-A 1
). Sorry that I could not be of more help.
â Robert Riedl
Apr 18 at 10:52
 |Â
show 2 more comments
up vote
1
down vote
up vote
1
down vote
Using only-matching in combination with some other options(see below), might be very close to what you are seeking, without the processing overhead of regex mentioned in the other answer
grep -RnHo 'MyClassName'
n numeric output, show the line number of the match
H filename, show the filename at the start of the line of the match
o only matches, only show the mathed string, not the whole line
Using only-matching in combination with some other options(see below), might be very close to what you are seeking, without the processing overhead of regex mentioned in the other answer
grep -RnHo 'MyClassName'
n numeric output, show the line number of the match
H filename, show the filename at the start of the line of the match
o only matches, only show the mathed string, not the whole line
answered Apr 18 at 9:29
Robert Riedl
2,730623
2,730623
While it is true that the result is found much faster, there is missing info. The file path is shown, the line number is shown, but the text output is only my initial searchMyClassName
. Hence, the context is missing.
â Socrates
Apr 18 at 9:36
grep -RnHo "MyClassName"
andgrep -Rno "MyClassName"
have the same output.
â Socrates
Apr 18 at 9:37
@Socrates output is not the same without H in the same directory
â Robert Riedl
Apr 18 at 9:44
The-o
flag might be interesting if the regex had some variable part. For a fixed string, itâÂÂs useless to print it each time. OP is most likely interested in the near context.
â Melebius
Apr 18 at 9:54
1
@Socrates, true - context is missing, but I thought that was the point ? Limit the output ? You can add context again by adding the lines before (-B 1
) or after (-A 1
). Sorry that I could not be of more help.
â Robert Riedl
Apr 18 at 10:52
 |Â
show 2 more comments
While it is true that the result is found much faster, there is missing info. The file path is shown, the line number is shown, but the text output is only my initial searchMyClassName
. Hence, the context is missing.
â Socrates
Apr 18 at 9:36
grep -RnHo "MyClassName"
andgrep -Rno "MyClassName"
have the same output.
â Socrates
Apr 18 at 9:37
@Socrates output is not the same without H in the same directory
â Robert Riedl
Apr 18 at 9:44
The-o
flag might be interesting if the regex had some variable part. For a fixed string, itâÂÂs useless to print it each time. OP is most likely interested in the near context.
â Melebius
Apr 18 at 9:54
1
@Socrates, true - context is missing, but I thought that was the point ? Limit the output ? You can add context again by adding the lines before (-B 1
) or after (-A 1
). Sorry that I could not be of more help.
â Robert Riedl
Apr 18 at 10:52
While it is true that the result is found much faster, there is missing info. The file path is shown, the line number is shown, but the text output is only my initial search
MyClassName
. Hence, the context is missing.â Socrates
Apr 18 at 9:36
While it is true that the result is found much faster, there is missing info. The file path is shown, the line number is shown, but the text output is only my initial search
MyClassName
. Hence, the context is missing.â Socrates
Apr 18 at 9:36
grep -RnHo "MyClassName"
and grep -Rno "MyClassName"
have the same output.â Socrates
Apr 18 at 9:37
grep -RnHo "MyClassName"
and grep -Rno "MyClassName"
have the same output.â Socrates
Apr 18 at 9:37
@Socrates output is not the same without H in the same directory
â Robert Riedl
Apr 18 at 9:44
@Socrates output is not the same without H in the same directory
â Robert Riedl
Apr 18 at 9:44
The
-o
flag might be interesting if the regex had some variable part. For a fixed string, itâÂÂs useless to print it each time. OP is most likely interested in the near context.â Melebius
Apr 18 at 9:54
The
-o
flag might be interesting if the regex had some variable part. For a fixed string, itâÂÂs useless to print it each time. OP is most likely interested in the near context.â Melebius
Apr 18 at 9:54
1
1
@Socrates, true - context is missing, but I thought that was the point ? Limit the output ? You can add context again by adding the lines before (
-B 1
) or after (-A 1
). Sorry that I could not be of more help.â Robert Riedl
Apr 18 at 10:52
@Socrates, true - context is missing, but I thought that was the point ? Limit the output ? You can add context again by adding the lines before (
-B 1
) or after (-A 1
). Sorry that I could not be of more help.â Robert Riedl
Apr 18 at 10:52
 |Â
show 2 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1026045%2flimit-grep-output-to-short-lines%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
3
Pipe your results thru
cut
â Rinzwind
Apr 18 at 8:26
So, let's say the pattern you're looking for is at position 50, but you said you only want 30 letters.What do you want to do then ? Ignore that line or also include it into output but trim it ? What exactly do you want to limit - the search or the lines themselves ?
â Sergiy Kolodyazhnyy
Apr 18 at 8:48
1
@Rinzwind I don't quite understand what you want to achieve with
cut
, as it only splits by delimiter or by count of characters. Though when I find a line withMyClassName
it may be anywhere in the line and not always at the same position. Furthermore, there may be a variation of characters in the front and the back of it, which breaks the possibility to split by delimiter.â Socrates
Apr 18 at 9:16
1
@SergiyKolodyazhnyy When a positive line with
MyClassName
has been found, I want to get as a result the file name and the x characters to the left and to the right. x is any number I provide, for instance 30. The rest of the file contents shall be ignored. This is to get a context to the matching files and limit the overload.â Socrates
Apr 18 at 9:19
1
@Rinzwind What type of custom delimiter would you suggest with
cut
if there are three files with the following input:oiadfaosuoianavMyClassNameionaernaldfajd
and/(/&%%§%/(§(/MyClassName&((/$/$/(§/$&
andpublic class MyClassName public static void main(String args)
?â Socrates
Apr 18 at 10:12