sh -x try.sh 2>&1 | sed "s/^+ \(.*\)$/\n$ <b>\1<\/b>/g;"
A place for John to record his techy notes, both to refer back to when needed, and as a help for others...
Monday, February 23, 2009
heads or tails? don't be so negative...
The number of lines options with
After 15 years of using head and tail (and wondering why they encourage the use of the "extra" '-n' option now!) a question just occurred to me...
"What if I don't know how many lines I want to see with tail, I just know I want to skip the first X lines...?"
The logical opposite of:
would of course be:
Which (thanks to Unix being so logical) naturally works!
You wouldn't really want to use it without a -n in front because +NUM would more likely be a valid file name than a switch, so you do get a warning:
and head just barfs:
So use:
Given the file:
Let's unit test...
means: "head the files '+2' and 'FILENAME'"
all mean: "show the first 2 lines of FILENAME"
means "show all BUT the last 2 lines of FILENAME"
means "I'm being too lazy to use -n, but show me FILENAME from line 2 down"
mean "Show me the last 2 lines of FILENAME"
means "Show me FILENAME from line 2 down"
head and tail do not have to be negative...After 15 years of using head and tail (and wondering why they encourage the use of the "extra" '-n' option now!) a question just occurred to me...
"What if I don't know how many lines I want to see with tail, I just know I want to skip the first X lines...?"
The logical opposite of:
tail -X FILENAMEwould of course be:
tail +Y FILENAMEWhich (thanks to Unix being so logical) naturally works!
You wouldn't really want to use it without a -n in front because +NUM would more likely be a valid file name than a switch, so you do get a warning:
$ tail +20 FILENAME
tail: Warning: "+number" syntax is deprecated, please use "-n +number"
and head just barfs:
$ head +20 FILENAME
head: cannot open `+20' for reading: No such file or directory
So use:
$ tail -n +$SKIP FILENAME
Given the file:
$ seq 1 5 > FILENAME
$ cat FILENAME
1
2
3
4
5
Let's unit test...
$ head +2 FILENAME
head: cannot open `+2' for reading: No such file or directory
==> FILENAME <==
1
2
3
4
5
means: "head the files '+2' and 'FILENAME'"
$ head -2 FILENAME
1
2
$ head -n 2 FILENAME
1
2
$ head -n +2 FILENAME
1
2
all mean: "show the first 2 lines of FILENAME"
$ head -n -2 FILENAME
1
2
3
means "show all BUT the last 2 lines of FILENAME"
$ tail +2 FILENAME
tail: Warning: "+number" syntax is deprecated, please use "-n +number"
2
3
4
5
means "I'm being too lazy to use -n, but show me FILENAME from line 2 down"
$ tail -2 FILENAME
4
5
$ tail -n 2 FILENAME
4
5
$ tail -n -2 FILENAME
4
5
mean "Show me the last 2 lines of FILENAME"
$ tail -n +2 FILENAME
2
3
4
5
means "Show me FILENAME from line 2 down"
Wednesday, February 18, 2009
vi - delete all matching lines...
:%g/^sometext,/ dsearch range (all lines) : %
beginning with "sometext,
execute the Ex cmd "d" ; delete the line
Thought for the day...
People that call it V I instead of "vie"...
...probably don't need a "vi quick reference guide/cheatsheet" anyway.
:o)
...probably don't need a "vi quick reference guide/cheatsheet" anyway.
:o)
Tuesday, February 17, 2009
Add correct host key in /home/john/.ssh/known_hosts to get rid of this message.
john@hostname:~/.ssh$ ssh user@hostname
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
The RSA host key for hostname has changed,
and the key for the according IP address 123.45.67.89
is unknown. This could either mean that
DNS SPOOFING is happening or the IP address for the host
and its host key have changed at the same time.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
01:23:45:67:89:ab:cd:ef:fe:dc:ba:98:76:54:32:10.
Please contact your system administrator.
Add correct host key in /home/john/.ssh/known_hosts to get rid of this message.
Offending key in /home/john/.ssh/known_hosts:9
RSA host key for hostname has changed and you have requested strict checking.
Host key verification failed.
The known_hosts file is now encrypted, which makes it harder to find the bad line...
but you will note the line that says:
Offending key in /home/john/.ssh/known_hosts:9(where :9 means line 9)
If you comment out or remove the line that the error indicates, this should fix the problem.
Alternatively, you can remove an entry for a given host using:
$ ssh-keygen -R hostname
Or find it with:
$ ssh-keygent -F hostname
Thursday, February 12, 2009
find grep hack to show name and content...
For a long time I've done things like:
to get both the filename and the string it's matching on because there's no "name plus match" flag for grep... you either have -l or -n but not together...
I had a brainwave just now...
If you give grep more than one file to grep in - it tells you which file it found the match in... so how about:
If you can't be bothered typing, it can be any valid filename instead of /dev/null - i.e.:
I just figured /dev/null should hopefully have the least overhead to stat.
Be careful you don't give it an invalid filename or you'll get an error for each file the find finds!
for FILE in $(find * -type f -exec grep -l "$STRING" {} \;)
do
echo -n "$FILE"
grep -n "$STRING" $FILE
done
to get both the filename and the string it's matching on because there's no "name plus match" flag for grep... you either have -l or -n but not together...
I had a brainwave just now...
If you give grep more than one file to grep in - it tells you which file it found the match in... so how about:
$ find * -type f -exec grep -n $STRING {} /dev/null \;
path/to/the/FILE:12:I found $STRING on line 12!
If you can't be bothered typing, it can be any valid filename instead of /dev/null - i.e.:
grep -n $STRING {} /
grep -n $STRING {} .
grep -n $STRING {} *
I just figured /dev/null should hopefully have the least overhead to stat.
Be careful you don't give it an invalid filename or you'll get an error for each file the find finds!
$ find * -type f -exec grep STRING {} no_such_file \;
grep: no_such_file: No such file or directory
grep: no_such_file: No such file or directory
grep: no_such_file: No such file or directory
grep: no_such_file: No such file or directory
grep: no_such_file: No such file or directory
Tuesday, October 14, 2008
Holding Debian versions
Hold is a status flag which tells apt (or aptitude) not to automatically upgrade a package.
To hold a package, 'echo pkgname +hold|dpkg --set-selections'
or 'aptitude hold package'
or use = in aptitude's curses interface.
You can ignore a hold by using apt-get install foopkg; or by using ++ in aptitude's curses interface.
[Note that this is *NOT* the same as packages which have been "held back" for dependency reasons.]
Snapshots is an archive that contains all Debian packages uploaded since 2002,
including those removed from the official archives because they were very buggy, unusable, broken, vulnerable or in some way undistributable.
Much of 2004 was lost because of harddisk problems.
See http://snapshot.debian.net/ for more information.
e.g.
dpkg -i /var/cache/apt/archives/sun-java5-bin_1.5.0-14-3_i386.deb /var/cache/apt/archives/sun-java5-jre_1.5.0-14-3_all.deb
aptitude hold sun-java5-bin
aptitude hold sun-java5-jre
Thanks to #debian
To hold a package, 'echo pkgname +hold|dpkg --set-selections'
or 'aptitude hold package'
or use = in aptitude's curses interface.
You can ignore a hold by using apt-get install foopkg; or by using ++ in aptitude's curses interface.
[Note that this is *NOT* the same as packages which have been "held back" for dependency reasons.]
Snapshots is an archive that contains all Debian packages uploaded since 2002,
including those removed from the official archives because they were very buggy, unusable, broken, vulnerable or in some way undistributable.
Much of 2004 was lost because of harddisk problems.
See http://snapshot.debian.net/ for more information.
e.g.
dpkg -i /var/cache/apt/archives/sun-java5-bin_1.5.0-14-3_i386.deb /var/cache/apt/archives/sun-java5-jre_1.5.0-14-3_all.deb
aptitude hold sun-java5-bin
aptitude hold sun-java5-jre
Thanks to #debian
Wednesday, May 28, 2008
Stitching images with ImageMagick
From: http://studio.imagemagick.org/pipermail/magick-users/2002-July/003925.html
Simplified:
If you want to join images side by side:
use:
NB: If they're not photos, you'll want the output to be PNG so it's not lossy.
When you want to join the rows the
Simplified:
If you want to join images side by side:
[ AA.jpg ][ AB.jpg ][ AC.jpg ]
use:
convert +append AA.jpg AB.jpg AC.jpg row_A.png
NB: If they're not photos, you'll want the output to be PNG so it's not lossy.
When you want to join the rows the
+append becomes -append like so:
[ row_A.png ]
[ row_B.png ]
[ row_C.png ]
convert -append row_A.png row_B.png row_C.png all_rows.png
Tuesday, April 29, 2008
Converting seconds to Hours, Minutes & Seconds
It's amazing how many different approaches to doing this you can find on the web.
Some of them are really tortured and inefficient!
These are the tightest two I've found:
To convert the other way, an easy way is to use POSIX mktime:
Some of them are really tortured and inefficient!
These are the tightest two I've found:
#!/usr/bin/perl
#
#
#
use warnings;
use strict;
my $time = time % 86400;
#
# http://www.perlmonks.org/?node_id=101511
#
printf "%02d:%02d:%02d\n",(gmtime $time)[2,1,0];
#
# http://www.perlmonks.org/?node_id=101548
#
printf "%02d:%02d:%02d\n", ($time/3600)%24, ($time/60)%60, $time%60;
To convert the other way, an easy way is to use POSIX mktime:
#!/usr/bin/perl
use warnings;
use strict;
use POSIX qw(mktime);
my $day = "2008-05-28";
my ($d_year, $d_mon, $d_mday) = split /-/, $day;
my $secs = mktime(0, 0, 0, $d_mday, $d_mon-1, $d_year-1900);
Wednesday, April 02, 2008
Showing a function in ksh
If you need to see the contents of a function in ksh...
if you do typeset -f by itself it will list all of the defined functions.
$ typeset -f [FUNCTION_NAME]
if you do typeset -f by itself it will list all of the defined functions.
Subscribe to:
Posts (Atom)