Delimited File to 2D Array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delimited File to 2D Array
# 1  
Old 07-12-2012
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 09:32 AM.. Reason: code tags
# 2  
Old 07-12-2012
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
This User Gave Thanks to rbatte1 For This Post:
# 3  
Old 07-12-2012
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

This User Gave Thanks to complex.invoke For This Post:
# 4  
Old 07-12-2012
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
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 04:32 PM..
# 5  
Old 07-12-2012
Code:
}' RS=";" ${1}

The font in red defines the Record Separator
I'm not familiar with ksh,sorry
This User Gave Thanks to complex.invoke For This Post:
# 6  
Old 07-12-2012
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.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need to convert a pipe delimited text file to tab delimited

Hi, I have a rquirement in unix as below . I have a text file with me seperated by | symbol and i need to generate a excel file through unix commands/script so that each value will go to each column. ex: Input Text file: 1|A|apple 2|B|bottle excel file to be generated as output as... (9 Replies)
Discussion started by: raja kakitapall
9 Replies

2. Shell Programming and Scripting

How to make tab delimited file to space delimited?

Hi How to make tab delimited file to space delimited? in put file: ABC kgy jkh ghj ash kjl o/p file: ABC kgy jkh ghj ash kjl Use code tags, thanks. (1 Reply)
Discussion started by: jagdishrout
1 Replies

3. Shell Programming and Scripting

awk read one delimited file, search another delimited file

Hello folks, I have another doozy. I have two files. The first file has four fields in it. These four fields map to different locations in my second file. What I want to do is read the master file (file 2 - 23 fields) and compare each line against each record in file 1. If I get a match in all four... (4 Replies)
Discussion started by: dagamier
4 Replies

4. Shell Programming and Scripting

Help with converting Pipe delimited file to Tab Delimited

I have a file which was pipe delimited, I need to make it tab delimited. I tried with sed but no use cat file | sed 's/|//t/g' The above command substituted "/t" not tab in the place of pipe. Sample file: abc|123|2012-01-30|2012-04-28|xyz have to convert to: abc 123... (6 Replies)
Discussion started by: karumudi7
6 Replies

5. Shell Programming and Scripting

How to convert a space delimited file into a pipe delimited file using shellscript?

Hi All, I have space delimited file similar to the one as shown below.. I need to convert it as a pipe delimited, the values inside the pipe delimited file should be as highlighted... AA ATIU2345098809 009697 005374 BB ATIU2345097809 005445 006518 CC ATIU9685098809 003215 003571 DD... (7 Replies)
Discussion started by: nithins007
7 Replies

6. Shell Programming and Scripting

how to read delimited text into array

I am trying to parse a string using delimited char into array. BNAME=B10,B20,B30 B10.Q=X B20.Q=Y B30.Q=Z I need to parsethe BNAME into array, then i will loop through array to execute command using these variables. like: for i in $array do qload array array.Q # execute command: qload B10... (3 Replies)
Discussion started by: adars1
3 Replies

7. Shell Programming and Scripting

convert a pipe delimited file to a':" delimited file

i have a file whose data is like this:: osr_pe_assign|-120|wg000d@att.com|4| osr_evt|-21|wg000d@att.com|4| pe_avail|-21|wg000d@att.com|4| osr_svt|-11|wg000d@att.com|4| pe_mop|-13|wg000d@att.com|4| instar_ready|-35|wg000d@att.com|4| nsdnet_ready|-90|wg000d@att.com|4|... (6 Replies)
Discussion started by: priyanka3006
6 Replies

8. UNIX for Dummies Questions & Answers

Converting Space delimited file to Tab delimited file

Hi all, I have a file with single white space delimited values, I want to convert them to a tab delimited file. I tried sed, tr ... but nothing is working. Thanks, Rajeevan D (16 Replies)
Discussion started by: jeevs81
16 Replies

9. UNIX for Dummies Questions & Answers

Loading a comma Delimited file into an Array

Hello, I have been stuck on this aspect of loading a comma delimited file into an array. I thought i had the syntax right, but my commands are not working the way I want them to. Basically my cut command is splitting the file up by spaces and commas. I want the cut command to ignore white spaces.... (2 Replies)
Discussion started by: grandtheftander
2 Replies

10. Shell Programming and Scripting

Converting Tab delimited file to Comma delimited file in Unix

Hi, Can anyone let me know on how to convert a Tab delimited file to Comma delimited file in Unix Thanks!! (22 Replies)
Discussion started by: charan81
22 Replies
Login or Register to Ask a Question