LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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-24-2011, 11:14 AM   #1
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Rep: Reputation: Disabled
BASH: read line and split it to array


Dear all,

I have a file with several fields for each line:
Code:
aaa   bbb   ccc
ddd   eee   fff
ggg   hhh   iii
...
I want to read this file line by line and when it read each line, this line is sent to an array with each filed as its element. I use a while loop to do this:
Code:
while read LINE
do
a=${LINE[0]}
b=${LINE[1]}
c=${LINE[2]}
echo $b
done < $1
I hope variable $a to be the first filed of each line in each cycle of while loop, that is aaa, ddd and ggg ..
same idea for $b and $c.

However, what I got is $b and $c are empty strings and $a is a string includes the whole line which has three fields together. I test it by echo them separately.

I have tried many tests and still do not find where is the problem. Could any one help me to figure it out? Thanks a lot!!

Last edited by cristalp; 11-24-2011 at 11:19 AM.
 
Old 11-24-2011, 11:24 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
It's not clear from your example which variable should be an array. Here are three different cases:

1. Let read create an array with the content of each line: you need the -a option:
Code:
while read -a LINE
do
  echo ${LINE[0]}
  echo ${LINE[1]}
  echo ${LINE[2]}
done < file
2. Explicitly assign the content of each line to an array:
Code:
while read line
do
  array=($line)
  echo ${array[0]}
  echo ${array[1]}
  echo ${array[2]}
done < file
3. Assign the content of each line to three variables individually:
Code:
while read a b c
do
  echo $a
  echo $b
  echo $c
done < file
What is that one matching your requirement?
 
1 members found this post helpful.
Old 11-24-2011, 03:12 PM   #3
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
To give line of text its own array element, you can use the new (bash v4+) mapfile command. a.k.a. readarray.

Code:
mapfile -t LINE < filename	#use -t to remove the trailing newlines.
mapfile also has a convenient -n option, allowing you to explictly set the index number to start on. Using -n 1 means that line one = index one, etc.

In older systems you can do something like this:

Code:
while IFS= read -r line ; do
	LINE+=( "$line" )
done < filename
I cleared the IFS setting for read, because otherwise it removes all leading and trailing whitespace from each line. You can of course remove it if this behavior is desirable.

There's also an issue with loops using read when a file doesn't contain a final newline. The contents get stored in the variable, but the subsequent commands aren't executed. You may need to explicitly save it after the loop with an additional command:

Code:
[[ -n $line ]] && LINE+=( "$line" )
http://mywiki.wooledge.org/BashFAQ/001


And finally a caution: colucix's second example likely does not do what you want. It breaks each line up into individual words and saves them to the array, but then overwrites them again when the next line executes. So only the final line remains contained in the array at the end of the loop.
 
  


Reply



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
[SOLVED] split a string into array in bash xeon123 Linux - Newbie 8 03-17-2011 11:16 AM
[SOLVED] bash: Split a text file into an array? (NOT line-by-line) DJCharlie Programming 9 09-19-2010 09:22 PM
[bash] Read file line by line and split on whitespace tskuzzy Programming 4 07-06-2009 03:24 PM
help with c program to read each line from text file, split line , process and output gkoumantaris Programming 12 07-01-2008 12:38 PM

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

All times are GMT -5. The time now is 01:15 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
Open Source Consulting | Domain Registration