|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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:
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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 | ||
|
![]() |
| Tags |
| array, awk |
| Thread Tools | Search this Thread |
| 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 |
|
|