how to read first 10 lines from a file?

how to read first 10 lines from a file?

Post by Rick Ng Chi W » Thu, 24 Jul 2003 10:57:03



Dear all,

I have a text file about 1000 lines.
How can I write a shell to read first 10 lines from this file??

Pls help.

Thanks!

 
 
 

how to read first 10 lines from a file?

Post by CoReKiL » Thu, 24 Jul 2003 11:08:51



Quote:> Dear all,

> I have a text file about 1000 lines.
> How can I write a shell to read first 10 lines from this file??

> Pls help.

> Thanks!

head -10 filename

that should do it

 
 
 

how to read first 10 lines from a file?

Post by Walt Fle » Thu, 24 Jul 2003 21:36:24


head is always not guaranteed to be in every unix installation, is it?
then do this:
typeset -i x
x=10
while [ ${x} -ne 0 ]
do
    read line < $file
    $x=$x-1
done



Quote:> Dear all,

> I have a text file about 1000 lines.
> How can I write a shell to read first 10 lines from this file??

> Pls help.

> Thanks!

 
 
 

how to read first 10 lines from a file?

Post by Barry Margoli » Fri, 25 Jul 2003 03:25:56




>head is always not guaranteed to be in every unix installation, is it?

It's been part of Unix for 20 years, don't know why it shouldn't be.

--

Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

how to read first 10 lines from a file?

Post by Jeremiah DeWitt Weine » Fri, 25 Jul 2003 03:25:58



> head is always not guaranteed to be in every unix installation, is it?

        I suppose not, but if you find one that doesn't have it, let
us know.  In Solaris 8, it's in the SUNWcsu package, and if you don't
have that, you also don't have chown, chmod, date, dd...etc.  In AIX
5.1, it's part of bos.rte.commands, and if you don't have that, you
don't have cat, find, grep, ls...etc.  In Slackware 8.1, it's in the
textutils package, and if you don't have that, you don't have cksum,
uniq, wc, or cat.  In OpenBSD 3.2, I think it's part of base.tgz, and
my Irix 6.5 box isn't up for me to check!  *whew!*  In short, I
suppose it's not _guaranteed_, but I'd be willing to lay money that
you'll find it on 100 out of the first 100 Unix hosts you look at.
 
 
 

how to read first 10 lines from a file?

Post by rakesh shar » Fri, 25 Jul 2003 06:24:43



Quote:> Dear all,

> I have a text file about 1000 lines.
> How can I write a shell to read first 10 lines from this file??

#1#
    head -10 inputfile

#2#
    sed -e '10q' inputfile

#3#
    nawk '1;10==NR{exit}' inputfile

#4#
    perl -lpe '$.==11&&exit' inputfile

#5#
    cat -n inputfile | egrep '^ *([1-9]|10)       '

#6#
   k=1
   while read line
   do
        printf "$line\n"
        k="`expr $k + 1`"
        [ "$k" -le 10 ] || exit
   done < inputfile

 
 
 

how to read first 10 lines from a file?

Post by Kevin Rodger » Fri, 25 Jul 2003 06:36:24



> #5#
>     cat -n inputfile | egrep '^ *([1-9]|10)       '

You need to pipe that into

        cut -d' ' -f2

(that's a tab between the single quotes, just like at the end of the egrep
regex), or use awk instead of egrep

        awk -F' ' '$1 ~ /^ *([1-9]|10)$/ {print $2}'

(that's also a tab as the -F option).

--
Kevin Rodgers

 
 
 

how to read first 10 lines from a file?

Post by Michael Wa » Fri, 25 Jul 2003 09:29:18




>head is always not guaranteed to be in every unix installation, is it?
>then do this:
>typeset -i x
>x=10
>while [ ${x} -ne 0 ]
>do
>    read line < $file
>    $x=$x-1
>done

Are you reading first 10 lines or read the first line 10 times?
--

 
 
 

how to read first 10 lines from a file?

Post by rakesh shar » Fri, 25 Jul 2003 11:01:54




> > #5#
> >     cat -n inputfile | egrep '^ *([1-9]|10)       '

> You need to pipe that into

>    cut -d' ' -f2

You are right!  I forgot to reconstruct the line back.
However, were there to be a TAB in the original line, then the above
won't do.
                   cut -d'   ' -f2-

Quote:> (that's a tab between the single quotes, just like at the end of the egrep
> regex), or use awk instead of egrep

>    awk -F' ' '$1 ~ /^ *([1-9]|10)$/ {print $2}'

> (that's also a tab as the -F option).

the awk line needs to be changed for the same reason.

How would one print field#2 onwards in awk and at the same time preserve
the whitespace between fields?

The following:

   nawk -F'\t' '{print $2-}' gives syntax errors,
and
   nawk -F'\t' '{$1="";print}' folds multiple whitespace.

 
 
 

how to read first 10 lines from a file?

Post by Peter S Tillie » Fri, 25 Jul 2003 12:05:25



> How would one print field#2 onwards in awk and at the same time
> preserve
> the whitespace between fields?

> The following:

>    nawk -F'\t' '{print $2-}' gives syntax errors,
> and
>    nawk -F'\t' '{$1="";print}' folds multiple whitespace.

Use substr() to slice $2 onwards out then print it.  (It's easy enough
to find where $2 starts in $0).
HTH
--
Peter S Tillier
"Who needs perl when you can write dc, sokoban,
arkanoid and an unlambda interpreter in sed?"