Skip to main content

Posts

Showing posts from 2009

Linux tip - Find and replace in vi

To find and replace one or more occurences of a given text pattern with a new text string, use the s[ubstitute] command. There are a variety of options, but these are what you most probably want: :%s/foo/bar/g find each occurance of 'foo' and replace it with 'bar' without asking for confirmation :%s/foo/bar/gc find each occurance of 'foo' and replace it with 'bar' asking for confirmation first :%s/ /bar/gc find (match exact word only) and replace each occurance of 'foo' with 'bar' :%s/foo/bar/gci find (case insensitive) and replace each occurance of 'foo' with 'bar' :%s/foo/bar/gcI find (case sensitive) and replace each occurance of 'foo' with 'bar' NB: Without the 'g' flag, replacement occurs only for the first occurrence in each line. For a full description and some more interesting examples of the substitute command refer to :help substitute See ...

Linux Tip - Rename set of files

To rename a set of files to a name starting with a particular pattern (here the pattern is taken to be to start the filename with "MyFile_" files = `ls -1`;for file in $files; do mv $file MyFile_$file ; done Dissection: ls -1 ls is the command in linux to list the files in the present directory The parameter -1 will list one file a line "files" is a variable which has the list of all the files listed by the command ls -1 files = `ls -1` save the result of ls -1 to a variable called files Remember to put the command inside a pair of back ticks ` (the key is generally found just before the key "1" in the keyboard) Significance of ` (backtick) for file in $files Loop through each file name available in the variable files do mv $file MyFile_$file; done (The do - done loop) Executes the command given after do End the do with a done