LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-15-2009, 10:34 PM   #1
jayran
LQ Newbie
 
Registered: Dec 2009
Posts: 14

Rep: Reputation: 0
Unhappy need help with writing array elements into a file


Hi, I have an array like this:
array[0] =
268
11132
11953
12097
12932
13499
13761

array[1] =
950
1140
1324
4231
4382
5197
7307
7329
9678
10953
12267
13669
18594

array[2] =
438
492
987


and so on up to array[1999]. I am trying to write these columns into a file (basically get a matrix out of it with zeros added where needed to adjust the length of each column); so my file will look like this (or its transpose; doesnt really matter):
268 950 438 ...
11132 1140 492 ...
11953 1324 987 ...
12097 4231 0 ...
12932 4382 0 ...
13499 5197 0 ...
...
... .... ... .. .

is there a way i can do this? i have been looking all around the web, turned into a webwiz but havent solved my prob yet... should i use echo with IFS fixed as newline? i dont know.
 
Old 12-15-2009, 10:43 PM   #2
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 12,712
Blog Entries: 27

Rep: Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055
Hi,

Welcome to LQ!

Now that we are all aware of your 'needs'. What are your 'deeds'?

Sure does smell like homework to me.

 
Old 12-15-2009, 10:49 PM   #3
jayran
LQ Newbie
 
Registered: Dec 2009
Posts: 14

