For Programmers: Free Programming Magazines  


Home > Archive > AWK > November 2005 > Extract several fields from duplicate lines









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 Extract several fields from duplicate lines
jonhardt@yahoo.com

2005-11-29, 6:56 pm

Probably an easy one for the pros, but I can't figure it out...

Basically I have a file that I want to extract the memory size in MB
from...problem is this line occurs several times in the file.

The line is question reads:

Memory Size: 1024 MB

I want to assign "1024 MB" to a variable for use later in my script

But since it occurs several times in the file, my awk statement:

MEM_SIZE=`awk '/Memory Size:/ { print $3 " " $4 }' $INPUT_FILE`

produces this:

1024 MB1024 MB1024 MB1024 MB

Basically I just want to find the first occurrence of the pattern
"Memory Size:" and assign that to my variable.

Is that possible in awk or shoud I use some sort of other utility? TIA
for all responses!

Janis Papanagnou

2005-11-29, 6:56 pm

jonhardt@yahoo.com wrote:
> Probably an easy one for the pros, but I can't figure it out...
>
> Basically I have a file that I want to extract the memory size in MB
> from...problem is this line occurs several times in the file.
>
> The line is question reads:
>
> Memory Size: 1024 MB
>
> I want to assign "1024 MB" to a variable for use later in my script
>
> But since it occurs several times in the file, my awk statement:
>
> MEM_SIZE=`awk '/Memory Size:/ { print $3 " " $4 }' $INPUT_FILE`
>
> produces this:
>
> 1024 MB1024 MB1024 MB1024 MB
>
> Basically I just want to find the first occurrence of the pattern
> "Memory Size:" and assign that to my variable.
>
> Is that possible in awk or shoud I use some sort of other utility? TIA
> for all responses!
>


MEM_SIZE=`awk '/Memory Size:/ { print $3 " " $4 ; exit }' $INPUT_FILE`


Janis
jonhardt@yahoo.com

2005-11-29, 6:56 pm

Wow can't believe it was that easy - thanks for the speedy reply!

Sponsored Links







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

Copyright 2008 codecomments.com