Sponsored Content
Full Discussion: Read array from a file
Top Forums Shell Programming and Scripting Read array from a file Post 302356991 by durden_tyler on Monday 28th of September 2009 11:02:12 AM
Old 09-28-2009
One way to do it with Perl:

Code:
$
$ cat file1
#comment
 
k_array: 1 2 3 4 5
n_array: 7 8 9 0 11
$
$ perl -lne 'BEGIN{$x=0} chomp; if(/.*: (.*)/){if ($x==0){@a1 = split/ /,$1;$x=1}
>            else{@a2 = split/ /,$1}} END {print "\@a1 = @a1\n\@a2 = @a2"}' file1
@a1 = 1 2 3 4 5
@a2 = 7 8 9 0 11
$
$

Otherwise -

Code:
$
$ cat file1
#comment

k_array: 1 2 3 4 5
n_array: 7 8 9 0 11
$
$
$ ##
$ awk 'BEGIN {x=0} {
>       if (/:/ && x==0) { sub(/^.*: /,"",$0); split($0,a1); x=1}
>       else if (/:/ && x==1) { sub(/^.*: /,"",$0); split($0,a2)}
>      } END {
>          for (i=1; i<=length(a1); i++) {print "a1["i"] = "a1[i]}
>          for (i=1; i<=length(a2); i++) {print "a2["i"] = "a2[i]}
>      }' file1
a1[1] = 1
a1[2] = 2
a1[3] = 3
a1[4] = 4
a1[5] = 5
a2[1] = 7
a2[2] = 8
a2[3] = 9
a2[4] = 0
a2[5] = 11
$
$

tyler_durden

Last edited by durden_tyler; 09-28-2009 at 12:15 PM..
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to read from txt file and use that as an array

Hi Guys How u all doing? I am having tough time to achieve this I have a unix .ksh script which calls sql script Right now I harcoded column id's in sql script but I want to read them from a txt file 1084,1143,1074,1080,1091,1090,1101,1069,1104,1087,1089,1081 I want to read this... (4 Replies)
Discussion started by: pinky
4 Replies

2. Shell Programming and Scripting

how to read a var value into array

Hi I need to read a value of the variable into array so each character/digit will become an array element,for example: A=147921231432545436547568678679870 The resulting array should hold each digit as an element. Thanks a lot for any help -A (7 Replies)
Discussion started by: aoussenko
7 Replies

3. Shell Programming and Scripting

Issues using array credentials to read contents of a file

Hi, I am trying to read the contents of a file using array credentials in unix. The file I am trying to read is tab separated and contains the below contents. # partnerid Direc Server Port Source_Dir Target_Dir Mask Remove Files Passwordless Compare Files ... (3 Replies)
Discussion started by: aartikara
3 Replies

4. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

5. Shell Programming and Scripting

perl: Read array from a flat file

Hello Guru's I want to read an array into a flatfile Please let me know how to do the same So far this the below code use strict; use warnings; open (my $data , '<', $ARGV)|| die "could not open $ARGV:\n$!"; my @array=(<$data>); my @sorted=sort... (8 Replies)
Discussion started by: Pratik4891
8 Replies

6. UNIX and Linux Applications

Perl Script to read an excel file into an array and search in the UNIX directories

Hi, I want the Perl script with versions 5.8.2 and 5.8.5 starting with #!/usr/bin/perl The Perl program should read the excel file or text file line by line and taking into an array and search in the UNIX directories for reference file of .jsp or .js or .xsl with path .The Object names... (2 Replies)
Discussion started by: pasam
2 Replies

7. Shell Programming and Scripting

Read file and get the data then put it in array

Hi, I have a file called "readfile" it contains below parameters #cat readfile word=/abc=225,/abc/cba=150 three=12 four=45 five=/xyz/yza likewise multiple line. From the above file, I have to read "word" output should be like, /abc /abc/cba these values need to be put in... (3 Replies)
Discussion started by: munna_dude
3 Replies

