A simple app to read from and output to files


This is a script I wrote to get in touch with some bash basics.

#: Description	: Reads a file with entries in separated
lines and write them to a file with a comma separated format
#:	Author	: Thameera
#: Date	: 23/12/2009

while IFS= read -r name1
	read -r	name2
	read -r age
	if [ -z "$age" ]
	printf "%s %s, %s\n" "$name1" "$name2" "$age"

What this code does is to read from a file specified in the argument list a list with the following format:


and arrange them in the following order in a new file specified by the argument:

firstname1 lastname1, age1
firstname2 lastname2, age2

Suppose you save this file with the name main and your input is in the file in. Then the command to execute the script should be:

bash main <in >out

This will write the output in a new file called out.

When I first wrote this script, I found out that the output would produce an extra blank line with a comma. The if condition if [ -z “$age” ] was added to rectify this. This condition will break the loop when it finds that the variable age is no longer assigned to a valid value.

Well, it does not do any interesting stuff, but helped me to get touch with some basics. Hope to post some more interesting stuff in the time to come.


No Responses Yet to “A simple app to read from and output to files”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: