Do you mean line-by-line backwards, or character-by-character?Quote:> Is there a simple way to do this in a shell script? Korn, Bash, CSH,
> any will work for me.
>Do you mean line-by-line backwards, or character-by-character?
> >> Is there a simple way to do this in a shell script? Korn, Bash, CSH,
> >> any will work for me.
> >Do you mean line-by-line backwards, or character-by-character?
> line by line backwards.
>>> Is there a simple way to do this in a shell script? Korn, Bash, CSH,
>>> any will work for me.
>>Do you mean line-by-line backwards, or character-by-character?
> line by line backwards.
TAC(1) TAC(1)
NAME
tac - concatenate and print files in reverse
Later,
Ashok
--
Department of Microbiology-Immunology office: (312) 503-2524
303 E. Chicago Avenue, WARD 4-123 lab: (312) 503-2542
Northwestern University, Chicago, IL 60611 fax: (312) 503-1339
> Is there a simple way to do this in a shell script? Korn, Bash, CSH,
> any will work for me.
- matt
--
_______________________________________________________________________
<< Comments, views, and opinions are mine alone, not IBM's. >>
> > Is there a simple way to do this in a shell script? Korn, Bash, CSH,
> > any will work for me.
> How about `tail -r <filename>`. Is that simple enough?
> > > Is there a simple way to do this in a shell script? Korn, Bash, CSH,
> > > any will work for me.
> > How about `tail -r <filename>`. Is that simple enough?
> That's useful to have. Unfortunately not on HP UX though ):
cat -n /etc/rc.net |sort -r |sed 's/^[ ]*[0-9]* //'
^ ^^^^^
space tab
- Matt
--
_______________________________________________________________________
<< Comments, views, and opinions are mine alone, not IBM's. >>
> >> Is there a simple way to do this in a shell script? Korn, Bash, CSH,
> >> any will work for me.
> >Do you mean line-by-line backwards, or character-by-character?
> line by line backwards.
As a shell script, this should work (accepts stdin as well as files):
cat $* | {
IFS=""
read f
while read line
do
f="$line
$f"
done
echo "$f"
It will be quicker with awk:Quote:}
cat $* | awk '
BEGIN {getline f}
{f = $0 "\n" f}
END {print f}
'
--
Chris F.A. Johnson http://cfaj.freeshell.org
===================================================================
My code (if any) in this post is copyright 2001, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
> > > > Is there a simple way to do this in a shell script? Korn, Bash, CSH,
> > > > any will work for me.
> > > How about `tail -r <filename>`. Is that simple enough?
> > That's useful to have. Unfortunately not on HP UX though ):
> Ok, here is another option. Not sure how fast it is compared to
> other methods, but seems quick to me. Also doesn't require knowing
> the line number or parsing line by line.
> cat -n /etc/rc.net |sort -r |sed 's/^[ ]*[0-9]* //'
> ^ ^^^^^
> space tab
> > > > Is there a simple way to do this in a shell script? Korn, Bash, CSH,
> > > > any will work for me.
> > > How about `tail -r <filename>`. Is that simple enough?
> > That's useful to have. Unfortunately not on HP UX though ):
> Ok, here is another option. Not sure how fast it is compared to
> other methods, but seems quick to me. Also doesn't require knowing
> the line number or parsing line by line.
> cat -n /etc/rc.net |sort -r |sed 's/^[ ]*[0-9]* //'
> ^ ^^^^^
> space tab
awk '{lines[NR]=$0} END{for(x=NR;x>0;x--)print lines[x]}' filename
I compiled the following list of solutions to this question a while back.Quote:> >> Is there a simple way to do this in a shell script? Korn, Bash, CSH,
> >> any will work for me.
> >Do you mean line-by-line backwards, or character-by-character?
> line by line backwards.
These are roughly arranged from most to least portable:
# This method is POSIX.2 portable and works on large files, and is
# also faster than using awk or sed:
pr -nt | sort -nr | cut -f2-
# Most implementations of awk can handle bigger files than sed.
# Note: On HP-UX 11, awk aborts if the input file contains any lines
# longer than 3000 characters. Method A below is considerably faster
# than method B:
awk '{l[NR]=$0} END{for (i=NR;i;i--) print l[i]}' # method A
awk '{x=$0"\n"x} END{ORS="";print x}' # method B
# Just be aware that most seds will *on big files using the
# below methods (in older ones the limit can be as small as 4k,
# and even POSIX.2 requires only 8k). On HP-UX 11.00, sed
# dumps core on files >2k (or >4k w/ sed cumulative patch PHCO_22760
# installed). The same exact bug exists in Solaris 7. sed is also
# much slower than awk. Method A below is about 33% faster than
# method B or C:
sed -n 'x;1!H;${g;p;}' # method A
sed -n '1!G;h;$p' # method B
sed '1!G;h;$!d' # method C
# nl is part of XPG2-4, but not POSIX; works fine w/ large files, and it
# is very fast:
nl -ba -w9 | sort -nr | cut -f2-
# in HP-UX 10.00 and later, Perl 4 is included (/usr/contrib/bin/perl);
# Perl imposes no limit on the size of the input file, and it is extremely
# fast:
perl -e 'print reverse <>'
# tac is part of the GNU textutils package, which does not come w/ HP-UX,
# but should come with many versions of Linux & BSD, as well as Solaris 8;
# it is both the least portable, and speediest, way to perform the task:
tac
1. How to read lines backwards from a file?
What is the best way to read text lines backwards
from a text file? I am trying to avoid writing
some kind of buffer management support so that I
can seek forward and backward in a text file by line.
--
= P.K. Shiu ===========================================
2. Shuttle HOT-569/P-100 problem
3. Paging backwards in a file...? help
5. consistent read() on file while rename() file.new -> file?
6. Compile problem with g++ on Sparcstation IPX
7. Reading a directory and knowing which type of file am I reading
8. DELETE Key
9. [ak@suse.de: Re: iosched: impact of streaming read on read-many-files]
10. read() from driverfs files can read more bytes
11. ksh, read reads files funny
12. read from stdin while reading line by line a file
13. How to read from a .csv file (with read)?