Write array contents to file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Write array contents to file
# 1  
Old 12-16-2011
Write array contents to file

Hi,

I have a bash script that currently holds some data. I am trying to write all the contents to a file called temp.txt.

I am using
Code:
echo ${array[@]} > temp.txt

The problem that I am experiencing is that the elements are being written horizontally in the file. I want them written vertically.

This code writes it correctly but it appends the file which I don't want it to do.

Code:
for j in "${array[@]}"
do
  echo $j >>temp.txt
done

By changing the line to echo $j > temp.txt, the file only contains the last array element.



Example

array[0] = 5
array[1] = 6
array[2] = 1
array[3] = 3

File contents

5 6 1 3

What I want is

5
6
1
3

Thanks.

Last edited by Franklin52; 12-16-2011 at 11:59 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 12-16-2011
You can move your redirection to the end of the loop, and use a single redirection that will overwrite the file:

Code:
for j in "${array[@]}"
do
      echo $j 
done >tmp.txt


Last edited by agama; 12-16-2011 at 02:01 AM.. Reason: indention
This User Gave Thanks to agama For This Post:
# 3  
Old 12-16-2011
Code:
echo ${x[@]} | sed 's/ /\n/g' > temp.txt

This User Gave Thanks to balajesuri For This Post:
# 4  
Old 12-16-2011
Thanks Agama

Your suggestion worked perfectly.
# 5  
Old 12-16-2011
Code:
> temp.txt   # clear file or create
for j in ${array[@]}
do
     echo $j >>temp.txt
done

Code:
for j in ${array[@]}
do
   echo $j 
done >> temp.txt  # append   or  done > temp.txt to overwrite

# 6  
Old 12-16-2011
No need to use loops or sed to separate an array on newlines. The shell can do this by itself by changing the IFS variable into a newline, letting you print the entire array in a single operation.
Code:
OLDIFS="$IFS"; IFS=$'\n'
        echo "${ARRAY[*]}" > file
IFS="$OLDIFS"

Note that "${ARRAY[*]}" must be in quotes for IFS to have the right effect here.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Trying To Write File Contents To Specfic .csv Cell

Hi, I'm attempting to write the entire contents of a file to a specific .csv cell. So far have only a nawk one liner that will write a value into a specific .csv cell. Trying to use man page but can't seem to get any farther. Any help would be appreciated. nawk -v r=2 -v c=3 -v val=5 -F,... (7 Replies)
Discussion started by: jimmyf
7 Replies

2. UNIX for Dummies Questions & Answers

Write terminal contents into a one file in UNIX

Hi guys, How to write terminal contents into a file in Unix operating system Actually I created GUI by using Gtk2-perl. I want to display data on GUI whatever the contents writing on terminal. So which command I have to use and where that command to be run I mean in shell script or Perl... (2 Replies)
Discussion started by: kiran425
2 Replies

3. Shell Programming and Scripting

Place the contents of a .CSV file to an array

Hi, I am trying to place the contents of a .CSV file to an array, but not sure how to do that. Here is my .CSV file content: App,SLA,Job name,Avg start time,Avg run time,Frequency,Downstream apps XYZ,,ABC14345,3:00 AM,00.04.00,Daily,STAMP XYZ,9:00,ABC12345,3:15 AM,00.05.00,Daily,STAMP ... (4 Replies)
Discussion started by: ajayakunuri
4 Replies

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

5. Shell Programming and Scripting

Most reliable way to store file contents in an array in bash

Hi Guys, I have a file which has numbers in it separated by newlines as follows: 1.113 1.456 0.556 0.021 -0.541 -0.444 I am using the following code to store these in an array in bash: FILE14=data.txt ARRAY14=(`awk '{print}' $FILE14`) (6 Replies)
Discussion started by: npatwardhan
6 Replies

6. Shell Programming and Scripting

PERL - copy fiel contents to array then compare against other file

Basically to illuminate i want to take a file with mutliple lines, C:\searching4theseletters.txt a b c Read this into an array @ARRAY and then use this to compare against another file C:\inputletters.txt b o a c n a (9 Replies)
Discussion started by: bradleykins
9 Replies

7. Shell Programming and Scripting

Remove spaces from first field, and write entire contents into other text file

Hi all, I have searched and found various threads about removing spaces from a field within a text file. Unfortunately, I have not found exactly what I'm looking for, nor am I adept enough to modify what I've found into what I need. I use the following command to remove the first line... (3 Replies)
Discussion started by: carriehoff
3 Replies

8. UNIX for Dummies Questions & Answers

compare array contents with file

I have an array "arrA" with the following contents: A0012 Paint Shop.doc ES001 Contract Signature.doc Budget Plan.pdf TS PWS.pdf My data file "Files.dat" has the same contents: A0012 Paint Shop.doc ES001 Contract Signature.doc Budget Plan.pdf TS PWS.pdf I have a script that compares... (0 Replies)
Discussion started by: orahi001
0 Replies

9. Programming

How to read and write directory or file contents in c++ ?

Dear Friends, I m beginner unix programmer. I want to know, how to read and write directory or file contents in c++ ? (3 Replies)
Discussion started by: namrata5
3 Replies

10. Shell Programming and Scripting

Can I assign the contents of file into an array?

I am trying to assign the contents of file e.g ls "$HOME" into an array. If it is possible then please guide me without using the concept of awk,sed, and perl16 Thanks (10 Replies)
Discussion started by: murtaza
10 Replies
Login or Register to Ask a Question