Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

looping lines 3

Status
Not open for further replies.

TheDash

MIS
Mar 25, 2004
171
US
Hi,

Could someone suggest a solution?

Basically I want to take each line in a file, compress multiple spaces to single space in each line and write them to outfile. I have this,

var=`cat infile`
var=$(echo "$var" | sed 's!! !g')
echo "$var\n" >> $outfile

Thanks
 
Since the number of columns is variable, you have to capture the maximum. Changes highlighted...
Code:
awk '{
   for (col=1; col<=NF; col++) {
      if (max[col] < length($col))
          max[col] = length($col)
      arr[NR, col] = $col
   }
   [highlight]if (maxnf < NF)[/highlight]
       [highlight]maxnf = NF[/highlight]
}
END {
   for (row=1; row<=NR; row++) {
      for (col=1; col<=[highlight]maxnf[/highlight]; col++)
         printf "%-" max[col] "s  ", arr[row,col]
      print ""
   }
}' file1
 
Nice work, Ygor, but we see the column 5 is right-aligned.

Code:
#!/bin/bash
#
# Table-view script
#
cat << EOF > table.tmp
.TS
center,tab (_);
l l l l r l l l l.
EOF
#
# replace with your data-producing cmd:
#
cat tbl.data | sed 's/  */_/g' >> table.tmp
#
echo ".TE" >> table.tmp
tbl table.tmp | nroff | head -n8
which leads to the following - wide output, which I have trimmed at layout-unspecific places, to keep the page in a not too wide range. (It already happened before).
Code:
942   07/07/2004   08:17:00   aaaaaaaaaa                     2393286   ORA-02290:   check       c   vio
997   07/07/2004   14:21:28   aaaaaaaaaaaaaaaaaaaaaaaaaaa   43434343   Primary      Violation   c   vio
033   07/07/2004   14:21:28   aaaaaaaaaaaaaaaaa              2933101   ORA-00001:   unique      c   vio
891   07/08/2004   08:23:40   aaaaaaaaaaa                    3093754   Primary      Violation   c   vio

seeking a job as java-programmer in Berlin:
 
stefanwagner,

I think you are just inventing imaginary problems now, but you can right align column 5 just by changing the awk printf statement to...
Code:
printf [b](col==5?"%":"%-")[/b] max[col] "s  ", arr[row,col]
However, you could also use different formatting for each column like this....
Code:
awk '{
   for (col=1; col<=NF; col++) {
      if (max[col] < length($col))
          max[col] = length($col)
      arr[NR, col] = $col
   }
   if (maxnf < NF)
       maxnf = NF
}
END {
   for (row=1; row<=NR; row++) {
      for (col=1; col<=maxnf; col++) [b]{
         if (col==5)
              fmt="%" max[col] "s  "
         else if (col>5)
              fmt="%s "
         else
              fmt="%-" max[col] "s  "[/b]
         printf [b]fmt[/b], arr[row,col]
      [b]}[/b]
      print ""
   }
}' file1
Notice that the 5th column is right-aligned and that column 6 onwards are variable-width....
[tt]
0000000083942 07/07/2004 08:17:00 aaaaaaaaaa 2393286 ORA-02290: check constraint violated
0008015669997 07/07/2004 14:21:28 aaaaaaaaaaaaaaaaaaaaaaaaaaa 43434343 Primary Violation
0000000149033 07/07/2004 14:21:28 aaaaaaaaaaaaaaaaa 2933101 ORA-00001: unique constraint violated
0008015696891 07/08/2004 08:23:40 aaaaaaaaaaa 3093754 Primary Violation[/tt]
 
stefanwagner,

How would you use tbl and nroff to set column 6 onwards to be variable-width?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top