How can I fit a 1024 character file in a unix environment?

I have a file that I need to fill each line with spaces up to about 1100 characters from a bash

script. Each line in the file is currently 900 characters long.

The natural way to do this is

awk -F, '{printf("%-1100s\n",$0)}'  src.txt > dst.txt

      

However, I get the error

awk: formatted string too long
 record number 1

      

After some experimentation and searching on the web, I determined that the max formatted string length awk

can handle 1024.

Is there a better way to get around this limitation?

(note: I am running on SunOS 5.10 and I cannot add GNU tools etc. to it)

+3


source to share


4 answers


Get GNU awk.

$ awk 'BEGIN{printf "%-1100s\n", "foo"}'
     foo

      



You are probably using an old, broken one awk

- use nawk

or /usr/xpg4/bin/awk

on Solaris. If you have this problem with one of them, use the other.

+4


source


There was another solution I came up with:

awk -F, '{printf("%-900s\n",$0)}'  src.txt > tmp1.txt
awk -F, '{printf("%200s\n","")}'  src.txt > tmp2.txt
paste -d "\0" tmp1.txt tmp2.txt > dst.txt

      



This results in the same file as

nawk -F, '{printf("%-1100s\n",$0)}' src.txt > dst.txt 

      

+2


source


If you have installed perl

in the system (probably yes), you can install the script as follows and then run it as pad.pl input.txt 1100 > output.txt

.

#! /usr/bin/perl

open (INPUT, "<$ARGV[0]");
$LENGTH=$ARGV[1];

while (<INPUT>) {
    chomp($_);
    while (length($_) < $LENGTH ) { $_ = $_." "; }
    print $_."\n";
}

close INPUT;

      

+1


source


You may try:

awk '{ pad=1100-length($0); x = $0; 
    while (pad > 0) { x = x " "; pad--}; print x }' src.txt >dst.txt

      

..., which avoids use entirely printf()

, but does multiple string concatenations instead.

0


source







All Articles