Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Awk Split Question
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  14 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Beery  
View profile  
 More options Sep 14 2012, 11:59 pm
Newsgroups: comp.unix.shell
From: Beery <mentorbe...@gmail.com>
Date: Fri, 14 Sep 2012 20:59:32 -0700 (PDT)
Local: Fri, Sep 14 2012 11:59 pm
Subject: Awk Split Question
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ed Morton  
View profile  
 More options Sep 15 2012, 12:16 am
Newsgroups: comp.unix.shell
From: Ed Morton <mortons...@gmail.com>
Date: Fri, 14 Sep 2012 23:16:55 -0500
Local: Sat, Sep 15 2012 12:16 am
Subject: Re: Awk Split Question
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris F.A. Johnson  
View profile  
 More options Sep 15 2012, 12:17 am
Newsgroups: comp.unix.shell
From: "Chris F.A. Johnson" <cfajohn...@gmail.com>
Date: Sat, 15 Sep 2012 00:12:21 -0400
Local: Sat, Sep 15 2012 12:12 am
Subject: Re: Awk Split Question

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]
   }
'

--
   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)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Beery  
View profile  
 More options Sep 15 2012, 12:21 am
Newsgroups: comp.unix.shell
From: Beery <mentorbe...@gmail.com>
Date: Fri, 14 Sep 2012 21:21:45 -0700 (PDT)
Local: Sat, Sep 15 2012 12:21 am
Subject: Re: Awk Split Question

That worked. Thanks!!!

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Beery  
View profile  
 More options Sep 15 2012, 12:25 am
Newsgroups: comp.unix.shell
From: Beery <mentorbe...@gmail.com>
Date: Fri, 14 Sep 2012 21:25:23 -0700 (PDT)
Local: Sat, Sep 15 2012 12:25 am
Subject: Re: Awk Split Question

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?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris F.A. Johnson  
View profile  
 More options Sep 15 2012, 12:47 am
Newsgroups: comp.unix.shell
From: "Chris F.A. Johnson" <cfajohn...@gmail.com>
Date: Sat, 15 Sep 2012 00:46:26 -0400
Local: Sat, Sep 15 2012 12:46 am
Subject: Re: Awk Split Question

-F'\\\\n'

--
   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)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kaz Kylheku  
View profile  
 More options Sep 15 2012, 1:23 am
Newsgroups: comp.unix.shell
From: Kaz Kylheku <k...@kylheku.com>
Date: Sat, 15 Sep 2012 05:23:42 +0000 (UTC)
Local: Sat, Sep 15 2012 1:23 am
Subject: Re: Awk Split Question
On 2012-09-15, Beery <mentorbe...@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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris F.A. Johnson  
View profile  
 More options Sep 15 2012, 3:47 am
Newsgroups: comp.unix.shell
From: "Chris F.A. Johnson" <cfajohn...@gmail.com>
Date: Sat, 15 Sep 2012 03:38:24 -0400
Local: Sat, Sep 15 2012 3:38 am
Subject: Re: Awk Split Question
On 2012-09-15, Kaz Kylheku wrote:

> On 2012-09-15, Beery <mentorbe...@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?

--
   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)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mirko K.  
View profile  
 More options Sep 15 2012, 6:00 am
Newsgroups: comp.unix.shell
From: "Mirko K." <mirkok.li...@googlemail.com>
Date: Sat, 15 Sep 2012 12:00:54 +0200
Local: Sat, Sep 15 2012 6:00 am
Subject: Re: Awk Split Question
On 15.09.2012 09:38, Chris F.A. Johnson wrote:

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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Janis Papanagnou  
View profile  
 More options Sep 15 2012, 8:04 am
Newsgroups: comp.unix.shell
From: Janis Papanagnou <janis_papanag...@hotmail.com>
Date: Sat, 15 Sep 2012 14:04:43 +0200
Local: Sat, Sep 15 2012 8:04 am
Subject: Re: Awk Split Question
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris F.A. Johnson  
View profile  
 More options Sep 15 2012, 11:02 am
Newsgroups: comp.unix.shell
From: "Chris F.A. Johnson" <cfajohn...@gmail.com>
Date: Sat, 15 Sep 2012 11:01:43 -0400
Local: Sat, Sep 15 2012 11:01 am
Subject: Re: Awk Split Question
On 2012-09-15, Mirko K. wrote:

   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...

--
   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)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kaz Kylheku  
View profile  
 More options Sep 15 2012, 2:24 pm
Newsgroups: comp.unix.shell
From: Kaz Kylheku <k...@kylheku.com>
Date: Sat, 15 Sep 2012 18:24:30 +0000 (UTC)
Local: Sat, Sep 15 2012 2:24 pm
Subject: Re: Awk Split Question
On 2012-09-15, Chris F.A. Johnson <cfajohn...@gmail.com> wrote:

> On 2012-09-15, Kaz Kylheku wrote:
>> On 2012-09-15, Beery <mentorbe...@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 Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mirko K.  
View profile  
 More options Sep 15 2012, 2:49 pm
Newsgroups: comp.unix.shell
From: "Mirko K." <mirkok.li...@googlemail.com>
Date: Sat, 15 Sep 2012 20:49:50 +0200
Local: Sat, Sep 15 2012 2:49 pm
Subject: Re: Awk Split Question
On 15.09.2012 17:01, Chris F.A. Johnson wrote:

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 ...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris F.A. Johnson  
View profile  
 More options Sep 15 2012, 10:47 pm
Newsgroups: comp.unix.shell
From: "Chris F.A. Johnson" <cfajohn...@gmail.com>
Date: Sat, 15 Sep 2012 22:38:46 -0400
Local: Sat, Sep 15 2012 10:38 pm
Subject: Re: Awk Split Question
On 2012-09-15, Kaz Kylheku wrote:

> On 2012-09-15, Chris F.A. Johnson <cfajohn...@gmail.com> wrote:
>> On 2012-09-15, Kaz Kylheku wrote:
>>> On 2012-09-15, Beery <mentorbe...@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.

--
   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)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »