Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

'Tricky' formatting issue (awk, cut, sed, perl etc)

30 views
Skip to first unread message

Paul D

unread,
Oct 20, 2017, 12:12:48 PM10/20/17
to
I have a file thus:


text 1148 1148 1148 1148 1148 1148 1148
texttext 499 499 499 499 499 499 499
texttexttext 3021 3021 3021 345 767 765 345


and I want to get to:

text 1148 1148 1148 1148 1148 1148 1148
texttext 499 499 499 499 499 499 499
texttexttext 3021 3021 3021 345 767 765 345


The text in the 1st col can vary in width per above. This is probably trivial for some but I've struggled to find an eloquent solution. The first field needs to be 16 chars wide and all the remaining fields 6 chars wide left justified. The file actually has 42 columns so the solution needs to be generic and not specify explicit formatting for all 42 cols (just the 1st and all remaining as occasionally there will be a couple of extra columns..)

Thanks in advance...

Paul D

unread,
Oct 20, 2017, 12:22:15 PM10/20/17
to
urghh - if you view this in a browser the 'want to get to' formatting can get skewed. Just focus on the fact that I need the first field to be 16 chars wide and all the remaining fields 6 chars wide left justified

Kenny McCormack

unread,
Oct 20, 2017, 12:42:33 PM10/20/17
to
In article <fa514ab8-3c1b-4257...@googlegroups.com>,
Paul D <spectre....@gmail.com> wrote:
>I have a file thus:
>
>
>text 1148 1148 1148 1148 1148 1148 1148
>texttext 499 499 499 499 499 499 499
>texttexttext 3021 3021 3021 345 767 765 345
>
>
>and I want to get to:
>
>text 1148 1148 1148 1148 1148 1148 1148
>texttext 499 499 499 499 499 499 499
>texttexttext 3021 3021 3021 345 767 765 345

This is your AWK script:

{
printf("%-16s",$1)
for (i=2; i<=NF; i++)
printf("%-6s",$i)
print ""
}

Put that in a file and do (from the shell):

$ awk -f awkfile yourfile.txt > yournewfile.txt
$

BTW, here's a slightly shorter (and more cryptic) version:

{
for (i=1; i<=NF; i++)
printf("%-*s",6+10*(i==1),$i)
print ""
}
--
"The party of Lincoln has become the party of John Wilkes Booth."

- Carlos Alazraqui -

Ed Morton

unread,
Oct 20, 2017, 1:20:05 PM10/20/17
to
$ awk '{printf "%-16s",$1; for (i=2;i<=NF;i++) printf "%-6s",$i; print ""}' file
text 1148 1148 1148 1148 1148 1148 1148
texttext 499 499 499 499 499 499 499
texttexttext 3021 3021 3021 345 767 765 345

Ed.

Kenny McCormack

unread,
Oct 20, 2017, 1:46:52 PM10/20/17
to
In article <osdb7q$v1n$1...@dont-email.me>,
Ed Morton <morto...@gmail.com> cluelessly wrote:
...
>$ awk '{printf "%-16s",$1; for (i=2;i<=NF;i++) printf "%-6s",$i; print ""}' file
>text 1148 1148 1148 1148 1148 1148 1148
>texttext 499 499 499 499 499 499 499
>texttexttext 3021 3021 3021 345 767 765 345

Totally oblivious to the fact that:

In article <fa514ab8-3c1b-4257...@googlegroups.com>,
Kenny McCormack wrote:

>I have a file thus:
>
>
>text 1148 1148 1148 1148 1148 1148 1148
>texttext 499 499 499 499 499 499 499
>texttexttext 3021 3021 3021 345 767 765 345
>
>
>and I want to get to:
>
>text 1148 1148 1148 1148 1148 1148 1148
>texttext 499 499 499 499 499 499 499
>texttexttext 3021 3021 3021 345 767 765 345

This is your AWK script:

{
printf("%-16s",$1)
for (i=2; i<=NF; i++)

Paul D

unread,
Oct 20, 2017, 2:42:00 PM10/20/17
to
Both perfect ! - thanks so much :)

Barry Margolin

unread,
Oct 22, 2017, 12:23:03 AM10/22/17
to
In article <ef6e824b-2545-4c5b...@googlegroups.com>,
Paul D <spectre....@gmail.com> wrote:

> Both perfect ! - thanks so much :)

Which part of the non-cryptic solution was "tricky"? It's just a direct
translation from English to awk. Use %-16s for the 6-character field,
and %-6s for the 6-character fields? Maybe you didn't know how to loop
over the remaining fields?

The only way this could be tricky is if you just don't know some common
awk idioms.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Kenny McCormack

unread,
Oct 22, 2017, 2:16:35 AM10/22/17
to
In article <barmar-6CA80E....@reader.eternal-september.org>,
Barry Margolin <bar...@alum.mit.edu> wrote:
>In article <ef6e824b-2545-4c5b...@googlegroups.com>,
> Paul D <spectre....@gmail.com> wrote:
>
>> Both perfect ! - thanks so much :)
>
>Which part of the non-cryptic solution was "tricky"? It's just a direct
>translation from English to awk. Use %-16s for the 6-character field,
>and %-6s for the 6-character fields? Maybe you didn't know how to loop
>over the remaining fields?
>
>The only way this could be tricky is if you just don't know some common
>awk idioms.

That seems to be precisely the case. I can't speak for any of the other
languages mentioned in the original thread title, but awk is a language
that exists at many different levels:

1) As not really a language at all; just a "shell helper", like cut,
join, etc.
2) As a simple language with some very basic constructs, but nothing
fancy. At this level, I put it as at about the same level as sed
(which actually does have a programming language embedded in it,
though no sane person would ever use it).
3) As a full blown programming language with various operating system
hooks, including the abiltiy to write your own code in extension
libraries and such.

Most people (*) do not get past #1 in the above list. The ability to do
printf() style formatting (including the %-16s thing) is at level 2.

(*) Including, I would assume, OP.

--
The randomly chosen signature file that would have appeared here is more than 4
lines long. As such, it violates one or more Usenet RFCs. In order to remain
in compliance with said RFCs, the actual sig can be found at the following URL:
http://user.xmission.com/~gazelle/Sigs/God
0 new messages