Posts Tagged ‘unix’

Use grep to match on the beginning of a line

Tuesday, June 9th, 2009

Hi

Just a simple one but a syntax that I can never remember when I need to so a quick post may help jog mine, and your, memory.  This grep syntax is for matching lines from a text file that begin with a particular word or expression :

$ grep -w '^Word' input_file.txt > output_file.txt
This will search input_file.txt for lines that begin with ‘Word’ and then redirect the output to output_file.txt.

Removing £ signs from a file using sed

Tuesday, December 2nd, 2008

Hi

If you’ve ever tried to use ’sed’ to remove the UK pound sign (£) from a file you may have struggled.  We were bashing our head against the wall until we tried this little trick :

 sed -e 's/'`echo -e "\xA3"`'//g' input.txt > output.txt

Note the important difference between the single-quote and the ‘back-tick’ around the echo.

We had been trying to use the pound sign directly in the terminal window but it just wasn’t matching for some reason.  We then used a Hex Editor  to find the raw code that was being used to represent the pound sign in the file and used that instead and voila!, it works.

 

Converting tab delimited data to CSV data

Tuesday, July 29th, 2008

Here’s a little trick we discovered for converting tab delimited text to comma separated (CSV).  You’ll need access to a Unix, Linux or Mac OS X machine as it relies on the ‘tr’ command line not present on Windows. 

To convert the file simply :

$ tr '\t' ',' < file.tab > file.csv

where ‘file.tab’ is your tab-delimited file and ‘file.csv’ is the file to write to.

And you’re done!