Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Distributions > Red Hat
User Name
Red Hat This forum is for the discussion of Red Hat Linux.


  Search this Thread
Old 04-30-2007, 03:34 PM   #1
Registered: Jun 2006
Posts: 132

Rep: Reputation: 15
how to read certain line of file, and output to variable?

I have a file : arg.txt
oi1 oi2 oi3 oi4
k2 k3
l4 l5 l6

the line nubmers are fixed.but elements in each line are seperated by space and their numbers are random, not always same. that's mean, next time you read arg.txt maybe will looks like this:

oi1 oi2 oi3 oi4 oi5
k2 k3
l4 l5 l6

how to read certain line of file, and put each element in certain line to variables.
for exmple,I want to output line 1 to variables:

line1=getline 1 arg.txt # this is only a assume,not real code.
y=(number of line1) # I don't know how to get it.
for (( i =0; i<=y; i++ ))

#arg1 arg2 arg3 ...arg(y-1) are source files, argy are desionation directory.
cp arg1 arg2 arg3 ...arg(y-1) argy

I'm new to bash script,sorry for the mess.
Old 04-30-2007, 04:52 PM   #2
HCL Maintainer
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 76
First off, I don’t see what this has to do with Red Hat…

As for your problem, the most elegant solution involves bash arrays like so:
while read fullline; do

	for i in $(seq 0 $(($y - 1))); do
		echo -n "${line[$i]} "
	done; echo

#	echo ${line[*]}
#	cp ${line[*]}
done < arg.txt
This code sample reads in one line at a time to the variable “fullline”. In each iteration of the outer loop, an array called “line” is declared with contents of $fullline, but each in a separate index of the array. So you could access each element of the array by index, starting at 0 (for example, in the first iteration of the loop, if you said “echo ${line[2]}”, it would output “oi3”). You can access all the elements of the array with spaces in between with “${line[*]}”, and you can access the number of elements with “${#line[*]}”. The for loop simply prints each element followed by a space, and a final newline (the same thing could more easily have been accomplished by uncommenting the line in blue). If you change, for example, the number in red from “1” to “2”, you print out all but the last element (i.e., arg1 arg2 arg3 … arg(y-1)). Finally, the line in green (if uncommented) does what you ask — “cp arg1 arg2 arg3 … arg(y-1) argy”.

I suggest you read up on bash arrays. If you have any questions, just post.
Old 05-01-2007, 08:46 AM   #3
Registered: Jun 2006
Posts: 132

Original Poster
Rep: Reputation: 15
thanks! it works!


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
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM
Bash store last line from displayed text output in a variable carl0ski Programming 1 01-16-2007 03:38 AM
Saving the output of a file as a variable mrobertson Programming 5 07-08-2005 12:43 AM
Command to output file content line by line aznluvsmc Programming 2 09-12-2004 07:45 PM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM > Forums > Linux Forums > Linux - Distributions > Red Hat

All times are GMT -5. The time now is 10:02 AM.

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