1 Stella2 Stelle3 Stelle4 Stelle5 Stelle (Nessun voto ancora)
Loading ... Loading ...

“grep” è un bellissimo strumento

Utilizzando linux, come non fare a usare “grep” un tool per la ricerca testuale con il supporto delle espressioni regolari. Sotto troverete un po’ di tips per la ricerca.

(fonte: eriwen.com)

Tutorial

Suppose we want to search for duplicate functions in all of our JavaScript files. Let’s start basic and work up to it. This technique can be used to search for a TON of duplicate items like:

  • Duplicate HTML IDs
  • Check how many times a CSS class is used
  • Duplicate java classes
  • many, many more…

</p>

<h1> Search JS files in this directory for "function"</h1>

<p>grep function *.js

</p>

<h1>Search JS files in this directory for "function"</h1>

<p>grep function *.js

The above command will print the lines containing “function” in all JavaScript files in the current directory (NOT subdirectories). Printing out line contents would be much more helpful if we knew what files they come from and their line numbers:

  1. # Print filenames, line #s, and lines that start with ”(white space)function”
  2. grep -EHn “^\s*(function \w+|\w+ \= function)” *.js

Depending on how you format your JavaScript files, something like this will omit comments, anonymous functions, and also words like “functionality” giving you better results.

  1. # Print a list of: function <function-name> and sort it
  2. grep -Eho “^\s*function \w+” *.js | sort

-o prints only the part that matches the regular expression. -E options gives me extended regex and -h suppresses printing of the file name. I am then piping to sort which just sorts the output so it a list of function <function-name>. If you don’t have a lot of files/functions to go through, you can just scan the list and then note the duplicate function names you see. Let’s go a step further for those that DO have a big list:

  1. # Print only duplicate function names
  2. grep -hEo “^\s*function \w+” *.js | sort | uniq -d

There we go! That will list only the duplcated functions. I know that we can expand this with awk or other stuff and get the file names and line numbers of the duplicates, but I don’t want to explaining the details of awk ;) . I actually had it in this article and then removed it so leave a comment or contact me if you want the code for that.

Other Examples

  1. # Count the number of functions in all JS files
  2. grep -c function *.js
  3. # Print lines that DO NOT have ”function”
  4. grep -v function *.js
  5. # List processes that match ”pidgin” (non-Windows)
  6. ps -ef | grep pidgin

Conclusion

grep is one of the most used command-line tools, often piped to for filtering output. Understanding it is essential to increasing productivity on the command-line. There is so much more to grep than what I’ve shown here, and it would be cool to see your best uses in the comments!

Ti piace questo articolo? Salvalo o condividilo!!
  • Twitter
  • Facebook
  • Digg
  • Sphinn
  • del.icio.us
  • Mixx
  • Google Bookmarks
  • Live
  • Segnalo
  • StumbleUpon
  • Technorati
  • laaik.it
  • BarraPunto
  • email
  • Netvouz
  • BlinkList
  • Print
  • blogmarks
  • HelloTxt
  • FriendFeed
  • PDF

Post relativi

1 Comment

grep toolaprile 4th, 2010 at 05:23

[...] efficient compared to grep. Refers to simple examples of awk here. [...] Leave a Reply. Name …Squid Brain Backup SB2 Blog Archive grep ¨ un …luglio 14, 2008 in Editor di testo, Filosofia, My Brain, Programmi, Scriptare, White side of Brain | [...]

Leave a comment

Your comment