Playing with GREP: grepping out lines that start with empty space lines that start with empty space, grep, empty space, regular expressions
Working with config files or with lots of output lines could be quite dificult sometimes, specially if you need to filter out lines based on regular expressions.
How to filter lines that start with empty(blank) space
Let's say we have a file containing IP addresses and some lines start with the IP address and other lines start with " IP" like the one below:
How to filter (grep out) lines that start directly with 77 or 193 or 64 is easy. But let's say want to grep out the IP addresses that start with 193 and have an empty space before them: " 193.24.215.135" (that starts with empty space):
$ cat test | grep '^[[:space:]][[:space:]]*193' 193.24.215.135
What I did above was to grep out the lines that start with at least one empty space, followed by possibly more space and by 193.
Let's say we need to grep out all the IPs that start with 193 (with or without empty space at the beginning of the line):
$ cat test | grep '^[[:space:]]*193' 193.24.215.135 193.254.203.58
Above I used grep to filter the lines that possibly start with empty space followed by 193.
How to output only lines that start with empty space
Of course, you need to play a little more with grep and regular expressions (a very interesting field, of course) in order to customize the output, depending on your situation. Man grep and google:regular expressions or yahoo: regular expressions will be very usefull in this case.
Hello, Guest ! You can Login or Register to www.ivorde.ro!
Post comment:
1 comment(s) to Playing with GREP: grepping out lines that start with empty space:
1. Re: Playing with GREP: grepping out lines that start with empty space
SysAdmin by Lucas
at October 24th, 2008 - 13:32
That's a Useless Use of Cat. Don't waste time concatenating the file and piping it to grep when you can just do: "grep foo filename" and get the same thing.
That said, thanks for the tip on '[[:space:]]', didn't know I could do that and it was exactly what I needed.
Designed and developed by Andrei Manescu. Optimized for Mozilla Firefox.
Copyright 2007 Andrei Manescu
All trademarks and copyrights on this page are owned by their respective owners. Comments are owned by those who posted them.