The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Array inside an array manas_ranjan UNIX for Advanced & Expert Users 5 06-10-2008 02:25 PM
Access value outside awk or split value of array jason.bean UNIX for Dummies Questions & Answers 1 11-26-2007 04:33 PM
How to get array to not split at spaces? jjinno Shell Programming and Scripting 1 07-20-2007 12:06 AM
split to array in perl jaganadh Shell Programming and Scripting 3 07-06-2007 05:29 AM
looping a array inside inside ssh is not working, pls help reldb Shell Programming and Scripting 5 07-07-2006 10:32 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-06-2008
dcfargo dcfargo is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 45
split and making an array inside another array

I want to run an awk split on a value that has been pushed through an array and I was wondering what the syntax should be??


e.g. running time strings through an array and trying to examine just minutes:

12:25:30
10:15:13
08:55:23
Code:
awk '                    
      NR==FNR{
             a[NR]=$0
             next
             }
             {
              for(i=1;i<NR;i++)
split($0,a[i],":"); print a[i][2]
}'
I guess I'm wondering how to format for the subsection?? (a[i])[2] ???



edit also will this substring from the split come out of the slit function as a number? e.g. can I do math on it or will it be a 'word'? If its a word can I make it a number?

Last edited by jim mcnamara; 08-06-2008 at 10:02 AM.. Reason: code tags
  #2 (permalink)  
Old 08-06-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
try this: setting a field separator.
Code:
awk -F: '{ a[FNR]=$1; b[FNR]=$2; c[FNR]=$3 }
            END{
            for(i in a) { hour+=a[i]}
            for(i in b) { min+=b[i]}
            for(i in c) { sec+=c[i]}
            printf "hours=%d min=%d sec=%d\", hour, min,sec)
            } ' inputfile
And yes, you can perform arithmetic as you see above.
  #3 (permalink)  
Old 08-06-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
What exactly you want to do with the minutes?

Code:
% print '12:25:30
10:15:13
08:55:23'|awk 'END{for(m in _){split(m, t, ":");print t[2]+1}}{_[$0]}'
56
26
16
Or just:

Code:
% print '12:25:30
10:15:13
08:55:23'|awk -F: '$0=$2+1'
26
16
56
Awk does not support multidimensional arrays, you can simulate them.

Last edited by radoulov; 08-06-2008 at 10:13 AM..
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:16 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0