nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

Trouble with arrays

This is a discussion on Trouble with arrays within the Shell scripting forums, part of the Development/Scripting category; Hello, i'm writing a script which should do the following. 1. Open two files and load them into arrays 2. ...

Register free or login to your existing account and remove all advertisements.


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-07-2009, 06:04 AM
Junior Member
User
 
Join Date: May 2009
OS: OSX
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
quadmachine is on a distinguished road
Default Trouble with arrays

Hello, i'm writing a script which should do the following.
1. Open two files and load them into arrays
2. Find same lines in the two files, and then append a line just under the duplicate

Code:
#!/bin/bash
#Declare array 
declare -a inputFile
#Open file for reading to inputFile
exec 10<kurac.txt
let count=0
while read LINE <&10; do
    inputFile[$count]=$LINE
    ((count++))
# get number of lines in the inputFile
lajneInput=${#inputFile[@]}
done
#Declare array 
declare -a outFile
#Open file for reading to outFile
exec 11<sisa.txt
let count=0
while read LINE <&11; do
    outFile[$count]=$LINE
    ((count++))
# get number of lines in the outFile
lajneOut=${#outFile[@]}
done

echo Broj lajni u ulaznom: ${#inputFile[@]}
echo Broj lajni u izlaznom: ${#outFile[@]}

# Select a line in inputFile and compare it to a line in outputFile
for (( i=0;i<$lajneInput;i++)); do
    lajnaIn=${inputFile[${i}]}
    y=i+1
    lajnaZapis=${inputFile[${y}]}
    for (( j=0;j<$lajneOut;j++)); do
        lajnaDva=${outFile[${j}]}
# Make sure the line1 and line1 are the same, and they have n as second char...
            if [ "${lajnaIn:1:1}" = "n" ] && [ "$lajnaIn" = "$lajnaDva" ]; then 
# Append a line to outputFile 
printf "%s\n" "${j}a" "$lajnaZapis" "." "w" | ed -s sisa.txt

            fi
    done
done
The problem seems to be that when it append a line to FILE2, the array which represents FILE2 doesn't "refresh" and shows old data, so when the next itteration comes it messes up :S

How do I make it refresh the array after each itteration?

Thanks for your help!

cheers!
Reply With Quote
  #2 (permalink)  
Old 05-19-2009, 05:02 AM
Member
User
 
Join Date: May 2009
OS: Mandriva
Posts: 78
Thanks: 0
Thanked 14 Times in 14 Posts
Rep Power: 2
cfajohnson has a spectacular aura about cfajohnson has a spectacular aura about
Default

Quote:
Originally Posted by quadmachine View Post
Hello, i'm writing a script which should do the following.
1. Open two files and load them into arrays
2. Find same lines in the two files, and then append a line just under the duplicate

Code:
#!/bin/bash
#Declare array 
declare -a inputFile
#Open file for reading to inputFile
exec 10<kurac.txt

Keep file descriptors below 10; bash uses descriptors 10 and above internally
Quote:
Code:
let count=0
while read LINE <&10; do
    inputFile[$count]=$LINE
    ((count++))
# get number of lines in the inputFile
lajneInput=${#inputFile[@]}
done

You don't need to keep count of the lines; use += instead:

Code:
exec 3<kurac.txt
while read LINE <&3
do
  inputFile+=( "$LINE" )
done
lajneInput=${#inputFile[@]}
In bash 4.0 you can use the builtin command mapfile instead of
a loop:

Code:
mapfile -t inputFile < kurac.txt
Quote:
Code:
#Declare array 
declare -a outFile
#Open file for reading to outFile
exec 11<sisa.txt
let count=0
while read LINE <&11; do
    outFile[$count]=$LINE
    ((count++))
# get number of lines in the outFile
lajneOut=${#outFile[@]}
done

echo Broj lajni u ulaznom: ${#inputFile[@]}
echo Broj lajni u izlaznom: ${#outFile[@]}

# Select a line in inputFile and compare it to a line in outputFile
for (( i=0;i<$lajneInput;i++)); do
    lajnaIn=${inputFile[${i}]}
    y=i+1
    lajnaZapis=${inputFile[${y}]}
    for (( j=0;j<$lajneOut;j++)); do
        lajnaDva=${outFile[${j}]}
# Make sure the line1 and line1 are the same, and they have n as second char...
            if [ "${lajnaIn:1:1}" = "n" ] && [ "$lajnaIn" = "$lajnaDva" ]; then 
# Append a line to outputFile 
printf "%s\n" "${j}a" "$lajnaZapis" "." "w" | ed -s sisa.txt

Why are you using ed? Use append redirection:

Code:
printf "%s\n" "$lajnaZapis" >> sisa.txt
Quote:
Code:
            fi
    done
done
The problem seems to be that when it append a line to FILE2, the array which represents FILE2 doesn't "refresh" and shows old data, so when the next itteration comes it messes up :S

How do I make it refresh the array after each itteration?

Code:
outFile+=( "$lajnaZapis" )
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Open Source Trouble Ticket Application w/ Active Directory Integration Johnny Utah Linux software 3 03-05-2008 01:30 PM
Search Pattern And Arrays mercuryshipz Coding in General 1 02-11-2008 03:26 PM
How to use arrays and its values present in one script? Nishanthhampali Shell scripting 0 01-31-2008 03:04 PM
More trouble with sed :oops: sparky Shell scripting 9 07-13-2006 10:21 PM


All times are GMT +5.5. The time now is 11:48 AM.


Powered by vBulletin® Version 3.8.4 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2009 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38