
How to join five rows in one row?
Quote:
> HI
> I have a problem to join 5 rows in a text in only one row.
> as example below:
> TapeID : 000001
> Available : YES
> GroupID : DEFAULT
> Instance : 1
> Location : IN LIBRARY
> TapeID : 000002
> Available : YES
> GroupID : DEFAULT
> Instance : 1
> Location : 1000
> The text have 10000 rows
> I need write a new text as example below for use awk language.
> TapeID ..... Available ......GroupID ....Instance....Location ....
> TapeID ..... Available ......GroupID ....Instance....Location ....
> how I can make this?
Here is one way of doing it.
Basically, for each line, store the data (after setting the first
two fields to the empty string -- you might want to do
something more elegant here.
When we get a full set of five, print them as one record.
There are other approaches you could take, perhaps based on
NR%5, but you should be able to develop it from here.
/TapeID/ { $1 = $2 = ""; tape = $0 }
/Available/ { $1 = $2 = ""; avail = $0 }
/GroupID/ { $1 = $2 = ""; grp = $0 }
/Instance/ { $1 = $2 = ""; inst = $0 }
/Location/ {
$1 = $2 = ""; loc = $0
# check that all fields have been set
if (tape == "" || avail == "" || grp == "" || inst == "" || loc == "")
{
printf("bad data") > "/dev/stderr" # Change if not Gnu awk
# do something intelligent
}
# now print it all on one line
printf("%s ... %s ... %s ... %s ... %s\n",
tape, avail, grp, inst, loc)
# set all fields to "" so we can check they get set (see above)
tape = avail = grp = inst = loc = ""
Quote:
}
You might prefer to not print the line in the case of bad data, in which
case just make that line the "else" clause of the above if statement.
John.