Showing posts with label grep. Show all posts
Showing posts with label grep. Show all posts

BASH: grep - hide filenames without matching patterns

Hello ,

If you want to get count for pattern matchings in a file then you would use " -c" flag like we all do. It will print matching files with count in the ending and non matching files with count as :0 in the ending. If you are doing this for 100 files your screen will flood with matching and non-matching details.

To get clear picture of only matching, if you can pipe( | ) your result into grep -v ':0$' then you will get results only for matching files.

so full command would be

grep 'your_pattern' file_list* | grep -v ':0$'
Thats it!!

Hope it helps.

Thanks
Raja

grep: unknown device method

Today while using grep command via rundeck I was through grep: unknown device method error.

And the reason is due to I am having ' - ' in my search pattern I was through this error.

I mean search includes

grep "-search.this" /path/to/file


Then you may get error with grep. So remove ' - ' in pattern and repeat your search.

grep "search.this" /path/to/file


Hope that helps.





Extract log efficiently by using sed

Modification of Log pulling command by using sed

Old command That I use to do log extraction

sed -nre '/12:23:12/,/12:24:12/ p' logfile > /tmp/somelog.txt

But this will take match of given two time stamps at any where in log file.And sometimes from middle of the line which gives obviously wrong log that I need.

So , Now we are modifying sed to look after only at lines starting with timestamp.
It will pull the logs if timestamp exists at beginning of the lines else It wont pull any log.

sed -nre '/^12:23:12/,/^12:24:12/ p' logfile > /tmp/somelog.txt

Note: If you want to make sure that given timestamp existed or not at line starting
you can use

grep -m 5 12:23:12 server.log

The above command ran for 5 first matching of grep and so you can conclude the availability of
given time-stamp in logfile.

For more information please refer sed man page

Hope it will help you.