3

I have a file that I need to pad each line with spaces to around 1100 characters from a bash script. Each line in the file currently has 900 characters.

The natural way to to this is

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

However, I get an error telling me

awk: formatted string too long
 record number 1

After some experimentation and searching on the internet, I've determined that the maximum line length of formatted string awk can handle is 1024.

Is there a better way to get around this limitation?

(note: I'm runnin on SunOS 5.10, and I can't add GNU tools to it, etc.)

BIBD
  • 15,107
  • 25
  • 85
  • 137

4 Answers4

4

Get GNU awk.

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

You're probably using old, broken awk - use nawk or /usr/xpg4/bin/awk on Solaris. If you're having this problem with one of them, then use the other one.

BIBD
  • 15,107
  • 25
  • 85
  • 137
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • I see you added a note that says "I'm runnin on SunOS 5.10". Why did you tag your question linux then??? You're probably using old, broken awk - use nawk or /usr/xpg4/bin/awk on Solaris. – Ed Morton Jan 06 '15 at 19:47
  • Linux->SunOS - Corrected, my bad. – BIBD Jan 06 '15 at 20:23
  • 1
    `nawk` was the answer : nawk -F, '{printf("%1100s\n",$0)}' src.txt > dst.txt – BIBD Jan 06 '15 at 20:35
2

There was one other 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 
BIBD
  • 15,107
  • 25
  • 85
  • 137
1

If you have perl installed on that system (probably yes), you can set a script like this, and then run it like 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;
jimm-cl
  • 5,124
  • 2
  • 25
  • 27
0

You could try:

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

... which avoids use of printf() completely but does multiple string concatenations instead.

Simon
  • 10,679
  • 1
  • 30
  • 44