How do I cat into an array or is it not possible?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I cat into an array or is it not possible?
# 1  
Old 08-31-2012
How do I cat into an array or is it not possible?

Hi,

Normally, I would do

Code:
cat /etc/oratab | grep -v "^#" | grep -v "^*" > /tmp/oratab.00
while read line
do
   echo $line
done < /tmp/oratab.00

I want to know whether it is possible to use an array instead of re-directing it to file?

As a test, I did something like below:

Code:
#!/bin/ksh

cat /tmp/oratab.00 | grep -v "^#" | grep -v "^*" > /tmp/oratab.01
while read line
do
   echo $line
done < /tmp/oratab.01

echo ""
echo "###########"
echo ""

set -A oratab_array
oratab_array[*]=`cat /tmp/oratab.00 | grep -v "^#" | grep -v "^*"`
oratab_array_count=${#oratab_array[@]}
echo "oratab_array_count = ${oratab_array_count}"

echo ""
echo "###########"
echo ""

exit 0

Output from a sample run below:

Code:
mnl1:/opt/oracle/9.2.0.8:N:::N
mkt1:/opt/oracle/11.2.0:N:::N
cal1:/opt/oracle/11.2.0:N:::N

###########

oratab_array_count = 1

###########

/tmp/oratab.00 as below:

Code:
#
# This file is used by ORACLE utilities. It is created by root.sh
# and updated by the Database Configuration Assistant when creating
# a database.
# A colon, ':', is used as the field terminator. A new line terminates
# the entry. Lines beginning with a pound sign, '#', are comments.
#
# Entries are of the form:
# $ORACLE_SID:$ORACLE_HOME:<N|Y>:
#
# The first and second fields are the system identifier and home
# directory of the database respectively. The third filed indicates
# to the dbstart utility that the database should , "Y", or should not,
# "N", be brought up at system boot time.
#
# Field #5 is the db type (""=standalone database, P=primary, S=standby)
# Field #6 is used by the scripts in /nas_mnt/common/dba_utils/PS/patrol
#
# Multiple entries with the same $ORACLE_SID are not allowed.
#
#
*:/opt/oracle/9.2.0.8:N
mnl1:/opt/oracle/9.2.0.8:N:::N
mkt1:/opt/oracle/11.2.0:N:::N
cal1:/opt/oracle/11.2.0:N:::N

Note that oratab_array_count is 1. Am expecting to have 3 but I assume what's happening is the result of cat | grep is being treated as one single line? How do I get it to treat them as individual array members?

Feedback much appreciated. Thanks in advance.
# 2  
Old 08-31-2012
You could redirect into a while loop using something like this:

Code:
cat /etc/oratab | while read -a L ; do
   echo ${L[0]} etc...
done

Code:
cat /etc/oratab | while read file ; do i=$((${i:=-1} + 1)) ; lines[$i]="$file" ; done


Last edited by Subbeh; 08-31-2012 at 08:26 AM.. Reason: The first example doesn't really work. The second one does, but I think there is a better way to do it
# 3  
Old 08-31-2012
If you have bash you may be able to use mapfile.
# 4  
Old 08-31-2012
ksh:
Code:
# set -A ARR $(grep -vE "^#|^\*" oratab)
# echo ${ARR[0]}
"mnl1:/opt/oracle/9.2.0.8:N:::N"
# echo ${ARR[1]}
"mkt1:/opt/oracle/11.2.0:N:::N"
# echo ${ARR[2]}
"cal1:/opt/oracle/11.2.0:N:::N"


Last edited by zaxxon; 08-31-2012 at 08:55 AM.. Reason: Sticking to grep as awk was overpowered; thought double quotes around elements at declaration were needed, was wrong.
# 5  
Old 08-31-2012
Assuming that there are no spaces and/or tabs in the lines:
Code:
set -A arr $(sed '/^[*#]/d' file)
echo ${#arr[*]}
3
echo ${arr[0]}
mnl1:/opt/oracle/9.2.0.8:N:::N
echo ${arr[1]}
mkt1:/opt/oracle/11.2.0:N:::N
echo ${arr[2]}
cal1:/opt/oracle/11.2.0:N:::N

EDIT: Oops..similar solution already provided by zaxxon.

Last edited by elixir_sinari; 08-31-2012 at 10:38 AM..
# 6  
Old 08-31-2012
just one more way to do it...

Here is how i'd do that :
Code:
while read line ; do 

  array[${#array[@]}]="$line"

done <<<"$(grep -vE "^[#|*]" test.txt)"

And here is what inside the array after :

Code:
bash-3.2$ echo ${array[0]}
mnl1:/opt/oracle/9.2.0.8:N:::N
bash-3.2$ echo ${array[1]}
mkt1:/opt/oracle/11.2.0:N:::N
bash-3.2$ echo ${array[2]}
cal1:/opt/oracle/11.2.0:N:::N


EDIT : i've been caught in UUOC ! Shame on me!!! I updated my mistake.

Last edited by pierpier; 08-31-2012 at 06:38 PM..
# 7  
Old 08-31-2012
There's another option without using an array, you can set the $1 $2 ... parameters.

Code:
OLDIFS="$IFS"
IFS=$'\n' # Set IFS to newline
        set -- `grep -v "^#" /etc/oratab | grep -v "^*"`
IFS="$OLDIFS"

echo "$1"
echo "$2"

See also useless use of cat. Grep does not need cat's help to read a file.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash arrays: rebin/interpolate smaller array to large array

hello, i need a bit of help on how to do this effectively in bash without a lot of extra looping or massive switch/case i have a long array of M elements and a short array of N elements, so M > N always. M is not a multiple of N. for case 1, I want to stretch N to fit M arrayHuge H = (... (2 Replies)
Discussion started by: f77hack
2 Replies

2. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

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

4. Shell Programming and Scripting

Read files, lines into array, cat vs open

Hi Everyone, I have a file: a.txt a,b,c,d,6,6,6 1,2,3,d,6,6,6 4,5,6,6,6,6,6 #!/usr/bin/perl use warnings; use strict; my @array = (); ### Load file into array for my $i (split '\n', `cat /tmp/a.txt`) { push @array, ; } It works. But my a.txt have 1million lines, and... (2 Replies)
Discussion started by: jimmy_y
2 Replies

5. Shell Programming and Scripting

Bash: Zeilen aus Datei mit cat und grep in dynamisches Array schreiben

Hallo, ich habe eine Datei "Kino.ini" die z.B. wie folgt aussieht * KINOFILM A bla bla KINOFILM B blubb blubb KINOFILM C Ich möchte nun die Datei "Kino.ini" per cat und grep auslesen und testen ob der String KINOFILM nur mit einem '*' am Anfang vorkommt. In dieser Beispieldatei... (3 Replies)
Discussion started by: ABE2202
3 Replies

6. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies

7. Shell Programming and Scripting

for i in `cat myname.txt` && for y in `cat yourname.txt`

cat myname.txt John Doe I John Doe II John Doe III ----------------------------------------------------------------------- for i in `cat myname.txt` do echo This is my name: $i >> thi.is.my.name.txt done ----------------------------------------------------------------------- cat... (1 Reply)
Discussion started by: danimad
1 Replies

8. Programming

Creating an array to hold posix thread ids: Only dynamic array works

I am facing a strange error while creating posix threads: Given below are two snippets of code, the first one works whereas the second one gives a garbage value in the output. Snippet 1 This works: -------------- int *threadids; threadids = (int *) malloc (num_threads * sizeof(int)); ... (4 Replies)
Discussion started by: kmehta
4 Replies

9. UNIX for Dummies Questions & Answers

Difference between cat , cat > , cat >> and touch !!!

Hi Can anybody tell the difference between Difference between cat , cat > , cat >> and touch command in UNIX? Thanks (6 Replies)
Discussion started by: skyineyes
6 Replies
Login or Register to Ask a Question