Friday, January 29, 2016

egrep-xargs-sed combo effect


Debugging is very significant part of verification engineer's life. In the fast paced SoC world, parsing through the logfile and quickly understanding the problem is the key. Also sometimes we may need to collect the data (say performance numbers) by parsing the list of logfiles. Almost the laborious tasks has become the part of verification.

egrep, xargs, sed are very powerful commands which can do wonders for that matter.

Now here the scenario is to find the list of files which don't have the below line and insert that at second line of those files.
// vim: tabstop=3 expandtab shiftwidth=3 softtabstop=3

2 steps for this.
1. We have to find the list of the files which don't have that line(pattern) using egrep.  -L option gives the file names only instead of lines. In general it is better to use egrep than grep.
egrep "vim: tabstop" -L *

2. We need to feed that file list to sed command which will insert that line. Here "1 a" will append that patter after the first line.

egrep "vim: tabstop" -L * | xargs sed -i '1 a // vim: tabstop=3 expandtab shiftwidth=3 softtabstop=3'

xargs is used to pass the file list from egrep to sed.  Just | alone is not enough.

Or we can try like this:-
sed -i '1 a // vim: tabstop=3 expandtab shiftwidth=3 softtabstop=3' `egrep "vim: tabstop" * -L`


Above is just an example.
There are lot of things that we could from the command line instead of writing a script.



No comments:

Post a Comment