Operating System - HP-UX
1831661 Members
2070 Online
110029 Solutions
New Discussion

Unix reading lines from a file

 
SOLVED
Go to solution
supriyo banerjee
New Member

Unix reading lines from a file

Hi i am newbie to unix.

I am trying to read a single line from a file and i am using the following code:

#!/bin/ksh

i=1

if [ $i -le 45 ]
then
sed -n ''$i','$ip'' a.dat>>c.dat

i=i+1

fi


Now somehow i am missing out the sed options.

My basic purpose of these steps is to merge the content of 30 files whose locations are in the file a.dat. Can some one chip in with some help
9 REPLIES 9
James R. Ferguson
Acclaimed Contributor

Re: Unix reading lines from a file

Hi:

# i=1; f=45
# sed -n "$i","$f"p a.dat > c.dat

...will write lines 1-45 of a.dat into c.dat

Regards!

...JRF...
supriyo banerjee
New Member

Re: Unix reading lines from a file

Hi jrf
Thanks...


Actually my fault i confused you with the post. The code which i have written is just to check if I am able to get the lines one by one written into c.dat...what i am looking ideally is for something which picks out each line one by one from a file.
James R. Ferguson
Acclaimed Contributor

Re: Unix reading lines from a file

Hi (again):

If all you want to do is read lines from a file, the shell can do this easily:

#!/usr/bin/sh
typeset -i LINENBR
while read LINE
do
(( LINENBR=LINENBR+1 ))
if [ "${LINENBR}" -le 45 ]; then
echo ${LINE} >> c.dat
fi
done < a.dat
exit 0

Regards!

...JRF...
supriyo banerjee
New Member

Re: Unix reading lines from a file

I know this way thanks!

I am trying to do this using sed the stream editor...
Peter Nikitka
Honored Contributor

Re: Unix reading lines from a file

Hi,

- to get exactly one specific line out of a file, I use
li=45
sed $li!d filename

- to walk through a file line by line (my example puts the line number as a prefix):
typeset -i num=0
while read li
do print L$((num+=1)):$li
done
mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
James R. Ferguson
Acclaimed Contributor

Re: Unix reading lines from a file

Hi:

> I am trying to do this using sed the stream editor...

That's wasteful if all that you are doing is using 'sed' to specify a line number and the line numbers represent a continuum. You are forcing 'sed' to read the input file n-times!

Hoever, your original code should have been:

# i=1;f=45;sed -n "$i","$f"p a.dat

...or for any line "i":

# sed -n "$i"p filein >> fileout

Regards!

...JRF...
supriyo banerjee
New Member

Re: Unix reading lines from a file

Thanks i got it done...
Peter Nikitka
Honored Contributor
Solution

Re: Unix reading lines from a file

Hi,

to respond to your overlapping post:

> I am trying to do this using sed the stream editor...

I wonder, why one should do it that way, but ...

typeset -i num=45 i=0
while [ i -le num ]
do
sed -e $((i+=1))!d -e q filename >c.dat
done

The 'q' command terminates the sed as a speedup on large files.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
James R. Ferguson
Acclaimed Contributor

Re: Unix reading lines from a file

Hi:

I see too, that you are a new member in our community. Welcome! Please see:

http://forums1.itrc.hp.com/service/forums/helptips.do?#28

Regards!

...JRF...