Stream Editing and Sed
sed is a very powerful tool.
How to Find the Unique IP from HTTPD log? I got the requirement to find out the unique IP from the httpd log file. the easy way will be using sed, sort, and wc. This will return your the amount unique IP in a log file.
$ sed 's/ - - .*//g' logfile | sort -u | wc
Search string after " - - " and replace with empty for all lines (flag "g").
Print out a block of test that begins with a line containing "START" and ends with a line containing "END":
$ sed -n -e '/START/,/END/p' filename | less
# Print out the table schema from php source files:
$ sed -n -e '/CREATE TABLE/,/;/p' *.php | less
# Print out main() function in a c source file:
$ sed -n -e '/main[[:space:]]*(/,/^}/p *.c | less
Removing HTML tags from a HTML file.
$ sed -e 's/<[^>]*>//g' html.html
Print out all xxx_install() functions from php source files. WordPress is using this convention to create tables on the fly for plugins and event it self. The following sed command will extract all install functions from all php files.
$ sed -n -e '/function .\+install()/,/^}/p' *.php
more examples
Generate CSV format for MySQL query
The default mysql output is tab separate row, the following sed command will convert the tab speparted file to a CSV format.
$ mysql -u username -pPASSWORD < query.sql | \ > sed "s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g"
References
- Handy sed tutorial by examples: http://www.ibm.com/developerworks/linux/library/l-sed1.html
- Good sed Examples: http://www.zytrax.com/tech/web/regex.htm