Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Awk Split Question

78 views
Skip to first unread message

Beery

unread,
Sep 14, 2012, 11:59:32 PM9/14/12
to
I have a file containing rows like

MemTotal: 32959876 kB\nMemFree: 309820 kB\nBuffers: 563784 kB...

I want to split the elements into an array with the "\n" being the separator. For some reason the following

awk '{if($1~/MemTotal/){split($0,a,"kB");print a[1]}}'

prints the whole row and fails to detect the separator. Any suggestion on how to do this?

Thanks.

Ed Morton

unread,
Sep 15, 2012, 12:16:55 AM9/15/12
to
On 9/14/2012 10:59 PM, Beery wrote:
> I have a file containing rows like
>
> MemTotal: 32959876 kB\nMemFree: 309820 kB\nBuffers: 563784 kB...
>
> I want to split the elements into an array with the "\n" being the separator. For some reason the following
>
> awk '{if($1~/MemTotal/){split($0,a,"kB");print a[1]}}'

You say you want to separate on "\n" but then you call split() with "kB" as the
separator instead.

> prints the whole row and fails to detect the separator. Any suggestion on how to do this?

You are using a valid syntax. Create a SMALL file that demonstrates the problem
and post a copy/paste of that file plus the command you're running plus the output.

Ed.

Chris F.A. Johnson

unread,
Sep 15, 2012, 12:12:21 AM9/15/12
to
awk '
{
n = split($0,a,/\\n/)
print a[1]
print a[2]
print a[3]
}
'


--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)

Beery

unread,
Sep 15, 2012, 12:21:45 AM9/15/12
to cfajo...@gmail.com
That worked. Thanks!!!

Beery

unread,
Sep 15, 2012, 12:25:23 AM9/15/12
to cfajo...@gmail.com
Just out of curiosity, if I wanted to define the separator using the -F option like

awk -F"\n" ....

I tried with the -F/\\n/ and that doesn't work.

How would I do that?

Chris F.A. Johnson

unread,
Sep 15, 2012, 12:46:26 AM9/15/12
to
On 2012-09-15, Beery wrote:
> On Friday, September 14, 2012 11:21:45 PM UTC-5, Beery wrote:
>> On Friday, September 14, 2012 11:17:55 PM UTC-5, Chris F.A. Johnson wrote:
>>
>> > On 2012-09-15, Beery wrote:
>>
>> > > I have a file containing rows like
>>
>> > > MemTotal: 32959876 kB\nMemFree: 309820 kB\nBuffers: 563784 kB...
>>
>> > > I want to split the elements into an array with the "\n" being the separator. For some reason the following
>>
>> > > awk '{if($1~/MemTotal/){split($0,a,"kB");print a[1]}}'
>>
>> > > prints the whole row and fails to detect the separator. Any suggestion on how to do this?
>>
>> > awk '
>> > {
>> > n = split($0,a,/\\n/)
>> > print a[1]
>> > print a[2]
>> > print a[3]
>> > }
>> > '
>>
>> That worked. Thanks!!!
>
> Just out of curiosity, if I wanted to define the separator using the -F option like
>
> awk -F"\n" ....
>
> I tried with the -F/\\n/ and that doesn't work.
>
> How would I do that?

-F'\\\\n'

Kaz Kylheku

unread,
Sep 15, 2012, 1:23:42 AM9/15/12
to
On 2012-09-15, Beery <mento...@gmail.com> wrote:
> I have a file containing rows like
>
> MemTotal: 32959876 kB\nMemFree: 309820 kB\nBuffers: 563784 kB...

LOL! See the "Simple Query" here:

http://rosettacode.org/wiki/Category:TXR

Note that that code will work even if the fields of /proc/meminfo are
reordered, and interspersed with fields that you don't need.

Chris F.A. Johnson

unread,
Sep 15, 2012, 3:38:24 AM9/15/12
to
On 2012-09-15, Kaz Kylheku wrote:
> On 2012-09-15, Beery <mento...@gmail.com> wrote:
>> I have a file containing rows like
>>
>> MemTotal: 32959876 kB\nMemFree: 309820 kB\nBuffers: 563784 kB...
>
> LOL! See the "Simple Query" here:
>
> http://rosettacode.org/wiki/Category:TXR

What? Yet another unnecessary language?

> Note that that code will work even if the fields of /proc/meminfo are
> reordered, and interspersed with fields that you don't need.

What does /proc/meminfo have to do with the question?

Mirko K.

unread,
Sep 15, 2012, 6:00:54 AM9/15/12
to
On 15.09.2012 09:38, Chris F.A. Johnson wrote:
> On 2012-09-15, Kaz Kylheku wrote:
>> On 2012-09-15, Beery <mento...@gmail.com> wrote:
>>> I have a file containing rows like
>>>
>>> MemTotal: 32959876 kB\nMemFree: 309820 kB\nBuffers: 563784 kB...
>>
>> LOL! See the "Simple Query" here:
>>
>> http://rosettacode.org/wiki/Category:TXR
>
> What? Yet another unnecessary language?
>
>> Note that that code will work even if the fields of /proc/meminfo are
>> reordered, and interspersed with fields that you don't need.
>
> What does /proc/meminfo have to do with the question?

