[102410] in RedHat Linux List

home help back first fref pref prev next nref lref last post

RE: parsing text files

daemon@ATHENA.MIT.EDU (Rick L. Mantooth)
Wed Dec 2 21:46:53 1998

Date: Wed, 2 Dec 1998 20:47:59 -0600 (CST)
From: "Rick L. Mantooth" <rickdman@cyberramp.net>
To: Ed Lazor <elazor@hcs.state.or.us>
cc: redhat-list@redhat.com
In-Reply-To: <NCBBKAHLNJCALPEJDMAECEAJCEAA.elazor@hcs.state.or.us>
Resent-From: redhat-list@redhat.com
Reply-To: redhat-list@redhat.com

Ed,
Oh for the world to be easier...;)

File one:
abc	123	LineOne
def	456	LineTwo
ghi	789	LineThree

$ nawk '{print $1}' one
abc
def
ghi
(Ah, life is grand)

File two:
ab c	123	LineOne
de f	456	LineTwo
gh i	789	LineThree
  ^ is a SPACE

$ nawk 'BEGIN{FS="     "}{print $1}' < two
                ^^^^^ is a TAB
will give the results:
ab c
de f
gh i

Now for a little fun.
Assuming TAB is the Field Separator for the columns and words
with spaces between them belong in the same field. (ass_u_me)
(Above nawk would work here also)

File three:
a b c   123     Line One
de f    456     LineTwo
gh i    7 89    Line Three

$ sed -e 's/ /_/g' three | nawk '{print $1}' | sed -e 's/_/ /g'
a b c
de f
gh i

Let sed swap the SPACES to _ , hand it off to nawk and then let
sed swap _ back to SPACE.

Note:
Not *thoroughly tested*, just some quickies.

Rick

On Wed, 2 Dec 1998, Ed Lazor wrote:

> 
> You're right and you're wrong.  It turns out that the column of
> information I was actually pulling information from only had one word -
> so I lucked out *grin*.  This wouldn't have worked in the example I gave
> though if I were trying to pull the person's last and first name because
> they are separate words and would have been parsed into separate
> variables.  So out of curiosity, how would have you gone about
> separating the column if it had multiple words and the number of words
> varied? 
> 
--
Rick L. Mantooth	rickdman@cyberramp.net
http://www.cyberramp.net/~rickdman
I live with FEAR and sometimes she lets me go fishing!


-- 
  PLEASE read the Red Hat FAQ, Tips, Errata and the MAILING LIST ARCHIVES!
		http://www.redhat.com http://archive.redhat.com
         To unsubscribe: mail redhat-list-request@redhat.com with 
                       "unsubscribe" as the Subject.


home help back first fref pref prev next nref lref last post