Unix Shell - Write output into a given column name

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2006 > Write output into a given column name





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Write output into a given column name
Lie-Algebra

2006-10-24, 7:17 pm

Hi,

I am trying to redirect some output to a file which contains a given
number of columns.

Suppose the head of the file is like :

(Line 1) # Column1 Column2 Column3 ... Column6

(Line 3) Data1 Data2 Data3 Data6

..
..
..

The output mentionned above is made of 6 fields but and each field value
may differ from the other because of its length.

My idea was to set properly and definitively (Line 1) using widely
horizontal tabs and then redirect the output into each of these 6 fields.

This may sound easy but I must keep for each of theses columns a perfect
row alignement.

I partially solve the problem using tabs but for some values, the row
alignement condition is not fullfilled.

So I figure out it could be possible to write to a file into a given
column...

Is this possible ?

Thks
osiris@abydos.kmt

2006-10-25, 1:32 am


On Wed, 25 Oct 2006 00:40:43 +0200, Lie-Algebra <Lie-Algebra@Amitsur-Levitski.net> wrote:
>
>Hi,
>
>I am trying to redirect some output to a file which contains a given
>number of columns.
>
>Suppose the head of the file is like :
>
>(Line 1) # Column1 Column2 Column3 ... Column6
>
>(Line 3) Data1 Data2 Data3 Data6
>
>.
>.
>.
>
>The output mentionned above is made of 6 fields but and each field value
>may differ from the other because of its length.
>
>My idea was to set properly and definitively (Line 1) using widely
>horizontal tabs and then redirect the output into each of these 6 fields.
>
>This may sound easy but I must keep for each of theses columns a perfect
>row alignement.
>
>I partially solve the problem using tabs but for some values, the row
>alignement condition is not fullfilled.
>
>So I figure out it could be possible to write to a file into a given
>column...
>
>Is this possible ?
>
>Thks


What's wrong with printf?

Where C1 - C6 equal the desired column widths
and COL1 - COL6 contain the desired data:

while read line
do
printf "%${C1}s%${C2}s%${C3}%${C4}%${C5}%${C6}" \
"$COL1" "$COL2" "$COL3" "$COL4" "$COL5" "$COL6"
done < mydata

You can get the column headings with a printf preceding the loop.
Ed Morton

2006-10-25, 1:32 am

Lie-Algebra wrote:
> Hi,
>
> I am trying to redirect some output to a file which contains a given
> number of columns.
>
> Suppose the head of the file is like :
>
> (Line 1) # Column1 Column2 Column3 ... Column6
>
> (Line 3) Data1 Data2 Data3 Data6
>
> .
> .
> .
>
> The output mentionned above is made of 6 fields but and each field value
> may differ from the other because of its length.
>
> My idea was to set properly and definitively (Line 1) using widely
> horizontal tabs and then redirect the output into each of these 6 fields.
>
> This may sound easy but I must keep for each of theses columns a perfect
> row alignement.
>
> I partially solve the problem using tabs but for some values, the row
> alignement condition is not fullfilled.
>
> So I figure out it could be possible to write to a file into a given
> column...
>
> Is this possible ?
>
> Thks


Does this do what you want:

$ echo "a b c" |
awk 'BEGIN{col="%-15s"; fmt=col col col "\n";
printf fmt, "Column1", "Column2", "Column3"}
{printf fmt, $1, $2, $3}'
Column1 Column2 Column3
a b c

Regards,

Ed.
Michael Tosch

2006-10-25, 1:27 pm

Lie-Algebra wrote:
> Hi,
>
> I am trying to redirect some output to a file which contains a given
> number of columns.
>
> Suppose the head of the file is like :
>
> (Line 1) # Column1 Column2 Column3 ... Column6
>
> (Line 3) Data1 Data2 Data3 Data6
>
> .
> .
> .
>
> The output mentionned above is made of 6 fields but and each field value
> may differ from the other because of its length.
>
> My idea was to set properly and definitively (Line 1) using widely
> horizontal tabs and then redirect the output into each of these 6 fields.
>
> This may sound easy but I must keep for each of theses columns a perfect
> row alignement.
>
> I partially solve the problem using tabs but for some values, the row
> alignement condition is not fullfilled.
>
> So I figure out it could be possible to write to a file into a given
> column...
>


man printf

Example:

printf "%20s%20s%20s\n" 12 1234 123
puts the values right-adusted into 20 characters wide fields.

printf "%-20s%-20s%-20s\n" 12 1234 123
is left-adjusted.

--
Michael Tosch @ hp : com
Lie-Algebra

2006-10-25, 1:27 pm

Michael Tosch a écrit :
> Lie-Algebra wrote:
>
> man printf
>
> Example:
>
> printf "%20s%20s%20s\n" 12 1234 123
> puts the values right-adusted into 20 characters wide fields.
>
> printf "%-20s%-20s%-20s\n" 12 1234 123
> is left-adjusted.
>


Yeah, I manage to do the trick using printf and by passing it a variable
that contains the column widths as shown in this thread.

So far, I thought printf was essentially related to the c-function, but
I was wrong...

Thks for all ;)
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com