Tag Archives: text

Ubuntu: How to add a Suffix (or Prefix) to a Line of Text in a Text File using AWK CodeUnit 18 JUN 2012

If you are looking for a top notch tool to carry out quick text manipulations on text files under Ubuntu terminal, you could probably do no better than by looking up AWK (or I suppose SED if you are so inclined).

Anyway, back to AWK. The AWK utility is a data extraction and reporting tool that uses a data-driven scripting language consisting of a set of actions to be taken against textual data (either in files or data streams) for the purpose of producing formatted reports. The language used by awk extensively uses the string datatype, associative arrays (that is, arrays indexed by key strings), and regular expressions.

AWK was created at Bell Labs in the 1970s, and its name is derived from the family names of its authors – Alfred Aho, Peter Weinberger, and Brian Kernighan. ‘awk’, when written in all lowercase letters, refers to the Unix or Plan 9 program that runs other programs written in the AWK programming language.

Okay, now that we have some history, lets see just how easy it is to add either a prefix or suffix to each line contained in our text file using awk:

#add a prefix to each line in the text file
awk '{ printf("myprefix %sn", $l);}' sample-text-file.txt

#add a suffix to each line in the text file
awk '{ printf("%s mysuffixn", $l);}' sample-text-file.txt

#add both a prefix and a suffix to each line in the text file
awk '{ printf("myprefix %s mysuffixn", $l);}' sample-text-file.txt

The first example will result in each line in the sample text file being prepended with the word ‘myprefix’. The second example will result in each line in the sample text file being appended with the work ‘mysuffix’. I doubt that at this stage I still need to spell out what the third example does!

If you wish to save these changes to a text file, using the standard IO redirect functionality, i.e. the > sign:

#save output to a file
awk '{ printf("myprefix %s mysuffixn", $l);}' sample-text-file.txt > altered-text-file.txt

(Note that you shouldn’t direct the output at the input file as you’ll seriously screw things up. Rather save to a new file instead).


Related Link: http://en.wikipedia.org/wiki/AWK

PHP: Write to Text File Programming 29 SEP 2010

green php elephants - elephpantsJust a simple code snippet to remind myself just how easy it is to spit out content into a text file using PHP.

As you can see from the code snippet below, to write data to a text file in PHP we make use of three functions, namely fopen, fwrite and fclose.

As its name implies, fopen is responsible for opening the stream. Note the use of the ‘w’ switch which indicates we are opening a stream for writing. This will also force the creation of the file if it doesn’t exist.

The fwrite is used to insert text into the stream and finally the fclose essentially saves the changes and closed the handle opened initially by the fopen call.

$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, "Text sentence for line 1n");
fwrite($fh, "Text sentence for line 2n");

Note, if you wish to append data to an existing file, simply use the fopen function with an ‘a’ switch.

This is demonstrated below:

if (is_writable($myFile)) {
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, "Appended text sentence for line 1n");
fwrite($fh, "Appended text sentence for line 2n");

Simple isn’t it? Don’t know why I keep forgetting it then! O.o

Joomla 1.5: Remove Copyright Info from the Bottom of the Page Footer Tips, Tricks and Tutorials 19 AUG 2010

Most Joomla sites, with or without snazzy templates applied, all carry a pretty standard Powered by Joomla! footer text.

Now while most people should be in favour of keeping this in, there are a lot of us out there who simply don’t want it spoiling our tight site design, and so digging around the web revealed the two simplest ways of removing this line of text from your website.

First, you could actually edit the core files of your Joomla 1.5 installation directly. To do this, go to the core files, navigating through core files >> Languages >> en-GB.mod_footer.ini.

Opening this file you will see the lines:

FOOTER_LINE1=Copyright © %date% %sitename%. All Rights Reserved.
FOOTER_LINE2=Joomla! is Free Software released under the GNU/GPL License.
THIS MODULE SHOWS THE JOOMLA! COPYRIGHT INFORMATION=This Module shows the Joomla! Copyright information

Simply change the data to what you would like to display and voila – you are done! :)

A second way is to modify your template file located at yourtemplate/index.php – scroll to the bottom, locate the lines below and change accordingly:

<a href="http://www.joomla.org">Joomla!</a> <a href="http://validator.w3.org/check/referer">XHTML</a>  <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a>

And that is literally that. Nifty.

jQuery: Get all Selected Values or Text from a Multiple Select Listbox CodeUnit 01 MAR 2010

Grabbing all the selected values or even text values from a multiple select listbox turns out to be quite simple if you know which tools to use.

The idea here is pretty simple. First we declare an array to hold our gleaned variables. Then we need to grab all the selected items in the listbox by making use of the :selected jQuery selector. Next we iterate through the selected items, using the standard val() to return the selected option’s value or text() to grab the actual display text that made up the list item.

Translating this all to jQuery code, we get this:

var realvalues = [];
var textvalues = [];
$('#multiplelistbox :selected').each(function(i, selected) {
    realvalues[i] = $(selected).val();
    textvalues[i] = $(selected).text();

And that’s pretty much it. If you check out the two arrays we just created then you’ll see we are now in possession of all selected values, as well as the selected list items’ display text.