How to join five rows in one row? 
Author Message
 How to join five rows in one row?

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?

Thanks
Carlos Biazoto
Brazil



Fri, 06 May 2005 22:20:07 GMT  
 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.



Sat, 07 May 2005 00:07:11 GMT  
 How to join five rows in one row?
If you're sure of the format, then the following should the trick:

        { gsub(".*: ","",$0) }              # eliminate the descriptions
i++<4   { b = b $0 " ..... " }             # concatenate the first four lines
                                        # with a delimiter after each one
i==4    { print b $0; b = ""; i = 0}  # print the first 4 items plus the
fifth one

The code is untested, so exercise proper caution.

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?

> Thanks
> Carlos Biazoto
> Brazil



Sat, 07 May 2005 00:37:02 GMT  
 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?

First make certain the input is in expected format before you run it
through this  : > Im only a fan of awk not a grand master coder
so my idea is probably grossly simplistic and ugly:

awk '{if($1~/^Location/){printf("%s:%s %s\n",$1,$3,$4)}else{printf("%s:%s ",$1,$3)}}' file

This will print an extra space if there is no $4 :
TapeID:000001 Available:YES GroupID:DEFAULT Instance:1 Location:IN LIBRARY
TapeID:000002 Available:YES GroupID:DEFAULT Instance:1 Location:1000



Sat, 07 May 2005 02:23:10 GMT  
 How to join five rows in one row?


Quote:
>I have a problem to join 5 rows in a text in only one row.
>as example below:
>TapeID                  : 000001
>Available               : YES
>  .        .           .      .
>  .        .           .      .
>I need write a new text as example below for use awk language.
>TapeID  ..... Available ......GroupID ....Instance....Location  ....
>TapeID  ..... Available ......GroupID ....Instance....Location  ....

To begin  $ awk -f col.awk textfile
## col.awk  
BEGIN {ORS=""}                                 #n1
/TapeID/      { printf ("%1s",$3)}             #n2
/Available/   { printf ("%11s",$3)}            #n3
/GroupID/     { printf ("%11s",$3)}            #n3
/Instance/    { printf ("%11s",$3)}            #n4
/Location/    { printf ("%11s\n",$3)}          #n5

if textfile is complete , and original "fields"  in textfile consist
of one word.
But IN LIBRARY consists of two words so change n5 to
/Location/     {printf ("%11s %s\n",$3,$4)}      
( Pattern can also be  NR==k%5   ,where k=1 in n2, k=2 in n3 etc.
n1 can be changed into )
BEGIN{printf("TapeID     Available ................\n\n") ;ORS=""}
to print a heading.
Improve the layout.
Suppose there is no GroupID at line 68 ?
Maybe further on in textfile some strange things happen.
And so on; everything AFAIK.

cor



Sat, 07 May 2005 05:04:35 GMT  
 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?

> Thanks
> Carlos Biazoto
> Brazil

awk '{if (NR%5!=0) printf("%s ",$0); else print $0}'  should work.

TK



Sat, 07 May 2005 07:18:10 GMT  
 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?

> > Thanks
> > Carlos Biazoto
> > Brazil

> awk '{if (NR%5!=0) printf("%s ",$0); else print $0}'  should work.

> TK

A reductionist derivitive with no whitespace in the program:

    awk '{printf"%s",$3(NR%5?OFS:ORS)}' data

    - Dan



Sat, 07 May 2005 09:05:14 GMT  
 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?

>> > Thanks
>> > Carlos Biazoto
>> > Brazil

>> awk '{if (NR%5!=0) printf("%s ",$0); else print $0}'  should work.

>> TK

>A reductionist derivitive with no whitespace in the program:

>    awk '{printf"%s",$3(NR%5?OFS:ORS)}' data

>    - Dan

I found this pearl in a Google search:

    gawk 'ORS=NR%6?",":"\n"'

I'm sure it is relevant somehow...



Sat, 07 May 2005 09:43:46 GMT  
 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?

I can do that in one line!  :-)

untested, but should work:

awk '{printf("%s %s%s", $1, $3, ($1=="Location" ? "\n" : " "))}' infile

Chuck Demas

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sat, 07 May 2005 10:34:44 GMT  
 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?

>I can do that in one line!  :-)

>untested, but should work:

>awk '{printf("%s %s%s", $1, $3, ($1=="Location" ? "\n" : " "))}' infile

or

awk '{printf("%s %s%s", $1, $3, (NR%5==0 ? "\n" : " "))}' infile

Chuck Demas

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sat, 07 May 2005 10:39:30 GMT  
 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?

> >> > Thanks
> >> > Carlos Biazoto
> >> > Brazil

> >> awk '{if (NR%5!=0) printf("%s ",$0); else print $0}'  should work.

> >> TK

> >A reductionist derivitive with no whitespace in the program:

> >    awk '{printf"%s",$3(NR%5?OFS:ORS)}' data

> >    - Dan

> I found this pearl in a Google search:

>     gawk 'ORS=NR%6?",":"\n"'

> I'm sure it is relevant somehow...

Neat, even shorter, and still no spaces (the mark of a great grogram)...it
seems, somehow, familiar...


Sat, 07 May 2005 12:59:37 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. alternate row color for every other row in table

2. reading an array within a textfile ROW by ROW

3. jumping to a tktable row, make row visible.

4. One Click Row Picture

5. SQL 2000 one row table

6. changing the properties of only one row in a 2D array

7. Operations on a two-dimensional array, one row at a time

8. MySQL: one row in array

9. like modifying variable tcl the present ones on rows external

10. Eric Gunnerson blunders twice in a row!

11. reassigning rows

12. Using expand and filling rows and columns with sums

 

 
Powered by phpBB® Forum Software