[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Vá: awk FIELDWIDTHS howto?



David Jardine wrote:
On Wed, Jan 09, 2008 at 08:48:38PM +0100, Paul Csanyi wrote:
2008/1/9, Sergio Cuéllar Valdés <herrsergio@gmail.com>:
2008/1/9, Paul Csanyi <csanyipal@gmail.com>:
I try to use awk to print second field from a text file
but awk prints a part of 1. field as a 2. field. Why?

aptitude search ~i | awk '{ NF = "2" } { FIELDWIDTHS = " 4 32 " }
{print $2}' > foltelepitett_debian_csomagok

less foltelepitett_debian_csomagok

..
bsdmainutils
bsdutils
busybox
A
ca-certificates
checksecurity
A record is like these:

i busybox - Tiny utilities for small and embedded syst
i A bzip2 - high-quality block-sorting file compressor
    |
1234123456789etc.
1.  |2.

I don't know aptitude, but that output can hardly constitute a "record", since "busybox" is the 2nd field whereas "bzip2" is the 3rd because of that "A".

I try to get only the package name from the 2. field.

Which is what you've got: "A" is the second field.

FIELDWIDTHS = " 4 32 "

I don't know awk, either (What the hell am I doing in this thread ? :{) and I couldn't find FIELDWIDTH in the awk manpage, but perhaps it doesn't do what you think it does. Perhaps it just limits the length of a longer field for output.

My man page says:

If the FIELDWIDTHS variable is set to a space separated list of numbers, each field is expected to have fixed width, and gawk splits up the record using the specified widths. The value of FS is ignored.


The given width for 1. field (4) should be right, but isn't.
Awk give me the letter "A" as a 2. field. Why?

Because it is the second field, but since the lines returned by that aptitude command are inconsistent in format I'm afraid I can't think of anything you can do about it. If you know what sort of things might appear where that "A" appears, you could maybe insert something like

sed -r 's/ [A-Z] //g'

into your command.

something like this:   aptitude search ~i | cut -d" " -f2
does this help you ?
No.

--
Regards, Paul Csanyi
http://www.freewebs.com/csanyi-pal/index.htm


The bottom line problem is that the aptitude output has a fixed formatting, but the columns it "prints" are variable. So, given the fixed formatting, any command that honors that would work:

  aptitude search ~i | cut -c5-32 # NOTE! used -c, not -d" " -f2
  aptitude search ~i |
    sed 's/^....\(................................\).*$/\1/'

That sed command remembers character 5 through 32 and replaces the whole line with the remembered stuff. I'm not a 'sed' programmer, though, so I'm sure there's a simpler way to specify repetition, I just didn't bother to look it up.

I don't know if the following observation is a bug or a feature, but you apparently need to specify the number of field widths you want, *plus one*. Also, use the BEGIN keyword on code that needs to be "globally" available or done just once. The original code will reassign FIELDWIDTHS for every line of output. This code works as desired:

  aptitude search ~i | awk 'BEGIN{FIELDWIDTHS = "4 32 1"};{print $2}'

--
Bob McGowan

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature


Reply to: