Usually all mail log files specially SendMail, Qmail and Postfix, generates multi-lines of mail log files into its default log file. That is, a message line is being dumped to a log file from the time of a client's connection up to client's disconnection, there are already several lines dumped to its default mail log file, say /var/log/maillog.
Now, say we want to print a few lines before and after a matching string is found, here's a quick way of accomplishing this task using grep.
Grep can display leading and trailing lines before a string match is found.
Here's a quick way to do it.
# cat testfile.txt
~~~~~~~~~~~~~~~~~~~~
123
456
678
Linux SysAd Blog
abc
def
ghi
~~~~~~~~~~~~~~~~~~~~
Now, let us try to print leading lines before a match to 'linux' string is found using grep.
# grep -B 2 Linux testfile.txt
output:
~~~~~~~~~~~~~~~~~~~~
456
678
Linux SysAd Blog
~~~~~~~~~~~~~~~~~~~~
The above comamnd displays 2 leading lines before the matched string.
Alternatively, let us print 2 trailing lines after a match is found. This can be done like so
# grep -A 2 Linux testfile.txt
Output:
~~~~~~~~~~~~~~~~~~~~
Linux SysAd Blog
abc
def
~~~~~~~~~~~~~~~~~~~~
As you can see, a specified number of trailing lines before the search string 'Linux' .
MORE GREP APPLICATION
~~~~~~~~~~~~~~~~~~~~~~
You can also apply this to commented text file. In a large commented config files, usually putting a marker string is helpful on editing large files. The above commands can be handy if you wish to view those trailing and leading lines before that marker string.
# cat testfile.cfg
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# ..
# Two thousand config and commented lines before the next line
#
# here goes my marker string
# CHECK1
#
# here goes the actual config line
value1 = 1
value2 = 2
#
# another thousand config lines.
#
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Without using an editor, grep can easily display lines of value1 and value2 using CHECK1 as my string marker.
Final Note
~~~~~~~~~~
Grep saves time and effort on handling these type of grep searching.
HTH
Thanks for today's support!
Subscription
Categories
- HowTos (612)
- Linux Devices (40)
- Linux Diggs (620)
- Linux News (1541)
- Linux Videos (24)
Recent Posts
Blog Archive
-
▼
2007
(340)
-
▼
September
(58)
- Dear Linux SysAd Blog Readers
- Proactive monitoring from linux terminal
- deleting new lines and return line from text file
- Google chat setup using PSI howto
- PSI messenger - a truly promising open messaging a...
- Google chat setup using GAIM Pidgin howto
- Linux command line shell variables defined
- ls - displaying directory contents in many ways
- Control of alternative linux executables
- Searching using whereis linux command
- Witchy which linux command
- NeroLinux - diehard Nero burning software
- Google Sky - Explore and Rediscover the Sky
- Celestia - 3D Earth and Sky visualization
- Earth3D - real-time 3D Earth visualization
- GcStar - managing personal collection items
- DStat - resource statistics linux tool
- Bandwidth Monitor-NG - terminal-based interface ba...
- KNemo - KDE network interface monitoring tool
- EtherApe - graphical network activity monitoring tool
- Beauty of Math using Linux
- print leading/trailing lines before/after a matchi...
- Nagios Monitoring - install and generic setup howto
- ChRT- change real-time attribute process scheduling
- squeezed out multiple commented lines
- UNIX to DOS text file format converter
- totally squeezed out multiple blank lines
- squeezed multiple blank lines into single line
- fmt - simple optimal text formatter
- Linux backup powered by RDiff-Backup
- Linux Ping command explained
- read and display text file from terminal
- URLView - URL and email extractor
- TFTP server - setup and install howto
- NMap - Linux port scanning
- removing garbage characters from screen terminal
- invert string match using grep
- RDesktop - remote desktop howto
- Graveman on Linux - burn baby burn burn
- HTOP - interactive process viewer alternative
- Caching DNS server install howto
- BitTorrent - downloading large files made easy
- PHPAlbum - web photo album install howto
- MRTG graph creation with Cisco routers
- Tree view of directories and file listings from co...
- MAC address packet filtering using IPTables
- GTK-based GNOME Linux Tools
- Linux backups powered by RSnapShot
- MRTG tutorial, install and howtos
- FindSMB - view shared folders from network
- Squid - upgrade and install howto
- prompt and press a key between script lines
- Devede - DVD/VCD video authoring and creation tool
- display file and file system status
- determine file type
- GNOME GUI task scheduler install howto
- Linux backups powered by Rsync
- KPackage - GUI package administration and manageme...
-
▼
September
(58)
Friday, September 14, 2007
print leading/trailing lines before/after a matching string
Subscribe to:
Post Comments (Atom)
ILoveTux - howtos and news | About | Contact | TOS | Policy
0 comments:
Post a Comment