For Programmers: Free Programming Magazines  


Home > Archive > AWK > November 2007 > tac replacement









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author tac replacement
yiyu.jgl@gmail.com

2007-11-26, 6:58 pm

I need to process the end of a file before the beginning. I have seen
some threads where a tac | awk ... | tac solution is proposed, but
there is always the problem of not having tac in your system. Well, I
found a tac replacement in sed (credit is not mine, I found it in some
gentoo forum, but didn't take note of the url). I thought it could be
right to share it with the group, just for the record:
sed -e '1!G;h;$!d'
Jürgen Kahrs

2007-11-26, 6:58 pm

yiyu.jgl@gmail.com schrieb:

> I need to process the end of a file before the beginning. I have seen
> some threads where a tac | awk ... | tac solution is proposed, but
> there is always the problem of not having tac in your system.


If you have a Unix sort command, then try this:

awk '{print NR, $0}' data_file.txt | sort -n -r | awk '{$1=""; print $0}'
Ed Morton

2007-11-26, 6:58 pm



On 11/26/2007 3:16 PM, J=FCrgen Kahrs wrote:
> yiyu.jgl@gmail.com schrieb:
>=20
>=20
>=20
>=20
> If you have a Unix sort command, then try this:
>=20
> awk '{print NR, $0}' data_file.txt | sort -n -r | awk '{$1=3D""; prin=

t $0}'

That'll add a leading space and compress the rest of the spacing between =
fields.
If you really wanted to do that in awk|sort|awk, you'd do:

awk '{print NR, $0}' data_file.txt | sort -n -r | awk 'sub(/[[:digit:]]+ =
/,"")'

There are of course better UNIX tac-alternatives but that'd be best discu=
ssed in
comp.unix.shell....

Ed.

yiyu.jgl@gmail.com

2007-11-26, 6:58 pm

I also found this solution in awk, it is Charles Demas from 1999 in
this same newsgroup, but it could be interesting to remember it here,
just in case somebody is trying to get it:

With awk/gawk you could do it like this:

gawk '{a[NR]=$0}END{for(i=NR;i>0;i--){print a[i]}}' infile

And I'd like to tell that probably Ed Morton is right and this
discussion should be in comp.unix.shell, so with the awk solution I
think the thread should end here, sorry for the noise (anyway, I hope
having this information compiled here will be useful to anyone).

--
yy.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com