8. Programming

C++ how would you read this file into an array.

here is the pesudo file. REREREEEEEERRRREER SOMEStrinG1234 RERRRR EEERRRREER SOMEStrinG1224 REREREEEREERRR REE SOMEStrinG1214 REREREREREREREEEER SOMEStrinG1204 RERE EEEEEERRRRRRR SOMEStrinG1294 REREEREEE ERRRREER SOMEStrinG1284 REREREEEEEERR REER here is my attempted code #include... (3 Replies)
Discussion started by: briandanielz
3 Replies

9. UNIX for Beginners Questions & Answers

How To Read a File and Assign the line values to an Array?

i have this basic code that i wrote to read a file and place it's values to an array. the source/input file will have multiple strings on it that is separated by a whitespace. sample_list.txt file contents: ACCT1 TABLE1 ACCT2 TABLE2 ACCT3 TABLE3 script file: sample_list.sh ... (3 Replies)
Discussion started by: wtolentino
3 Replies
CAT(1)							    BSD General Commands Manual 						    CAT(1)

NAME
cat -- concatenate and print files SYNOPSIS
cat [-beflnstuv] [-] [file ...] DESCRIPTION
The cat utility reads files sequentially, writing them to the standard output. The file operands are processed in command line order. A single dash represents the standard input, and may appear multiple times in the file list. The word ``concatenate'' is just a verbose synonym for ``catenate''. The options are as follows: -b Implies the -n option but doesn't number blank lines. -e Implies the -v option, and displays a dollar sign ('$') at the end of each line as well. -f Only attempt to display regular files. -l Set an exclusive advisory lock on the standard output file descriptor. This lock is set using fcntl(2) with the F_SETLKW command. If the output file is already locked, cat will block until the lock is acquired. -n Number the output lines, starting at 1. -s Squeeze multiple adjacent empty lines, causing the output to be single spaced. -t Implies the -v option, and displays tab characters as '^I' as well. -u The -u option guarantees that the output is unbuffered. -v Displays non-printing characters so they are visible. Control characters print as '^X' for control-X; the delete character (octal 0177) prints as '^?'. Non-ascii characters (with the high bit set) are printed as 'M-' (for meta) followed by the character for the low 7 bits. EXIT STATUS
The cat utility exits 0 on success, and >0 if an error occurs. EXAMPLES
The command: cat file1 will print the contents of file1 to the standard output. The command: cat file1 file2 > file3 will sequentially print the contents of file1 and file2 to the file file3, truncating file3 if it already exists. See the manual page for your shell (i.e., sh(1)) for more information on redirection. The command: cat file1 - file2 - file3 will print the contents of file1, print data it receives from the standard input until it receives an EOF ('^D') character, print the con- tents of file2, read and output contents of the standard input again, then finally output the contents of file3. Note that if the standard input referred to a file, the second dash on the command-line would have no effect, since the entire contents of the file would have already been read and printed by cat when it encountered the first '-' operand. SEE ALSO
head(1), hexdump(1), lpr(1), more(1), pr(1), tail(1), view(1), vis(1), fcntl(2) Rob Pike, "UNIX Style, or cat -v Considered Harmful", USENIX Summer Conference Proceedings, 1983. STANDARDS
The cat utility is expected to conform to the IEEE Std 1003.2-1992 (``POSIX.2'') specification. The flags [-belnstv] are extensions to the specification. HISTORY
A cat utility appeared in Version 1 AT&T UNIX. Dennis Ritchie designed and wrote the first man page. It appears to have been cat(1). BUGS
Because of the shell language mechanism used to perform output redirection, the command ``cat file1 file2 > file1'' will cause the original data in file1 to be destroyed! This is performed by the shell before cat is run. BSD
September 23, 2006 BSD
All times are GMT -4. The time now is 08:43 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy