LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-27-2009, 07:38 AM   #1
lifeforce4
Member
 
Registered: May 2003
Location: 2:16:840
Distribution: Slackware, CentOS, Arch, Debian
Posts: 169
Blog Entries: 1

Rep: Reputation: 32
Question Need clarification for file to bash script array.


I have a file that ends up just having numbers in each line, like the following.
Code:
1234575
12475
275473636
24757392
I want to set each number in its own array element with in a bash script. I have been searching and trying to get this to work with no such luck. Any advice or tips would be most appreciated. I have posted the script below.

Thank you,
Kyle

Code:
#!/bin/bash

declare -a ORDERIDARRAY
COUNTER=0
i=0

########
# Set ORDERIDs from email.txt in an array.
#
cat email.txt |cut -d " " -f 1|grep ^[0-9] > temp-array.txt

########
# Get each line of temp-array.txt and set it as an array element.
#

########
# This is one way that I found on the net but cant seem to get it to work.
old_IFS=$IFS
IFS=$'\n'
ORDERIDARRAY=($(cat temp-array.txt))
IFS=$old_IFS


########
# Another way was this yet again did not work.

#exec 3<./temp-array.txt || exit 1
#let COUNTER=0

#while read currentline <&3; do
#	ORDERIDARRAY=("${ORDERIDARRAY[@]}" "$currentline")
#done
#exec 3<&-

########
# echo each element in ORDERIDARRAY
#
for ((i=0; i<$ORDERIDARRAY; i++))
do
	echo ${ORDERIDARRAY[${i}]}
done
rm temp-array.txt
 
Old 11-27-2009, 08:08 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
You need to put spaces between the parentheses.

Code:
ORDERIDARRAY=( $(cat temp-array.txt) )
And you probably don't need to mess with IFS unless there's a possibility of spaces appearing in the individual entries.

More on arrays here:
http://www.tldp.org/LDP/abs/html/arrays.html

Bash version 4 also has a new "mapfile" builtin that reads lines from a file into array entries (it can also be called as "readarray"). Check the bash man page if you're using that version.

Last edited by David the H.; 11-27-2009 at 08:18 AM.
 
Old 11-27-2009, 08:32 AM   #3
lifeforce4
Member
 
Registered: May 2003
Location: 2:16:840
Distribution: Slackware, CentOS, Arch, Debian
Posts: 169

Original Poster
Blog Entries: 1

Rep: Reputation: 32
Thumbs up

Thank you for the quick reply, I did not noticed that I needed to include the spaces. It seems to be working now.

Thanks again,
Kyle
 
Old 11-27-2009, 10:09 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
another way with bash
Code:
a=( $(< file) )
 
  


Reply

Tags
array, bash, file, scripting


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
Help needed for using awk to parse a file to make array for bash script tallmtt Programming 12 04-14-2012 01:16 PM
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 10:07 AM
how to load an array from a file in RHEL bash script steven.c.banks Linux - General 3 10-03-2008 03:24 PM
Bash script, read file into array (Newbie) TheMeeks Programming 6 04-17-2007 08:20 AM
Bash script - how to reassemble file from array sixerjman Programming 11 08-25-2006 11:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:55 PM.

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
Open Source Consulting | Domain Registration