Sponsored Content
Full Discussion: Delimited File to 2D Array
Top Forums Shell Programming and Scripting Delimited File to 2D Array Post 302670601 by complex.invoke on Thursday 12th of July 2012 09:02:22 AM
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:
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
ARRAY_REVERSE(3)							 1							  ARRAY_REVERSE(3)

array_reverse - Return an array with elements in reverse order

SYNOPSIS
array array_reverse (array $array, [bool $preserve_keys = false]) DESCRIPTION
Takes an input $array and returns a new array with the order of the elements reversed. PARAMETERS
o $array - The input array. o $preserve_keys - If set to TRUE numeric keys are preserved. Non-numeric keys are not affected by this setting and will always be preserved. RETURN VALUES
Returns the reversed array. EXAMPLES
Example #1 array_reverse(3) example <?php $input = array("php", 4.0, array("green", "red")); $reversed = array_reverse($input); $preserved = array_reverse($input, true); print_r($input); print_r($reversed); print_r($preserved); ?> The above example will output: Array ( [0] => php [1] => 4 [2] => Array ( [0] => green [1] => red ) ) Array ( [0] => Array ( [0] => green [1] => red ) [1] => 4 [2] => php ) Array ( [2] => Array ( [0] => green [1] => red ) [1] => 4 [0] => php ) SEE ALSO
array_flip(3). PHP Documentation Group ARRAY_REVERSE(3)
All times are GMT -4. The time now is 01:32 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy