unix sort

[problem]

You want to perform a sort:

… But ignore leading blanks;
… Perform numerical sort;
… Offset it to the third column;
… Reverse it;

[/problem]

[solution]

As always with UNIX we supply the arguments, with letters.

See the example tag.

[/solution]

[example]

8 flags to the UNIX sort command.

1) -b # ignores leading blanks.

2) -f # ignore case

3) -n # numerical

4) -r # reverse

5) –key=pos# # sort by key or +# in Solaris, etc take note of -t though for field separator

6) -o # output to file, useful for doing a sort -o fileA fileA – rather than redirect than can overwrite.

7) -u # strip unique lines. You can also use the uniq command, which includes a -c option to count repeating lines.

8) -i # ignore non-printable chars

[/example]

[reference]

[tags]UNIX Sort, UNIX, sort, UNIX Coding School[/tags]

[/reference]

Solaris Network Tracing

[problem]

You are experiencing issues on the network, or a server process is playing up.

[/problem]

[solution]

You need to trace network connection. In the example I provide examples using Solaris and Linux. AIX is similar to Linux in the is regard – either using tcpdump or iptrace.

[/solution]

[example]

# network trace connection from hostname (solaris)

snoop -x0 src hostname

# network trace connections by port (linux)

tcpdump -A -v -v port 80

tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
20:22:33.456278 IP (tos 0x0, ttl 128, id 63194, offset 0, flags [none], proto: TCP (6), length: 571) 10.0.0.8.4571 > bree.http: P 1:532(531) ack 1 win 32850
….GET /man/bash-1?sect=1&=kill HTTP/1.

[/example]

[reference]

[tags]Solaris Network Tracing, Solaris snoop, snoop, UNIX Coding School[/tags]

[/reference]

Linux Debugging strace

[problem]

You have a problem with a process, running some job on a Linux box and need to see some debug information.

Sometimes the problem is a missing library, etc and you need to spot the missing dependency.

Symptoms could be a binary runs fine on one system and fails on another.

[/problem]

[solution]

On Linux use strace, which takes basically the same options as truss on Solaris.

[/solution]

[example]

You just use -e, like this:


strace -e’open’ ls
open(”/etc/ld.so.preload”, O_RDONLY) =
-1 ENOENT (No such file or directory)
open(”/etc/ld.so.cache”, O_RDONLY) = 3
open(”/lib/tls/librt.so.1″, O_RDONLY) = 3
open(”/lib/libtermcap.so.2″, O_RDONLY) = 3
open(”/lib/libacl.so.1″, O_RDONLY) = 3
open(”/lib/tls/libc.so.6″, O_RDONLY) = 3
open(”/lib/tls/libpthread.so.0″, O_RDONLY) = 3
open(”/lib/libattr.so.1″, O_RDONLY) = 3
open(”/usr/lib/locale/locale-archive”, O_RDONLY|O_LARGEFILE) = 3
open(”.”, O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY) = 3
open(”/etc/mtab”, O_RDONLY) = 3
open(”/proc/meminfo”, O_RDONLY) = 3

[/example]

[reference]

[tags]Linux strace, Linux, strace, UNIX Coding School[/tags]

[/reference]

Solaris Debugging

[problem]

You have a problem with a process, running some job on Solaris and need to see some debug information.

Sometimes the problem is a missing library, etc and you need to spot the missing dependency.

Symptoms could be a binary runs fine on one system and fails on another.

[/problem]

[solution]

Run truss with the binary, to spot all the files it opens and for additional information. Sometimes you can even spot where the log files are. 🙂

[/solution]

[example]

This will show system process debug (solaris) for all exec/reads for a running process.

truss -xall -vall -rall -t'read' -p PID

This will run truss with the process.

truss -xall -vall -rall -t'read' program

[/example]

[reference]

[tags]Solaris Debugging, truss, UNIX Coding School[/tags]

[/reference]

Simple Basic UNIX – more advanced vi tips

[problem]

Continuing along with more vi tips, these can help you out of a jam or just save you time – going back to command line, etc.

Saving a file, which is read-only? Running java, Perl, PHP from within vi.

[/problem]

[solution]

Use chmod.

[/solution]

[example]

Have you ever edited a file, made your changes only to find the file is write only? Then in zsh at least, it won’t save it.

Here is a simple work around:

 :!chmod u+w % 

Percent is a shortcut in vi to the current file.

How about shell programming or PHP, Perl or even Java. You want to run the code, just like this:

 !./% !javac %;java `echo % | sed 's/.java//'` !php -q % 

[/example]

[tags]UNIX, PHP, Perl, Java[/tags]

Useful UNIX tip – generate 512 bytes of random data with dd

[problem]

You want to generate 512 bytes of random data

[/problem]

[solution]

Use UNIX dd (disk to disk) command with /dev/random blocks for truly random data, whereas /dev/urandom just pumps out whatever is available.

[/solution]

[example]

You need to escape the control characters, or else it will trash your screen. cat -ve escapes these control characters for you.

dd bs=1 count=512 if=/dev/urandom | cat -ve

dd is a very powerful command and can be used to read files, data, tapes, even disks, etc – varying the number of blocks with count and block size with bs.

if is the input device and of can be used to designate an output device.

dd bs=1 count=512 if=/dev/urandom | openssl base64

Also it can convert to upper and lower case, with conv=ucase, etc.

If you have an extremely large file, it can open at an offset – via the skip option, for example to skip the first 1k of the file and read 100 bytes:

dd if=access_log.techieblogs bs=1 skip=1024 count=1000  - - [04/Jun/2006:00:13:10 +0800] "GET /mysqldemo/run_q.php?database=mysql HTTP/1.1" 200 1364

[/example]