Arrays & File Reading


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Arrays & File Reading
# 1  
Old 12-17-2007
Question Arrays & File Reading

Ok; here is the code

Code:
INCREMENT=0 #Final Count
MATCH=0 #Treated as a Boolean
declare -a LINEFOUR #Declared Array

for FILE in $DIR; do # DIR was declared earlier
        test -f $FILE && (
        TEMP=(sed -n '4p' $FILE) #How do I assign the fourth line of the file to TEMP? This doesn't work.
        MATCH=0
        LFCOUNT=$LINEFOUR [@]
        for i in `seq 0 $LFCOUNT`; do
                if [ $LINEFOUR [$i] = $TEMP ]; then
                        MATCH=1
                fi
        done
        if [$MATCH=0]; then
                LINEFOUR [$INCREMENT]=TEMP
                let INCREMENT=$INCREMENT+1
        fi
        )
done

My main question is: How do I assign the fourth line into the TEMP variable? And how do I get the length of the array? I though it was $ARRAY[@]; but the above isn't working.

Thanks in advance for any help on this.
# 2  
Old 12-17-2007
I assume you are using the bash shell from the use of declare statement.

Answer to first question:
Code:
LINEFOUR=($(sed -n '4p' $FILE))

Answer to second question:
Code:
LFCOUNT=${#LINEFOUR[*]}


Last edited by fpmurphy; 12-17-2007 at 09:29 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading columns using arrays

Hello, Please help in how to read rows and columns using array and print them. I have below output and i want to store this in array and print the required rows or columns. aaaaaaa 123 bbbbbb 456 ccccccc 888 Use code tags, thanks. (1 Reply)
Discussion started by: Cva2568
1 Replies

2. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

I have a environment property file which contains: Input file: value1 = url1 value2 = url2 value3 = url3 and so on. I need to search all *.xml files under directory for value1 and replace it with url1. Same thing I have to do for all values mentioned in input file. I need script in unix bash... (7 Replies)
Discussion started by: Shamkamde
7 Replies

3. UNIX for Dummies Questions & Answers

Reading the dates from a file & moving the files from a directory

Hi All, I am coding for a requirement where I need to read a file & get the values of SUB_DATE. Once the dates are found, i need to move the files based on these dates from one directory to another. ie, this is how it will be in the file, SUB_DATE = 20120608,20120607,20120606,20120606... (5 Replies)
Discussion started by: dsfreddie
5 Replies

4. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

Hi, I have a folder which contains multiple config.xml files and one input file, Please see the below format. Config Files format looks like :- Code: <application name="SAMPLE-ARCHIVE"> <NVPairs name="Global Variables"> <NameValuePair> ... (0 Replies)
Discussion started by: haiksuresh
0 Replies

5. Shell Programming and Scripting

Reading a .dat file in to 2 different arrays

hi all, i have a data file that contains 2 columns, names and numbers. i need to read names in to a an array call names and numbers in to an array call numbers. i also have # and blank lines in my dat file and i need to skip those when i read the dat file. how do i do this? btw, my column 1 and... (3 Replies)
Discussion started by: usustarr
3 Replies

6. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

7. Shell Programming and Scripting

KSH: Reading a file line by line into multiple arrays

Hi - I have a file that contains data in this format:- #comment value1 value2 value3 #comment value4 value5 value6 value7 #comment value8 value9 I need to read value1, value2 and value3 into one array, value4 value5 value6 and value7 into another array and value8 and value9 into a 3rd... (2 Replies)
Discussion started by: sniper57
2 Replies

8. Shell Programming and Scripting

Test File Reading & Validation using Shell script

Please help develop script for below requirement -------Sample file------------------------------- HSVSHOSTRECON 20090115011817BP DARMAR60064966247003504720000000000000000000066626000000000000133000003D003463001332 ... (14 Replies)
Discussion started by: niraj_bhatt
14 Replies

9. Shell Programming and Scripting

Reading in data sets into arrays from an input file.

Hye all, I would like some help with reading in a file in which the data is seperated by commas. for instance: input.dat: 1,2,34,/test for the above case, the fn. will store the values into an array -> data as follows: data = 1 data = 2 data = 34 data = /test I am trying to write... (5 Replies)
Discussion started by: sidamin810
5 Replies

10. Shell Programming and Scripting

Reading value from one file & using it for 2nd file

In File A I have 1st col as numbers. Using these numbers, I need to find out the rows at this numbered index in other file. I know loose commands. To get the 1st col from file A : awk '{print $1}' fileA To get the row at the index, say 100 - from file B : sed '100p' fileB After... (7 Replies)
Discussion started by: videsh77
7 Replies
Login or Register to Ask a Question