Cvermule

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Friday, 25 January 2008

Like grep, but not

Posted on 07:49 by Unknown
This code is like using grep. However, grep only returns the lines that contain the actual text you are searching for. This code allows you to return x number of lines before and after the text you are searching for along with the line itself. This command came in handy searching for information in the uds log files.

nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=2 a=4 s="fetchHotel" file

b is the number of lines to print before the line encountered and a is the number of lines to print after the line is encountered.

s=”string” string you are looking for (in this case fetchHotel)
and file is the file you are looking in.

By Kris Chappell
Read More
Posted in grep, search, tip, unix | No comments
Newer Posts Older Posts Home
Subscribe to: Posts (Atom)

Popular Posts

Categories

  • amazon
  • browser specific
  • coding
  • Eclipse
  • error
  • example
  • funny
  • google
  • grep
  • hacks
  • javascript
  • jott
  • music
  • null
  • phone
  • search
  • search engine
  • tip
  • tool
  • unix
  • web development
  • yui

Blog Archive

  • ▼  2008 (5)
    • ►  September (1)
    • ►  August (1)
    • ►  July (1)
    • ►  June (1)
    • ▼  January (1)
      • Like grep, but not
  • ►  2007 (6)
    • ►  December (1)
    • ►  September (1)
    • ►  July (1)
    • ►  June (1)
    • ►  March (2)
  • ►  2006 (10)
    • ►  November (1)
    • ►  August (1)
    • ►  May (2)
    • ►  April (4)
    • ►  March (2)
  • ►  2005 (10)
    • ►  August (3)
    • ►  June (2)
    • ►  February (5)
  • ►  2004 (9)
    • ►  October (2)
    • ►  September (7)
Powered by Blogger.

About Me

Unknown
View my complete profile