For Programmers: Free Programming Magazines  


Home > Archive > AWK > May 2006 > Help is parsing line









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Help is parsing line
ram

2006-05-22, 7:57 am

Hi All,

I have a line in the following format.

"first second [third field within [\]] fourth

I want to parse this line and store the fields in individual variable.

(i.e) Result should be:
var1 = first
var2 = second
var3 = third field within \]
var4 = fourth

Note: Third field is within square bracket and ] within this field will
be a escaped one (i.e) \].

Request you to help me.

Thanks.

Kenny McCormack

2006-05-22, 7:57 am

In article <1148303334.700589.45610@g10g2000cwb.googlegroups.com>,
ram <ramsankar@gmail.com> wrote:
>Hi All,
>
>I have a line in the following format.
>
>"first second [third field within [\]] fourth
>
>I want to parse this line and store the fields in individual variable.
>
>(i.e) Result should be:
>var1 = first
>var2 = second
>var3 = third field within \]
>var4 = fourth
>
>Note: Third field is within square bracket and ] within this field will
>be a escaped one (i.e) \].
>
>Request you to help me.
>
>Thanks.
>


man awk

(should answer all your questions)

John Savage

2006-05-27, 3:57 am

"ram" <ramsankar@gmail.com> writes:
>I have a line in the following format.
>
>"first second [third field within [\]] fourth
>
>I want to parse this line and store the fields in individual variable.
>
>(i.e) Result should be:
>var1 = first
>var2 = second
>var3 = third field within \]
>var4 = fourth
>
>Note: Third field is within square bracket and ] within this field will
>be a escaped one (i.e) \].


This should get you started:

$cat datafile
nextfirst nextsecond [more in the \[this field\] too ] fourthfield

$mawk '{th=$0;sub(/^[^[]*\[ */,"",th);sub(/ *][^[\]]*$/,"",th);print $1;
print $2;print th; print $NF}' data

nextfirst
nextsecond
more in the \[this field\] too
fourthfield

While I used mawk my statements are just standard awk.
--
John Savage (my news address is not valid for email)

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com