Solved awk - printing output

March 13, 2013 at 16:51:11
Specs: Solaris 10 10/08, T5240
Hi all,

I have a file with the following information:
# cat exemplo.txt

I need show these information in the following output:

a e i n
b f j o
c g l p
d h m q

Does anyone know how I can do that ?

Thanks everybody !!

See More: awk - printing output

Report •

March 13, 2013 at 19:51:51
oops, my bad! Wrong OS (Duh!) :-(

Report •

March 14, 2013 at 09:11:59
✔ Best Answer
A solution for this problem is to use a 2-dimensional array. You indicate using awk under Solaris Unix. Awk doesn't support multi dimensional arrays - it only emulates them which I find difficult to use.

Instead, I use a Korn shell script which:
1) reads each line
2) parses the line with set by the pipe symbol
3) creates a variable for each parsed element for a total of 16 variables in this example. i.e. var11, var12, var13, var14, var21, var22, etc

Then, it's a matter of printing the variables out in the proper order with the two while loops.

Let me know if you have any questions:


# assume there are 4 pipe symbol delimited values per line
while read line
   set - $(IFS="|"; echo $line)
   while (($y <= $#))
      eval var$ln$y=\$$y

done < datafile.txt

# assume there are four rows and four columns
while (( $x <= 4 ))
   while (( $y <= 4 ))
      nv=$(eval echo \"\$var$y$x \")
      if [ $y -eq 4 ]
         printf "$nv \n"
         printf "$nv "

Report •

March 15, 2013 at 07:33:26
Hello Nails,

First of all, thanks so much for your reply.

Your script works for the example above, but the real problem is that i have an undetermined number of lines and 92 columns (separated by "|")

Do you know a solution fir this real problem ?

Ps.: Do you know what is wronkg if the commando bellow:
[root@spcas01ium5 claudio]# cat c

[root@spcas01ium5 claudio]# sed 's/^/\n/g' c | sed 's/|/*/g' | awk 'BEGIN {FS="\n|*";RS=""} {for (j=1;j<=3;j++)for (i=1;i<=4;i++) var$j[i]=sprintf("%s",$i)} {print var1[3]}'
awk: BEGIN {FS="\n|*";RS=""} {for (j=1;j<=3;j++)for (i=1;i<=4;i++) var$j[i]=sprintf("%s",$i)} {print var1[3]}
awk: ^ syntax error

Thanks so much for your help !!
Claudio Ract

Report •

Related Solutions

March 15, 2013 at 09:30:29
First, for advanced awk usage under Solaris, try nawk instead. But I don't think you'll get this script to work because I don't think non-GNU awk supports multiple characters for a Field Seperator.

Second, I tried a different design:
1) remove all pipe symbols
2) read each line using xargs to place each column one per line and redirect to its own file.
3) dynamically build the paste command. The paste command merges files. ie paste -d " " fn1.txt fn2.txt fn3.txt .....
4) execute the paste command string.


sed 's/|/ /g' my1.txt > newmy1.txt

cmd="paste -d \" \" "
while read line
  echo "$line"|xargs -n1 > fn${cnt}.txt
  # build the paste command
  cmd="${cmd} fn${cnt}.txt"
done < newmy1.txt
eval $cmd > newfile.txt

# You probably can pipe the sed command in to the while loop if you wish. Let me know if you have any questions.

Report •

Ask Question