Thursday, May 15, 2008

Chap 6 - Linuxgym

2)List cut
Create a file called "names.txt" containing only the first column of

/usr/local/linuxgym-data/census/femalenames.txt

in its existing order. Ensure that there are no other changes to the data.

A :

cat table.csv | cut -f1 -d,


6)Grep and regular expressions
Create a file called "natmat.txt" containing, IN THE ORIGINAL ORDER, only the lines of

/usr/local/linuxgym-data/census/femalenames.txt

starting with the string 'NAT' or 'MAT'. Ensure there is no extra white space in the file "natmat.txt".

Q:
egrep '^NAT|^MAT' /usr/local/linuxgym-data/census/femalenames.txt > natmat.txt

7)Name frequency
Create a bash script called namefreq.sh which takes one argument - a name. The output should be the FREQUENCY of that name's use, which is the entry in the second column of /usr/local/linuxgym-data/census/femalenames.txt

For example

./namefreq.sh ANA

will return

0.120

and nothing else.

Q :

#!/bin/bash

grep -w $1 ./table.csv | cut -f2 -d,

Single file word detection
Write a bash script called gender.sh which takes one argument - a name. The script should print "female" if the word appears in the file /usr/local/linuxgym-data/census/femalenames.txt, and "male" otherwise.

Hint: Redirect grep's stdout so it doesn't get printed, or use the grep "quiet" option.

A:

#!/bin/bash

grep -q $1 ./table.csv

if [ $? = 0 ]
then
echo female
else
echo male
fi

1 comment:

Unknown said...

Do u have answer for final Linuxgym chp6 excercise called Same Frequency?