LinuxQuestions.org
Visit Jeremy's Blog.
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 06-17-2012, 01:54 PM   #1
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Rep: Reputation: Disabled
read line basic question


hi

i have a question

when i write:

Code:
while read line;do

...

done
from where am i read the line?

also, if i want to read a line from the file: "ttt.dat"

what is the code to do that?

thanks alot
 
Old 06-17-2012, 02:17 PM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

If you type in the console `help read | less', you'll probably see the following:
Quote:
$ help read
read: read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
Read a line from the standard input and split it into fields.

Reads a single line from the standard input, or from file descriptor FD
if the -u option is supplied. The line is split into fields as with word
splitting, and the first word is assigned to the first NAME, the second
word to the second NAME, and so on, with any leftover words assigned to
the last NAME. Only the characters found in $IFS are recognized as word
delimiters.
....
So, `read' reads from standard input. If you want to read from file, just redirect it to standard input:
Code:
while read line;do

...

done < input_file
 
Old 06-17-2012, 02:25 PM   #3
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by firstfire View Post
Hi.

If you type in the console `help read | less', you'll probably see the following:

So, `read' reads from standard input. If you want to read from file, just redirect it to standard input:
Code:
while read line;do

...

done < input_file
hi

thanks you

can i ask you more question?

if i have a file named: "aaa-bbb.dat"

and i want to seperate into two variables the words: "aaa" and "bbb"

how can i do it?

i tried to use

cut -f -d- "file"

however it doesnt work for me
 
Old 06-17-2012, 02:38 PM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Did you look at man cut or even cut --help?

In order to get output from cut, you need to specify the fields to output.

Example:
Code:
$ echo "abc-def.ghi" | cut -d- -f1
abc
$ echo "abc-def.ghi" | cut -d- -f2
def.ghi
$ echo "abc-def.ghi" | cut -d- -f2 | cut -d. -f1
def
$ echo "abc-def.ghi" | cut -d- -f2 | cut -d. -f2
ghi
$ echo "abc-def.ghi" | cut -d. -f1 | cut -d- -f1,2 --output-delimiter=" "
abc def

Last edited by PTrenholme; 06-17-2012 at 03:20 PM.
 
Old 06-17-2012, 02:45 PM   #5
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by PTrenholme View Post
Did you look at man cut or even cut --help?

In order to get output from cut, you need to specify the fields to output.

Example:
Code:
$ echo "abc-def.ghi" | cut -d- -f1
abc
$ echo "abc-def.ghi" | cut -d- -f2
def.ghi
$ echo "abc-def.ghi" | cut -d- -f2 | cut -d. -f1
def
$ echo "abc-def.ghi" | cut -d- -f2 | cut -d. -f2
ghi
1. i didnt understand this example "$ echo "abc-def.ghi" | cut -d- -f2 | cut -d. -f1" ,i didnt understand why do i need the middle cut? and what is the input to this cut?

2.how can i refer as input to cut the file-name?

p.s i read about cut here:

http://ss64.com/bash/cut.html
 
Old 06-17-2012, 03:45 PM   #6
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
Originally Posted by ofer4 View Post
1. i didnt understand this example "$ echo "abc-def.ghi" | cut -d- -f2 | cut -d. -f1" ,i didnt understand why do i need the middle cut? and what is the input to this cut?
The pipe (|) takes standard output of the command on the left and makes it the standard input to the command on the right. So, in the example, the echo created the input to the first cut, that first cut split the input into two parts, abc and def.ghi, and passed the def.ghi down the pipe to the second cut. Then the second cut split the def.ghi and printed the first part, the def.
Quote:
2.how can i refer as input to cut the file-name?
That, of course, depends on the script you are writing. If you are reading the file names from a file (As suggested by the question with which you started this thread.), something like this might be what you want:
Code:
#!/bin/bash
while read line
do
 for p in $(echo line | cut -d. -f1 | cut -d- -f1,2 --output-delimiter=" ")
 do
   echo ${p}
 done
done < input_file_name
(Where, of course, you would replace the echo ${p} with whatever you wanted to do with each part of the file names from the input file.)
Quote:
p.s i read about cut here:http://ss64.com/bash/cut.html

Last edited by PTrenholme; 06-17-2012 at 03:48 PM.
 
Old 06-17-2012, 03:56 PM   #7
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by PTrenholme View Post
The pipe (|) takes standard output of the command on the left and makes it the standard input to the command on the right. So, in the example, the echo created the input to the first cut, that first cut split the input into two parts, abc and def.ghi, and passed the def.ghi down the pipe to the second cut. Then the second cut split the def.ghi and printed the first part, the def.

That, of course, depends on the script you are writing. If you are reading the file names from a file (As suggested by the question with which you started this thread.), something like this might be what you want:
Code:
#!/bin/bash
while read line
do
 for p in $(echo line | cut -d. -f1 | cut -d- -f1,2 --output-delimiter=" ")
 do
   echo ${p}
 done
done < input_file_name
(Where, of course, you would replace the echo ${p} with whatever you wanted to do with each part of the file names from the input file.)
HI

first of all thanks you all for your help...

second, the solution that you suggested it is not solve my problem...

because the file locted in a library and their names are not locaed in a spesific file...

is there any solution for this problem?

thanks in advance
 
Old 06-17-2012, 04:52 PM   #8
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
Originally Posted by ofer4 View Post
HI

first of all thanks you all for your help...

second, the solution that you suggested it is not solve my problem...

because the file locted in a library and their names are not locaed in a spesific file...

is there any solution for this problem?

thanks in advance
Sure. In place of < input_file_name use something like < $(ls -1 folder*-*.*) where folder is the name of the library containing the files. To see what the input would be, just run the command inside the $() expression from a command line. (Note that the option is -"number one" not -"letter L".)
 
Old 06-17-2012, 11:15 PM   #9
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

To break a string (say, file name) into pieces and assign them to variables in one pass, one may use something like
Code:
$ fname="abc-def.ghi"
$ read a b c <<< ${fname//[-.]/ }
$ echo $a + $b + $c
abc + def + ghi
Here-string "<<<" and pattern substitution ${parameter/pattern/string} are used here to minimize the number of forks (additional processes) required.
 
  


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
C question: how to read a file line by line in kernel module conlonloi Programming 3 11-23-2011 12:04 AM
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
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 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

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

All times are GMT -5. The time now is 11: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