Unix C++
Would you like to react to this message? Create an account in a few clicks or log in to continue.

read from a file

3 posters

Go down

read from a file Empty read from a file

Post by akalya Mon Mar 08, 2010 6:23 pm

To read from stdin we use
read line
how do i read the contents of a file(passed as an argument to script) line by line....

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

Back to top Go down

read from a file Empty Re: read from a file

Post by akalya Mon Mar 08, 2010 6:29 pm

ok got it!!!
cat linenos | while read line
do
echo $line
done

linenos is the filename!!


Last edited by akalya on Mon Mar 08, 2010 6:37 pm; edited 1 time in total

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

Back to top Go down

read from a file Empty Re: read from a file

Post by Saradha Kannan Mon Mar 08, 2010 6:33 pm

create the input file as c.txt

ur script file will be
#!/bin/bash
clear
cat $1 | while read line
do
echo $line
done

and exec it as: sh <filename>.sh c.txt

I think this is what u asked for..

Saradha Kannan

Posts : 25
Points : 25
Join date : 2010-03-04

Back to top Go down

read from a file Empty Re: read from a file

Post by Maithreyi Mon Mar 08, 2010 6:39 pm

n=`wc -l < $1`
i=1
while [ "$i" -le "$n" ]
do
line=`cat $1 | head -$i | tail -1`
echo $line
i=`expr $i + 1`
done

This is a small script which reads line by line and prints it.

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia

Back to top Go down

read from a file Empty Re: read from a file

Post by akalya Mon Mar 08, 2010 7:07 pm

even this work:
while read line
do
echo $line
done < $1

where $1 is the name of the file to be read

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

Back to top Go down

read from a file Empty Re: read from a file

Post by Maithreyi Mon Mar 08, 2010 7:13 pm

@ akalya ! ya it does. I posted my message before seeing that ! Smile

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia

Back to top Go down

read from a file Empty Re: read from a file

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum