Delimited File to 2D Array | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Delimited File to 2D Array

Shell Programming and Scripting


Tags
array, awk

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-12-2012
vinay4889 vinay4889 is offline
Registered User
 
Join Date: May 2012
Last Activity: 1 November 2013, 4:53 AM EDT
Posts: 20
Thanks: 26
Thanked 0 Times in 0 Posts
CPU & Memory Delimited File to 2D Array

Hello,

I need help in fetching data from a delimited file , into a 2D array.

Sample Input File:

"|" as delimiter


Code:
A|123|446pr;
B|46|hello89
krp;
C|78|ystp90
67;
D|ga|456;

Please be advised that ";" is the line separator (not "\n"). Could you please write an awk script to fetch this into a 2D array.

Output: array[0,1]=123

Thank You !

Last edited by Scrutinizer; 07-12-2012 at 08:32 AM.. Reason: code tags
Sponsored Links
    #2  
Old 07-12-2012
rbatte1 rbatte1 is offline Forum Staff  
Moderator
 
Join Date: Jun 2007
Last Activity: 22 July 2014, 4:22 PM EDT
Location: Lancashire, UK
Posts: 1,600
Thanks: 508
Thanked 272 Times in 243 Posts
What are you calling the awk with? Many shells only support single diminsion arrarys, i.e. a list. If you have few enough items, then you might get away with using a formula to convert your 2D plan into a single value that you can repeat the function with the function with to recall the data later, but there are limits to the number of elements you can have, e.g. ksh will support up to 1024 (depending how you interpret the rules and how you use it)

Could you tell us a bit more about what you have tried, what the purspose is and data volumes. It might suggest something or steer someone with an idea.


Robin
Liverpool/Blackburn
UK
The Following User Says Thank You to rbatte1 For This Useful Post:
vinay4889 (07-12-2012)
Sponsored Links
    #3  
Old 07-12-2012
complex.invoke's Avatar
complex.invoke complex.invoke is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 10 May 2014, 9:20 PM EDT
Location: BeiJing China
Posts: 236
Thanks: 6
Thanked 23 Times in 8 Posts

Code:
[root@node3 ~]# cat infile 
A|123|446pr;
B|46|hello89
krp;
C|78|ystp90
67;
D|ga|456;
[root@node3 ~]# cat 2D_awk_array.sh 
awk -F\| ' 
    {
       gsub(/\n/,"",$0) 
       if (max_nf<NF) 
           max_nf=NF 
       max_nr=NR 
       for(x=1; x<=NF; ++x) 
           vector[NR-1,x-1]=$x 
    } 
END { 
       for(row=0; row<max_nr; ++row) { 
           for(col=0; col<max_nf; ++col) 
               if(length(vector[row,col]) != 0)
                  print "vector["row","col"]="vector[row,col]
       } 
    }' RS=";" ${1} 
[root@node3 ~]# bash 2D_awk_array.sh infile 
vector[0,0]=A
vector[0,1]=123
vector[0,2]=446pr
vector[1,0]=B
vector[1,1]=46
vector[1,2]=hello89krp
vector[2,0]=C
vector[2,1]=78
vector[2,2]=ystp9067
vector[3,0]=D
vector[3,1]=ga
vector[3,2]=456

The Following User Says Thank You to complex.invoke For This Useful Post:
vinay4889 (07-12-2012)
    #4  
Old 07-12-2012
vinay4889 vinay4889 is offline
Registered User
 
Join Date: May 2012
Last Activity: 1 November 2013, 4:53 AM EDT
Posts: 20
Thanks: 26
Thanked 0 Times in 0 Posts
Hi Robin,

I tried below to do the same.



Code:
awk 'BEGIN{
FS="|";
RS=";";
}
{
print "We are here after begin";
--Here I want to assigne values in 2 D array
--say array[i,j]=$2
}
END {
;
}'File


Please help me here to improve the same.

Thanks,


---------- Post updated at 09:45 AM ---------- Previous update was at 09:07 AM ----------

Quote:
Originally Posted by huaihaizi3 View Post
Code:
[root@node3 ~]# cat infile 
A|123|446pr;
B|46|hello89
krp;
C|78|ystp90
67;
D|ga|456;
[root@node3 ~]# cat 2D_awk_array.sh 
awk -F\| ' 
    {
       gsub(/\n/,"",$0) 
       if (max_nf<NF) 
           max_nf=NF 
       max_nr=NR 
       for(x=1; x<=NF; ++x) 
           vector[NR-1,x-1]=$x 
    } 
END { 
       for(row=0; row<max_nr; ++row) { 
           for(col=0; col<max_nf; ++col) 
               if(length(vector[row,col]) != 0)
                  print "vector["row","col"]="vector[row,col]
       } 
    }' RS=";" ${1} 
[root@node3 ~]# bash 2D_awk_array.sh infile 
vector[0,0]=A
vector[0,1]=123
vector[0,2]=446pr
vector[1,0]=B
vector[1,1]=46
vector[1,2]=hello89krp
vector[2,0]=C
vector[2,1]=78
vector[2,2]=ystp9067
vector[3,0]=D
vector[3,1]=ga
vector[3,2]=456

Thanks for the help.
Please help me understand your code.
1) I could not see where ";" line separator is defined.
2) Can I below line in starting of code:
awk 'BEGIN{
FS="|";
RS=";";
}

3) I am working on KSH , I suppose gsub might not work there.I see you are replacing "\n" with "". Will that be necessary even if I use this line RS=";";
It would be really helpful if you could guide me what are changes I should as KSH perspective.


Thanks,

Last edited by vinay4889; 07-25-2012 at 03:32 PM..
Sponsored Links
    #5  
Old 07-12-2012
complex.invoke's Avatar
complex.invoke complex.invoke is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 10 May 2014, 9:20 PM EDT
Location: BeiJing China
Posts: 236
Thanks: 6
Thanked 23 Times in 8 Posts

Code:
}' RS=";" ${1}

The font in red defines the Record Separator
I'm not familiar with ksh,sorry
The Following User Says Thank You to complex.invoke For This Useful Post:
vinay4889 (07-12-2012)
Sponsored Links
    #6  
Old 07-12-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 23 July 2014, 6:58 PM EDT
Location: Saskatchewan
Posts: 18,972
Thanks: 747
Thanked 3,137 Times in 2,949 Posts
Shell is shell, awk is awk, they are independent and unrelated. Using ksh has nothing to do with whether you get gsub.

On some systems, especially Solaris, you need to use nawk to get gsub.
The Following User Says Thank You to Corona688 For This Useful Post:
vinay4889 (07-25-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to convert a space delimited file into a pipe delimited file using shellscript? nithins007 Shell Programming and Scripting 7 09-25-2011 05:33 AM
how to read delimited text into array adars1 Shell Programming and Scripting 3 03-21-2010 10:32 AM
convert a pipe delimited file to a':" delimited file priyanka3006 Shell Programming and Scripting 6 05-26-2009 10:53 AM
Loading a comma Delimited file into an Array grandtheftander UNIX for Dummies Questions & Answers 2 07-26-2006 01:19 PM
Converting Tab delimited file to Comma delimited file in Unix charan81 Shell Programming and Scripting 22 01-20-2006 08:24 AM



All times are GMT -4. The time now is 08:17 PM.