Original Poster
Rep: Reputation: 0
you are right... it is that time of the year, time of projects...but this only the beginning part of my homework!! ;-((
mmm...as my deeds... i thought of something like this to begin with:
for ((i=0; i<$((${arr[@]})); i++))
echo $arr[i] > file
tr '\n' ' ' < file
done

but what do i do with the zeroes...?
 
Old 12-15-2009, 11:16 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
echo ${array[@]} > file
 
Old 12-15-2009, 11:28 PM   #5
jayran
LQ Newbie
 
Registered: Dec 2009
Posts: 14

Original Poster
Rep: Reputation: 0
Hi ghostdog, i really like that movie.
your code gives me all array elements in one row:

declare -a antar
antar[3]=`echo -e "42\n43\n45\n47\n"`
antar[2]=`echo -e "32\n33\n35\n37\n"`
antar[1]=`echo -e "22\n23\n25\n27\n"`
antar[0]=`echo -e "12\n13\n15\n17\n"`

bash-3.2$ echo "${antar[0]}"
12
13
15
17

echo ${antar[@]} > file
cat file
12 13 15 17 22 23 25 27 32 33 35 37 42 43 45 47
 
Old 12-15-2009, 11:34 PM   #6
jayran
LQ Newbie
 
Registered: Dec 2009
Posts: 14

Original Poster
Rep: Reputation: 0
oh, also if i put double quotes in echo, i get sth different, but still not what i want:
bash-3.2$ echo "${antar[@]}" > file
bash-3.2$ cat file
12
13
15
17 22
23
25
27 32
33
35
37 42
43
45
47
 
Old 12-15-2009, 11:48 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
how did you get the array in the first place?
 
Old 12-16-2009, 12:00 AM   #8
jayran
LQ Newbie
 
Registered: Dec 2009
Posts: 14

Original Poster
Rep: Reputation: 0
well... the code is long. Here it is:

IFS=$'\n'; arr=( $( < arts1.tsv ) );
for ((i=0; i<$((${#arr[@]})); i++)); do echo "${arr[$i]}" | tr "[[:space:]]" '\n' | grep -v '^$' | sort | uniq >> ar; sed -i -e 's/^/ /;s/$/ /' ar; while read line <&3; do sed -i -e "s/[[:space:]]$line[[:space:]]/ /g" ar; done 3<stopwords; cat ar | sed "s/[[:space:]]//g" | grep -v '^$' > aar; mv aar ar; num="1"; while read line <&5; do while read LINE <&4; do if [ "$line" == "$LINE" ]; then echo "$num" >> artres; fi; done 4<ar; ((num++)); done 5<word.tsv; artarr[$i]=`cat artres`; rm ar; rm artres; done

It basically reads different articles listed in "arts1.tsv", then eliminates some unimportant words such as a, an, the,... using "stopwords" file. Then, compares those words in "arts1.tsv" articles (now listed in "ar" file) to a file "word.tsv" (contains a lot of words - sth like a library of words). i need to find out which article has which words from the file "word.tsv". So this file that I wrote at the begining of the thread has the indices of words in "word.tsv" contained in 1st article of "arts1.tsv", 2nd... and 2000th article of "arts1.tsv"
now I need to do some statistical analysis on this matrix and i need to pass to matlab.
I hope my explanation ins understandable!
thx.
 
Old 12-16-2009, 12:10 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
put your code in code tags. don't expect me(us) to read your code like that right? also, show samples of relevant files and describe your output. finally, a suggestion, since you are doing matlab, why don't you do everything in matlab,especially if you are dealing with matrixes. For reading files you can use LOAD (SAVE) or dlmread() ?? check the matlab docs for more.

Last edited by ghostdog74; 12-16-2009 at 12:14 AM.
 
Old 12-16-2009, 12:11 AM   #10
jayran
LQ Newbie
 
Registered: Dec 2009
Posts: 14

Original Poster
Rep: Reputation: 0
sorry...what are code tags?
 
Old 12-16-2009, 12:16 AM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by jayran View Post
sorry...what are code tags?
when you reply, you can see the "#" sign in the tool bar. put your code in between those tags.
 
Old 12-16-2009, 12:29 AM   #12
jayran
LQ Newbie
 
Registered: Dec 2009
Posts: 14

Original Poster
Rep: Reputation: 0
oh. sorry. Here we go:

IFS=$'\n'; arr=( $( < arts1.tsv ) ); # arr is an array; where arr[0] contains the first article ; arr[1] the second, etc

for ((i=0; i<$((${#arr[@]})); i++)); do # i = 1 up to arr length(=2000)
echo "${arr[$i]}" | tr "[[:space:]]" '\n' | grep -v '^$' | sort | uniq >> ar;
# here i just put each word into a new line, so i have a column of words in the article; i do this for each article and put the result in file "ar"; i remove "ar" at the end of the loop later on

sed -i -e 's/^/ /;s/$/ /' ar; # then i add a sapce to the begin and end of eah line(=each word) of "ar"
while read line <&3; do # this is the part where i take out those unimportant words
sed -i -e "s/[[:space:]]$line[[:space:]]/ /g" ar;
done 3<stopwords;
cat ar | sed "s/[[:space:]]//g" | grep -v '^$' > aar;
#then i take out that space from the begin and end of remaining words of "ar", kill all empty line and put these new words into a new file "aar"
mv aar ar; # remove "aar" to "ar"
num="1";
while read line <&5;
do while read LINE <&4;
do if [ "$line" == "$LINE" ];
then echo "$num" >> artres; # i read words from "word.tsv", compare them to words in "ar", and if its a match i get the indice of that word in "word.tsv" and put it in "artres" file; line by line. I get a column of indices.
fi;
done 4<ar;
((num++));
done 5<word.tsv;
artarr[$i]=`cat artres`;
# i put this file, "artres", containing indices of words in" words.tsv" matching words from ith (for loop) article of "arts1.tsv" into ith element of "artarr" array
rm ar; rm artres;
done
 
Old 12-16-2009, 12:30 AM   #13
jayran
LQ Newbie
 
Registered: Dec 2009
Posts: 14

Original Poster
Rep: Reputation: 0
Code:
IFS=$'\n'; arr=( $( < arts1.tsv ) );
for ((i=0; i<$((${#arr[@]})); i++)); do echo "${arr[$i]}" | tr "[[:space:]]" '\n' | grep -v '^$' | sort | uniq >> ar; sed -i -e 's/^/ /;s/$/ /' ar; while read line <&3; do sed -i -e "s/[[:space:]]$line[[:space:]]/ /g" ar; done 3<stopwords; cat ar | sed "s/[[:space:]]//g" | grep -v '^$' > aar; mv aar ar; num="1"; while read line <&5; do while read LINE <&4; do if [ "$line" == "$LINE" ]; then echo "$num" >> artres; fi; done 4<ar; ((num++)); done 5<word.tsv; artarr[$i]=`cat artres`; rm ar; rm artres; done
like this?
 
Old 12-16-2009, 12:34 AM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
Well, you've got the code tags sorted, but no-one is going to read that 'one-liner'; it's too hard.
How about a sane layout eg each newline of code gets it's own line ?
 
Old 12-16-2009, 12:34 AM   #15
jayran
LQ Newbie
 
Registered: Dec 2009
Posts: 14

Original Poster
Rep: Reputation: 0
i could do all in matlab, but part of the requirement for the homework is to extract data to be analyzed this way.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ array question on non assigned elements davimint Programming 9 11-24-2008 09:43 AM
trimming perl array elements homey Programming 7 02-17-2008 03:48 PM
print array elements in one line bharatbsharma Programming 1 10-29-2007 08:58 AM
Simultaneous writes into different elements of an array estratos Programming 7 12-15-2006 06:36 AM
Renaming array elements in bash bryan.out.there Programming 2 05-31-2006 11:44 PM


All times are GMT -5. The time now is 05:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration