1833730 Members
2059 Online
110063 Solutions
New Discussion

Colunm formating

 
SOLVED
Go to solution
Jose Mosquera
Honored Contributor

Colunm formating

Hi pals,

Pls a *easier* way to echo a $VAR content in n colummns, i.e:

VAR="01 02 03 04 05 06 07 08 09 10 11"
I need echo $VAR up to four columns format:
01 02 03 04
05 06 07 08
09 10 11

BR,
9 REPLIES 9
Massimo Bianchi
Honored Contributor

Re: Colunm formating

use "cut"

$ echo $VAR
1 2 3 4 5 6 7 8 9 10
$ echo $VAR | cut -f 1-3 -d\ 1-3
1 2 3

repeat "cut" as necessary to obtain the desidered output.

HTH,
Massimo
Kent Ostby
Honored Contributor

Re: Colunm formating

In your shell file put in a line that says:

echo $VAR | awk -f useme.awk

Create a file called useme.awk which contains a SINGLE LINE (you may have to join lines after you cut and paste) that has this in it:

{for (idx1=1;idx1<=NF;idx1++) { printf ("%3s",$idx1); if ((idx1%4==0)||(idx1==NF
)) {printf ("\n");} } }

Note, the "3s" is making columns three spaces wide. If you want it to just have the columns be the width of your data then take out the "3". If you want the data columns wider then change the "3" to whatever width column you want.

Best regards,

Kent M. Ostby
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Jose Mosquera
Honored Contributor

Re: Colunm formating

Thanks Massimo,

I need it in a single sweep.

BR,
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Colunm formating

Here's one pure shell method:

#!/usr/bin/sh

VAR="01 02 03 04 05 06 07 08 09 10 11"
set ${VAR}
while [[ ${#} -gt 0 ]]
do
typeset -i10 KNT=0
while [[ ${KNT} -lt 4 && ${#} -gt 0 ]]
do
echo "${1}\t\c"
shift
((KNT += 1))
done
echo
done

You probably would want to make a function out of this to call it with multiple variables.
If it ain't broke, I can fix that.
Ralph Grothe
Honored Contributor

Re: Colunm formating

strange that no one has mentioned good old fold yet?

e.g.

suppose you have only two digit wide columns,
how about something like this

$ var="12 43 28 29 23 21 38 21 93 21 29 29 39 29"
$ echo $var|fold -w 12
12 43 28 29
23 21 38 21
93 21 29 29
39 29
$
Madness, thy name is system administration
Rodney Hills
Honored Contributor

Re: Colunm formating

use "xargs"-

echo $VAR | xargs -n4 echo

Just specify number of columns on -n parameter.

HTH

-- Rod Hills
There be dragons...
Hein van den Heuvel
Honored Contributor

Re: Colunm formating

Here is a pretty silly solution in perl.


perl -e '$_=$ENV{"var"}; s/(\S+\s+\S+\s+\S+\s+\S+)\s+/$1\n/g; print "$_\n"'

or similar to the awk solution is:

echo $var | perl -e 'foreach $w (split(/ /,<>)){$s=(++$i % 4)?" ":"\n"; print "$w$s"}'

Or...

export $var
perl -e 'foreach $w (split (/ /,$ENV{"var"})) {$s=(++$i % 4)?" ":"\n"; print "$w$s"}'

I tried it in sed, but inserting a newline is too ugly for my taste for lack of a \n.
You have to use multiple lines. Some like

echo $var | sed -e "s/ /\\
/4" -e "s/ /\\
/7" -e "s/ /\\
/10"

Or very ugly with a post-translated helper character "q":

echo $var | sed -e "s/ /q/4" -e "s/ /q/7" -e "s/ /q/10" | tr q \\012
Elmar P. Kolkman
Honored Contributor

Re: Colunm formating

Better way with sed:
print $(echo $VAR | sed 's|\([^ ]* [^ ]* [^ ]* [^ ]*\) |\1\\n|g')

The sed command will replace every fourth space to '\n' which will be interpretted by print as a newline...
Every problem has at least one solution. Only some solutions are harder to find.
Hein van den Heuvel
Honored Contributor

Re: Colunm formating

Thanks! I now see how I can get a substituted 'normal' "\n" to turn into a newline throug print. Cheers, Hein.