0

I'm trying to read lines from a .txt and copy each line from the text to its own folder that was previously created with ARRAY

cat stores/locations.txt |while read -r LINE;do 
echo "$LINE" > county/${ARRAY[${i}]}/localstores.txt
done

when I run this it creates only 1 file in the directory county with one line of contact information for the store but what i really want it to do is to put a file in each element of the ARRAY instead of the parent folder county.

each line of data includes the following:

<storeid> <storename> <amountofEmployees> <nameofManager>

I'm super stuck and really would appreciate the help!

3
  • 1
    if we had sample data, we wouldn't have to guess, but my guess is that you want to append multiple lines into the county/${ARRAY[${i}]}/localstores.txt files. For that, you should be able to use >> redirection. Good luck. Commented Jul 17, 2016 at 21:54
  • Thanks for the prompt reply. the data is lines of information something along of the line of <storenumber> <storeaddress> <number of employees> <manager in charge> ..... so each line should be going to its own folder Commented Jul 17, 2016 at 22:35
  • Thanks for the sample data, that helps some. Did you try >> for append redirection? Good luck. Commented Jul 17, 2016 at 22:50

1 Answer 1

1

A simple way to do that would be as below, which creates a new file for each of the line and optionally also adds the line to an array.

 declare -A myArray()

 while IFS= read -r line # Read a line
 do
    touch  mytargetpath/"$line.txt" # Creates a new file for each of the line in the desired path
    myArray+=("$line") # Append line to the array
 done < stores/location.txt

And print array contents as:-

# Print the file (print each element of the array)

for e in "${myArray[@]}"
do
    echo "$e"
done
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.