$ head -3 /proc/meminfo
MemTotal: 3096544 kB
MemFree: 659352 kB
Buffers: 413220 kB

Janis Papanagnou

unread,
Sep 15, 2012, 8:04:43 AM9/15/12
to
On 15.09.2012 06:25, Beery wrote:

[big snip]

[You should really fix your newsreader issues!]

>
> Just out of curiosity, if I wanted to define the separator using the -F option like
>
> awk -F"\n" ....
>
> I tried with the -F/\\n/ and that doesn't work.
>
> How would I do that?

With contemporary shells I prefer using the ANSI-strings

awk -F $'\n' ...

For self-contained awk programs, and programs that should work on
different platforms[*] I suggest to embed the FS definition

awk '
BEGIN { FS="\n" }
...rest of the awk program...
'

Janis

[*] So you can put the lines between the single quotes in a file,
say xyz.awk, and call it on any platform uniquely as

awk -f xyz.awk

without specifying the FS on command line.

Chris F.A. Johnson

unread,
Sep 15, 2012, 11:01:43 AM9/15/12
to
Which is not what the OP asked about. The string is question has literal
backslash,n pairs, not newlines:

MemTotal: 32959876 kB\nMemFree: 309820 kB\nBuffers: 563784 kB...


Kaz Kylheku

unread,
Sep 15, 2012, 2:24:30 PM9/15/12
to
On 2012-09-15, Chris F.A. Johnson <cfajo...@gmail.com> wrote:
> On 2012-09-15, Kaz Kylheku wrote:
>> On 2012-09-15, Beery <mento...@gmail.com> wrote:
>>> I have a file containing rows like
>>>
>>> MemTotal: 32959876 kB\nMemFree: 309820 kB\nBuffers: 563784 kB...
>>
>> LOL! See the "Simple Query" here:
>>
>> http://rosettacode.org/wiki/Category:TXR
>
> What? Yet another unnecessary language?

Whoa! This coming from a master of the unnecessary.

Mirko K.

unread,
Sep 15, 2012, 2:49:50 PM9/15/12
to
On 15.09.2012 17:01, Chris F.A. Johnson wrote:
> On 2012-09-15, Mirko K. wrote:
>> On 15.09.2012 09:38, Chris F.A. Johnson wrote:
>>> On 2012-09-15, Kaz Kylheku wrote:
>>>> On 2012-09-15, Beery <mento...@gmail.com> wrote:
>>>>> I have a file containing rows like
>>>>>
>>>>> MemTotal: 32959876 kB\nMemFree: 309820 kB\nBuffers: 563784 kB...
>>>>
>>>> LOL! See the "Simple Query" here:
>>>>
>>>> http://rosettacode.org/wiki/Category:TXR
>>>
>>> What? Yet another unnecessary language?
>>>
>>>> Note that that code will work even if the fields of /proc/meminfo are
>>>> reordered, and interspersed with fields that you don't need.
>>>
>>> What does /proc/meminfo have to do with the question?
>>
>> $ head -3 /proc/meminfo
>> MemTotal: 3096544 kB
>> MemFree: 659352 kB
>> Buffers: 413220 kB
>
> Which is not what the OP asked about. The string is question has literal
> backslash,n pairs, not newlines:

Yes, but the OP's data is most probably extracted from
/proc/meminfo. And that's what /proc/meminfo has to do with it.
Nothing more, nothing less.

> MemTotal: 32959876 kB\nMemFree: 309820 kB\nBuffers: 563784 kB...

It's well possible, that the OP is generating the data in a wrong
(or just different than needed) way, like:

$ python -c 'm=open("/proc/meminfo").read();print repr(m)'
'MemTotal: 3096544 kB\nMemFree: 221028 kB\nBuffers:
366880 kB ...

Chris F.A. Johnson

unread,
Sep 15, 2012, 10:38:46 PM9/15/12
to
On 2012-09-15, Kaz Kylheku wrote:
> On 2012-09-15, Chris F.A. Johnson <cfajo...@gmail.com> wrote:
>> On 2012-09-15, Kaz Kylheku wrote:
>>> On 2012-09-15, Beery <mento...@gmail.com> wrote:
>>>> I have a file containing rows like
>>>>
>>>> MemTotal: 32959876 kB\nMemFree: 309820 kB\nBuffers: 563784 kB...
>>>
>>> LOL! See the "Simple Query" here:
>>>
>>> http://rosettacode.org/wiki/Category:TXR
>>
>> What? Yet another unnecessary language?
>
> Whoa! This coming from a master of the unnecessary.

You must be thinking of someone else.
0 new messages