Some SED Commands

Valic —  November 30, 2010 — Leave a comment

Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed’s ability to filter text in a pipeline which particularly distinguishes it from other types of editors.

Here are some SED commands used by everyone:

1) Print all the lines between 10 and 20 of a file

sed -n ’10,20p’ <filename>

Similarly, if you want to print from 10 to the end of line you can use: sed -n ’10,$p’ filename

This is especially useful if you are dealing with a large file. Sometimes you just want to extract a sample without opening the entire file.

2) Check your unread Gmail from the command line

curl -u username –silent “” | perl -ne ‘print “\t” if /<name>/; print “$2\n” if /<(title|name)>(.*)<\/\1>/;’

Checks the Gmail ATOM feed for your account, parses it and outputs a list of unread messages.

3) To print a specific line from a file

sed -n 5p <file>

You can get one specific line during any procedure. Very interesting to be used when you know what line you want.

4) Remove a line in a text file. Useful to fix “ssh host key change” warnings

sed -i 8d ~/.ssh/known_hosts

5) Recursive search and replace old with new string, inside files

grep -rl oldstring . |xargs sed -i -e ‘s/oldstring/newstring/’

recursively traverse the directory structure from . down, look for string “oldstring” in all files, and replace it with “newstring”, wherever found


grep -rl oldstring . |xargs perl -pi~ -e 's/oldstring/newstring'

Just put this in your ..htaccess file.

// .htaccess HTTP to HTTPS Rewrite Rule
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

In this example, each database will be stored separately and like earlier example we will save all databases and we will have the last 4 copies.

#Simple mySQL backup script for cron – updated version

# Modify the following to suit your environment
export DB_BACKUP=”/home/user/mysql_backup”
export DB_USER=”root”
export DB_PASSWD=”***********”
export DATE=”`date +”%d%b”`”
export MYSQL=”/usr/bin/mysql”
export MYSQLDUMP=”/usr/bin/mysqldump”

# Backup part
echo “mySQL_backup”
echo “———————-”
echo “* Rotating backups…”
rm -rf $DB_BACKUP/04
mkdir $DB_BACKUP/01

cd $DB_BACKUP/ && cd $DB_BACKUP/01
$MYSQL -u $DB_USER –password=$DB_PASSWD -Bse ‘show databases’ |while read m; \
do $MYSQLDUMP -u $DB_USER –password=$DB_PASSWD `echo $m` > `echo $m`.sql;done
bzip2 *sql

echo “* Creating new backup…”
echo “Backup done! `date`” > /tmp/my_report.log

# You can set the script to send you mail when backup it’s finished.
mail -s “MySql Backup report” [email protected] < /tmp/my_report.log
echo "----------------------"
echo "Done"
exit 0


Multitail Logs on Linux

Valic —  November 22, 2010 — Leave a comment

Have you ever having to control more than a single file with your tail -f ?

I will and I just recently found out that you can run the tail command specifying the -f flag several times so you can give the command:

tail -f /var/log/messages -f /var/log/syslog

You’ll get an output like this:

==> messages <== Nov 22 00:43:07 laptop kernel: [ 61.487969] ADDRCONF(NETDEV_UP): eth0: link is not ready

==> syslog <== Nov 22 00:43:31 laptop pulseaudio[1773]: ratelimit.c: 1 events suppressed

==> messages <== Nov 22 00:43:31 laptop pulseaudio[1773]: ratelimit.c: 1 events suppressed

==> syslog <==

Nov 22 00:45:44 laptop ntpd[1118]: kernel time sync status change 6001

The Debian Release Team has made a status update on Debian 6.0, “Squeeze”. They are proud to report that Debian is moving towards the release like a glacier: “inevitably and unstoppingly”.

Squeeze is almost in its final state: testing of the installer and upgrades are welcome. Once the latest version of the Debian Installer is added, a deep freeze will begin, with minimal changes allowed while the remaining bugs are fixed. Your help is welcome in:

There are two upcoming bug squashing parties you can join: Bern, Switzerland, November 27-28; and Boulogne-Billancourt, France,December 4-5.

More :

Page 1 of 3123