Tuesday, December 25, 2007

Add the student name in each line : AWK digest


This is kind of an academic assignment, but seems to be very useful. The input file is a data set of students in the following format.

$ cat data.out

Student=STD1
From GBBHSS

ATTEND
Mon Y
Tue N
Wed Y
Thu Y
Fri N

Student=STD2
From KKBB

ATTEND
Mon Y
Tue N
Wed Y
Thu Y
Fri N

The idea is to append the student name in each line that belongs to that student. This will be useful for parsing the file. i.e. output required is of the following type:

Student=STD1
From GBBHSS STD1
Blank line of STD1
ATTEND STD1
Mon Y STD1
Tue N STD1
Wed Y STD1
Thu Y STD1
Fri N STD1
Blank line of STD1
Student=STD2
From KKBB STD2
Blank line of STD2
ATTEND STD2
Mon Y STD2
Tue N STD2
Wed Y STD2
Thu Y STD2
Fri N STD2

This can be achieved this way:

$ awk ' BEGIN{FS="="}
/Student/ {Name=$2 ; print $0 ; next}
/^$/ { print "Blank line of "Name ; next}
{print $0 " " Name}
' "data.out"

A bit of explanation for the AWK newbies:

1) /Student/ {Name=$2 ; print $0 ; next}
# Lines containing pattern "Student", assign Name="name of the student", print the line, go next
2)
/^$/ { print "Blank line of "Name ; next}
# Blank lines, print the line as "
Blank line of [the student stored in Name variable]"
3) {print $0 " " Name}
# Rest all lines, just append the "Name of the student stored in Name variable after the line

No comments:

© Jadu Saikia www.UNIXCL.com