Saturday, 25 June 2016

Using sed to search and replace text in files within a directory

I needed to edit all files within a directory, replacing an ip address '' with 'localhost'.

sed is a very powerful command line tool. All the help web pages look intimidating so, it took me a long time to appreciate and understand the basics of it.

The basic usage is:
sed [options] commands [file-to-edit]

I used:
sed -i -e 's/10\.0\.7\.52/localhost/g' *

-i option is used to edit in place on filename.

-e option indicates a command to run.

s is the command used to replace the found expression "" with "localhost". The command and the two 'words' are separated by forward slashes.

g stands for "global", meaning to do this for the whole line. If you leave off the g and "" appears twice on the same line, only the first "" is changed to "localhost".

The backslashes are to escape the following character (the dot) otherwise, it would be interpreted as wildcard for a single character and not, as a dot in the ip address.

Finally, the asterisk states to run this on all files in the current directory.

Removing columns in a csv file

Sometimes I get supplied a large comma separated (.CSV) file from a client with erroneous data columns (fields) in it. Most people would say, open it in M$ Excel, highlight the columns you don't want and delete. What if the file is too large to open in M$ Excel? Well, Linux offers the awk command as the fastest solution.

For example, let's assume you have a comma delimited CSV file in your Home directory called myfile.csv and you want to display the first and third column

awk -F, '{OFS=",";print $1,$3}' < ~/myfile.csv

To actually strip out the first and third column from this file, to another file, you pipe the result.

awk -F, '{OFS=",";print $1,$3}' < ~/myfile.csv > ~/mynewfile.csv

You can select as many columns you would like to keep by, adding its column number, pre-pended with a dollar sign.

awk -F, '{OFS=",";print $1,$3,$4,$6}' < ~/myfile.csv > ~/mynewfile.csv

sfdisk: cannot open /dev/sdb read-write

I have Marius's excelent mdadm Cheat Sheet bookmarked ( Well, it is really a crib sheet. A great reference of mdadm, used to manage and monitor software RAID devices.

However, I had a problem with the following (on kubuntu) when trying to copy the partitions from a current drive (sda), to a new drive (sdb).

sfdisk -d /dev/sda | sfdisk /dev/sdb

/dev/sdb: Permission denied

sfdisk: cannot open /dev/sdb read-write

Simply add 'sudo' to both statements for this to work.

sudo sfdisk -d /dev/sda | sudo sfdisk /dev/sdb