LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-06-2009, 02:53 PM   #1
tskuzzy
LQ Newbie
 
Registered: Aug 2008
Posts: 9

Rep: Reputation: 0
[bash] Read file line by line and split on whitespace


I want to read in lines in the file and extract data from each line and store them into variables.

So suppose the input.txt file is:
Code:
0.001	1
0.005	1
0.01	1
0.001	10
0.005	10
0.01	10
So somehow I need to loop through this text file line by line and put the first number in a variable and the second number in another variable.

Is there a simple way to perform this task?
 
Old 07-06-2009, 02:59 PM   #2
0.o
Member
 
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 208

Rep: Reputation: 35
cat test | while read one two; do echo $one -- $two; done
 
1 members found this post helpful.
Old 07-06-2009, 03:02 PM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
while read LINE
do
var1=$(echo $LINE | cut -f1)
var2=$(echo $LINE | cut -f2)
do_something_sexy
done < input.txt

that's just with bash though (or rather bash and GNU tools - it's posisble to replace the echo / cut with something like var1=$(LINE%#\ ) but I can never remember the right syntax for the real short way so i always end up echoing)

[hands up that i didn't know read could go to multiple variables, props to mr 0.o]

better off using awk though

awk '{var1 = $1; var2 = $2; sexy_time}' -f input.txt

Last edited by acid_kewpie; 07-06-2009 at 03:04 PM.
 
1 members found this post helpful.
Old 07-06-2009, 03:17 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Yes!!

Set up an array for each column---let's say "this" and "that". On the first pass, you'll assign values to "this[1]" and "that[1]".

a straightforward way to assign variables based on columns is with AWK---eg:
varname = awk '{print $1}' filename

So, you might have something like this (some is pseudo-code, not tested):
Code:
n=1
while read line; do
var1 [n] = $(echo $line | awk '{print $1}')
var2 [n] = $(echo $line | awk '{print $2}')
increment n
done
printf to print var1 and var2 in desired format
 
Old 07-06-2009, 03:24 PM   #5
tskuzzy
LQ Newbie
 
Registered: Aug 2008
Posts: 9

Original Poster
Rep: Reputation: 0
Wow, nice variety of solutions!
I'm using 0.o's solution since its the simplest and most elegant one for my particular problem.

awk and cut are still way over my head to use effectively so I try to stay away from them :P
One of these days I'll learn...

Thanks for all the help
 
  


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
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
bash : read every line from text file starting at given line number quadmore Programming 4 02-20-2009 12:29 PM
I would like need a suggestion on bash shell : Read a file line by line and do stuff madi3d8 Linux - Newbie 1 01-15-2009 09:30 AM
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 03:53 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