Walt Mankowski via plug on 10 Sep 2020 10:09:32 -0700


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

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


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.

Attachment: signature.asc
Description: PGP signature

___________________________________________________________________________
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