K.S. Bhaskar via plug on 10 Sep 2020 10:21:43 -0700


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

Re: [PLUG] Awk *language*: was Win Subsystem for Linux2 broken


Some doggerel I remember from (I think) the early 1980s:

awk, awk, the language so cool
The more you awk, the better your tool!

There was also a two-panel cartoon in Datamation. The first labelled “The Way We Were” showed a bunch of cavemen sitting around rubbing sticks to start a fire saying things like “awk,” “grep” and “mkdir”. The second labelled “The Way We Are” showed a bunch of computer programmers in ties sitting at terminals (it was the early 1980s!) saying “awk,” “grep” and “mkdir.”

Regards
– Bhaskar

On Thu, Sep 10, 2020 at 1:09 PM Walt Mankowski via plug <plug@lists.phillylinux.org> wrote:
On Thu, Sep 10, 2020 at 12:10:33PM -0400, Rich Freeman via plug wrote:
> On Thu, Sep 10, 2020 at 12:01 PM Walt Mankowski via plug
> <plug@lists.phillylinux.org> wrote:
> >
> > I used to still use awk for splitting files since the syntax was a bit
> > simpler than Perl, but cut(1) does the simple cases even more simply,
> > and for anything else I use `perl -a`.
>
> Main thing I do with awk is use it to pull one field out of a record.
>
> Stuff like:
> zfs list -t snapshot -r offline1/storage | awk '{ print $1}' | tail -n
> +2 | xargs -n 1 zfs destroy
>
> This pulls up a list of snapshots, grabs just the first field of each,
> strips the headers, then deletes each one.
>
> Is there an easier way to pull one field (not necessarily the first)
> out of a list?

For simple things I use cut(1). To pull out the first field you use

cut -f1

cut defaults to splitting on tabs. If you wanted to split on, say,
commas, you'd say

cut -d, -f2

to pull the second field from a CSV file. I just ran that command in a
pipeline this morning!

Unfortunately cut doesn't have a way to split on regular expressions,
so it doesn't work when there are a variable number of spaces between
fields. For your example I'd probably use perl instead:

perl -aE 'say $F[0]'

Or maybe it's $F[1]; I don't remember if $1 is the first or second
field in awk.
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug