[TriLUG] Need some help parsing a file

Dewey Hylton plug at hyltown.com
Mon Dec 30 11:26:15 EST 2013


> From: "Peter Neilson" <neilson at windstream.net>
> To: "Triangle Linux Users Group General Discussion" <trilug at trilug.org>, "John Vaughters" <jvaughters04 at yahoo.com>
> Sent: Monday, December 30, 2013 10:45:39 AM
> Subject: Re: [TriLUG] Need some help parsing a file
> 
> On Mon, 30 Dec 2013 10:08:25 -0500, John Vaughters
> <jvaughters04 at yahoo.com> wrote:
> 
> > Brian,
> >
> > The awk actually worked as shown. It is more friendly when figuring
> > out
> > fields than cut, but still I use cut because it is easy to
> > remember.
> >
> > echo '11/09/2013  11:49 AM        7,887,098 this is filename 1.txt'
> > |
> > awk '{print $8}'
> 
> Hmmm. Why then do I get this:
> 
> $ echo '11/09/2013  11:49 AM        7,887,098 this is filename 1.txt'
> |
> awk '{print $8}'
> 1.txt
> $
> 
> The correct string would be:
> this is filename 1.txt

... but that is a string, not a "field" as seen by awk, which by default uses whitespace as field boundaries. this might give you what you want, but is clearly a bit ugly - and not very portable due to the fact that half the file names may have MORE fields ...

echo '11/09/2013  11:49 AM        7,887,098 this is filename 1.txt' \
  awk '{print $5 " " $6 " " $7 " " $8}'

if the start of the filename field is at a known location, and it is known that the filename is the last string on each line, cut is the way to go ...


More information about the TriLUG mailing list