UNIX/Linux

A simple use of sed

May 26th, 2004

sed and awk are programs I usually get by without, but here's a handy use for sed.

Say you've got a file called johnbrown.txt containing the following text:

This file is the property of John Brown.
John Brown will be very annoyed if anyone alters it.
Signed: John Brown.

Run the following command:

sed -e s/John Brown/Jill Green/g johnbrown.txt > jillgreen.txt

You now have a new file containing the following:

This file is the property of Jill Green.
Jill Green will be very annoyed if anyone alters it.
Signed: Jill Green.

Not much use for a three-line file, but very handy for a 3000-line file.

Find out more about sed at:
http://pegasus.rutgers.edu/~elflord/unix/sed.html

If you want to do anything more complex than this (i.e. match whole words only, or match across multiple lines) then you can do it in sed, but it's painful. You're much better switching over to Windows and using Replace from Eluent Tools.

Potentially similar posts

Leave a comment

 

SFU 3.5 extras

May 13th, 2004

SFU 3.5 is excellent value for money (at $0) but lacks a few common UNIX programs/utilities. However, additional components for SFU3.5 are available for free download from Interop Systems:

www.interopsystems.com/tools/warehouse.htm

For example:

OpenSSH
bash
vim
Apache

To install these, you need to install the package installer.
You do this by going to:

ftp://ftp.interopsystems.com/pkgs/3.5/

and downloading a shell script called pkg-1.6-bin35.sh, or similar (there might be a newer release by the time you read this). For example, save it to your SFU root directory (usually C:\SFU).

After downloading, run the script from an SFU shell prompt.

This adds the programs pkg_add, pkg_info, pkg_delete, pkg_create and pkg_sign to /usr/local/bin.

Download the package you want to install (e.g. OpenSSH at ftp://ftp.interopsystems.com/pkgs/3.5/openssh-current-bin.tgz) unzip it, then run the package installer on it, e.g:

pkg_add openssh-current-bin.tar

The above example adds SSH, so that you can login to the machine using SSH (e.g. using PuTTY).

Leave a comment



Use tr to transform a text string

April 20th, 2004

Problem:
In a shell script to back up a directory, you want to save the directory as a tar file whose name includes the full original path (just in case you have multiple directories, in separate locations, with the same name). So you want to save a directory with the path /dev/fs/D/mydir to a tar file called BACKUP_.dev.fs.D.mydir.

Solution:
Use tr to transform the path string. For example, the path to the directory you are backing up (/dev/fs/D/mydir) is stored in the variable $DIRECTORY. Use the following commands to create a variable called $NAME containing BACKUP_.dev.fs.D.mydir.

NAME = `echo $DIRECTORY | tr '/' '.'`
NAME = BACKUP_`echo $NAME | tr '\' '.'`

Note: don't mix up the backticks and the single quotes.

Leave a comment



Using rcp and cron to perform a scheduled network backup

April 14th, 2004

I recently set up a cron job on my PC to backup files from the PC to a network server every night. The technique uses SFU on the PC, rcp to copy the files to the network and cron to perform the backup at 3.15 every morning.

For a full description, see:

www.itauthor.com/unix_linux_shell/shell-backup-script.html

Leave a comment



Grepping ls output

April 14th, 2004

grep is a useful tool for modifying the output from ls (the UNIX directory listing command).

For example, if you want to list only the files beginning with a full stop ("."), you can use the command:

ls -a | grep '^\.'

Note: if you're going to be using \ or * you need to enclose the expression that you pass to grep in single quotes.

I frequently want to list just the directories within the current directory. This is especially useful in a directory with lots of files. To do this I use the command:

ls -l | grep ^d

Which produces something like:

drwxr-sr-x   2 me    mygroup   512 Mar  1  2002 News
drwx--S---   2 me    mygroup   512 Dec  8 12:42 Trash
drwxr-sr-x   3 me    mygroup   512 Jan  9 15:59 cvs
drwxrwsrwx   7 me    mygroup   512 Apr 14 00:28 documents-archive
drwx------   2 me    mygroup   512 Aug  4  2003 downloaded-files
drwx------   2 me    mygroup   512 Sep 18  2002 mail
drwxr-sr-x   3 me    mygroup   512 Feb 11 23:03 perlprogs
drwxrwsrwx   2 me    mygroup   512 Apr 13 23:44 temp
drwx------   2 me    mygroup   512 Aug 28  2003 upload-from-here

If you're only interested in the directory names use:

ls -l | grep ^d | awk '{print $9}'

Which prints only column 9, giving you:

News
Trash
cvs
documents-archive
downloaded-files
mail
perlprogs
temp
upload-from-here

Read the rest of this entry »

Potentially similar posts

Leave a comment



^ back to top ^

Page 7 of 9« FirstNewer56789