I want to run ack or grep on HTML files that often have very long lines. I don't want to see very long lines that wrap repeatedly. But I do want to see just that portion of a long line that surrounds a string that matches the regular expression. How can I get this using any combination of Unix tools?
相关问题
- Why should we check WIFEXITED after wait in order
- UNIX Bash - Removing double quotes from specific s
- bash delete line condition
- bash delete line condition
- Trying to make a permanent Alias - UNIX
相关文章
- What's the difference between grep -r and -R
- Making new files automatically executable?
- Reverse four length of letters with sed in unix
- Extracting columns from text file using Perl one-l
- Problem with piping commands in C
- Ruby grep with line number
- Makefile and use of $$
- Shell Removing Tabs/Spaces
gets characters from 1 to 100.
Pipe your results thru
cut
. I'm also considering adding a--cut
switch so you could say--cut=80
and only get 80 columns.You could use less as a pager for ack and chop long lines:
ack --pager="less -S"
This retains the long line but leaves it on one line instead of wrapping. To see more of the line, scroll left/right in less with the arrow keys.I have the following alias setup for ack to do this:
You could use the grep option
-o
, possibly in combination with changing your pattern to".{0,10}<original pattern>.{0,10}"
in order to see some context around it:..or
-c
:Taken from: http://www.topbug.net/blog/2016/08/18/truncate-long-matching-lines-of-grep-a-solution-that-preserves-color/
The suggested approach
".{0,10}<original pattern>.{0,10}"
is perfectly good except for that the highlighting color is often messed up. I've created a script with a similar output but the color is also preserved:Assuming the script is saved as
grepl
, thengrepl pattern file_with_long_lines
should display the matching lines but with only 10 characters around the matching string.