formatting columns in txt file

formatting columns in txt file

Post by myusenetaccoun » Mon, 15 May 2006 01:27:35



I'm outputting data to a text file. The data is stored in 4 columns but
each row has variable length. The result is that the columns are not
aligned and obviously it is an ordeal to read this data.

I've tried playing with putting tabs but this doesn't help.
Is is possible to do what I describe?

Thanks

 
 
 

formatting columns in txt file

Post by Xicheng Ji » Mon, 15 May 2006 01:46:42



> I'm outputting data to a text file. The data is stored in 4 columns but
> each row has variable length. The result is that the columns are not
> aligned and obviously it is an ordeal to read this data.

> I've tried playing with putting tabs but this doesn't help.
> Is is possible to do what I describe?

you can use Perl's pack function..


the numbers right behind 'A', like A15, A8, are the width(number of
characters) of a column which depends on your data, the last one A*
reaches to the eol(no need to assign).

Xicheng

Quote:> Thanks


 
 
 

formatting columns in txt file

Post by myusenetaccoun » Mon, 15 May 2006 01:52:12




>> I'm outputting data to a text file. The data is stored in 4 columns but
>> each row has variable length. The result is that the columns are not
>> aligned and obviously it is an ordeal to read this data.

>> I've tried playing with putting tabs but this doesn't help.
>> Is is possible to do what I describe?

> you can use Perl's pack function..


> the numbers right behind 'A', like A15, A8, are the width(number of
> characters) of a column which depends on your data, the last one A*
> reaches to the eol(no need to assign).

> Xicheng

>> Thanks

Thanks for the quick reply but I'm using bash (sorry didn't mention this
before). Any suggestions?
 
 
 

formatting columns in txt file

Post by Xicheng Ji » Mon, 15 May 2006 02:03:19





> >> I'm outputting data to a text file. The data is stored in 4 columns but
> >> each row has variable length. The result is that the columns are not
> >> aligned and obviously it is an ordeal to read this data.

> >> I've tried playing with putting tabs but this doesn't help.
> >> Is is possible to do what I describe?

> > you can use Perl's pack function..


> > the numbers right behind 'A', like A15, A8, are the width(number of
> > characters) of a column which depends on your data, the last one A*
> > reaches to the eol(no need to assign).

> > Xicheng

> >> Thanks

=> Thanks for the quick reply but I'm using bash (sorry didn't mention
this
=> before). Any suggestions?

you can use Perl under bash though, or probably awk, like:

 awk '{ printf "%-12s%-6s%-14s%-s\n", $1, $2, $3, $4 }' yourfile.txt

Xicheng

 
 
 

formatting columns in txt file

Post by Janis Papanagno » Mon, 15 May 2006 02:13:14



> I'm outputting data to a text file. The data is stored in 4 columns but
> each row has variable length. The result is that the columns are not
> aligned and obviously it is an ordeal to read this data.

> I've tried playing with putting tabs but this doesn't help.
> Is is possible to do what I describe?

> Thanks

Yes, there are several possibilities. You may use the pr command to
format your output, e.g.

   pr -at4  < yourfile

Janis

 
 
 

formatting columns in txt file

Post by base6 » Mon, 15 May 2006 05:52:25



> I'm outputting data to a text file. The data is stored in 4 columns but
> each row has variable length. The result is that the columns are not
> aligned and obviously it is an ordeal to read this data.

> I've tried playing with putting tabs but this doesn't help.
> Is is possible to do what I describe?

"man printf"
 
 
 

formatting columns in txt file

Post by Bill Marcu » Mon, 15 May 2006 05:54:32


On 13 May 2006 10:03:19 -0700, Xicheng Jia



>=> Thanks for the quick reply but I'm using bash (sorry didn't mention
> this
>=> before). Any suggestions?

> you can use Perl under bash though, or probably awk, like:

>  awk '{ printf "%-12s%-6s%-14s%-s\n", $1, $2, $3, $4 }' yourfile.txt

printf is also a builtin command in bash.

while read a b c d; do
  printf "%-12s%-6s%-14s%-s\n" "$a" "$b" "$c" "$d"
done < yourfile.txt

--
First Rule of History:
        History doesn't repeat itself -- historians merely repeat each other.

 
 
 

formatting columns in txt file

Post by Chris F.A. Johnso » Mon, 15 May 2006 09:02:18





>>> I'm outputting data to a text file. The data is stored in 4 columns but
>>> each row has variable length. The result is that the columns are not
>>> aligned and obviously it is an ordeal to read this data.

>>> I've tried playing with putting tabs but this doesn't help.
>>> Is is possible to do what I describe?

>> you can use Perl's pack function..


>> the numbers right behind 'A', like A15, A8, are the width(number of
>> characters) of a column which depends on your data, the last one A*
>> reaches to the eol(no need to assign).

> Thanks for the quick reply but I'm using bash (sorry didn't mention this
> before). Any suggestions?

   Use printf:

printf "%-10s %-15s %4d %s\n" "$field1" "$field2" "$field3" "$field4"

--
   Chris F.A. Johnson, author              <http://cfaj.freeshell.org>
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
   ===== My code in this post, if any, assumes the POSIX locale
   ===== and is released under the GNU General Public Licence

 
 
 

formatting columns in txt file

Post by myusenetacco.. » Mon, 15 May 2006 23:17:10






>>>> I'm outputting data to a text file. The data is stored in 4 columns but
>>>> each row has variable length. The result is that the columns are not
>>>> aligned and obviously it is an ordeal to read this data.

>>>> I've tried playing with putting tabs but this doesn't help.
>>>> Is is possible to do what I describe?
>>> you can use Perl's pack function..


>>> the numbers right behind 'A', like A15, A8, are the width(number of
>>> characters) of a column which depends on your data, the last one A*
>>> reaches to the eol(no need to assign).

>> Thanks for the quick reply but I'm using bash (sorry didn't mention this
>> before). Any suggestions?

>    Use printf:

> printf "%-10s %-15s %4d %s\n" "$field1" "$field2" "$field3" "$field4"

Thanks all. I'll use printf as this is built in.