Operating System - HP-UX
1846992 Members
3536 Online
110257 Solutions
New Discussion

Re: Simple Script Problem - reordering

 
SOLVED
Go to solution
Russ Hancock_1
Frequent Advisor

Simple Script Problem - reordering

Does anyone know how I can reorder a file, so the first line of the input file becomes the last line of the output file, and so on through out the input file...

I don't want it sorted, just reversed...

Thanks in advance.
Russ.
Russ
21 REPLIES 21
U.SivaKumar_2
Honored Contributor
Solution

Re: Simple Script Problem - reordering

Hi,
If you know total number of lines of that file
you can do this for file of 900 lines.
#tail -n 900 myoriginalfile > invertedfile

#man tail for more details.

regards,
U.SivaKumar
Innovations are made when conventions are broken
Steve Steel
Honored Contributor

Re: Simple Script Problem - reordering

Hi

nl -ba -s"~" file|sort -r|cut -f2-256 -d"~"


Use umlaut or something else for field definition see man


steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Ralph Grothe
Honored Contributor

Re: Simple Script Problem - reordering

Hi Russ,

this is yet another task for Perl ;-)

But beware, this is a resource greedy solution as it requires the size of your file from memory stack, since we store the whole file in an array first.
So you shouldn't do this with really big files:

perl -e '@tmp = reverse <>;print @tmp' /path/to/your/file


Unfortunately, if you have big files, something more elaborate is needed, which I imagine would require seek().

HTH
Ralph
Madness, thy name is system administration
Russ Hancock_1
Frequent Advisor

Re: Simple Script Problem - reordering

Here's what I did in the end.
Thanks All.

INPUT=$1
OUTPUT=$2

rm -f $OUTPUT

LINES=`wc -l $INPUT|awk '{print $1}'`

i=0

while test i -lt $LINES
do
i=`expr $i + 1`
echo " $i \c"
tail -n $i $INPUT | head -n 1 | tee -a $OUTPUT
done
Russ
john korterman
Honored Contributor

Re: Simple Script Problem - reordering

Hi,
you could try this rather simple solution:

#!/usr/bin/sh

# reverse order of inputlines in $1

PREVIOUS_FILE=/tmp/prev
if [ -w $PREVIOUS_FILE ]
then
rm $PREVIOUS_FILE
fi
touch $PREVIOUS_FILE

CURRENT_FILE=/tmp/curr
if [ -w $CURRENT_FILE ]
then
rm $CURRENT_FILE
fi
touch $CURRENT_FILE

cat $1| while read CURRENT
do
echo $CURRENT > $CURRENT_FILE
cat $PREVIOUS_FILE >> $CURRENT_FILE
cat $CURRENT_FILE > $PREVIOUS_FILE
done
cat $CURRENT_FILE



regards,

John K.
it would be nice if you always got a second chance
V. V. Ravi Kumar_1
Respected Contributor

Re: Simple Script Problem - reordering

hi,

try this
cat -n |sort -r|cut -f2 >

regds
Never Say No
H.Merijn Brand (procura
Honored Contributor

Re: Simple Script Problem - reordering

When using perl, you can use http://search.cpan.org/search?dist=File-ReadBackwards which is not that resource greedy
Enjoy, Have FUN! H.Merijn
Peter Godron
Honored Contributor

Re: Simple Script Problem - reordering

Russ,
you could also try:
grep -n ^ input-file | sort -kr1n,1n | awk -F':' '{print $2}'
Aashish Raj
Valued Contributor

Re: Simple Script Problem - reordering

#! /usr/bin/ksh
i=1
cat file.txt|while read xx
do
buf[$i]=`echo $xx`
i=$((i+1))
done
v=$((i-1))
while true
do
if [ $v -ge 1 ];then
echo ${buf[$v]}
v=$((v-1))
else
exit
fi
done

Thanks
AR
James R. Ferguson
Acclaimed Contributor

Re: Simple Script Problem - reordering

Hi Russ:

Here's one way:

# awk '{array [NR]=$0}; END{for (i=NR;i>0;i--) print array [i]}' inputfile > outputfile

Regards!

...JRF...
Reinhard Burger
Frequent Advisor

Re: Simple Script Problem - reordering

HI
I would prefer the solution f U.Sivakumar
because it follows my most important approach : Keep it simple

just use:
tail -10000000000 oldfile > newfile

it's a silly way just to use a very very big number for tail but IT WORKS and you get what you want.
As long as your file does not have that big amount of lines you will get the file "oldfile" listed into "newfile" in reverse order.

keep it simple
Scott Williams_5
Frequent Advisor

Re: Simple Script Problem - reordering

Hi Russ,

Try this:

ed yourfile <g/^/m0
w
EOD
Ralph Grothe
Honored Contributor

Re: Simple Script Problem - reordering

Hey procura,

thanks for the hint towards File::ReadBackwards module from CPAN

http://search.cpan.org/doc/URI/File-ReadBackwards-0.97/ReadBackwards.pm

Haven't stumbled yet over this one.
Of course this is *much* better than what I suggested as a quick one-liner.

I will definitely install this one.
It's always worth to dig the CPAN first

:-)
Madness, thy name is system administration
Shannon Petry
Honored Contributor

Re: Simple Script Problem - reordering

For you two who said to use the tail command to do this, I have a suggestion for you. Know what you are saying before giving answers on the forums!

Tail DOES NOT reverse order display of files, simply takes the last "n" lines of the file to stdout.

I.E. (yes I clipped the last two lines of cat)
cat /etc/hosts
127.0.0.1 localhost
10.37.161.12 sh0su012 tr0su010 loghost

tail -2 /etc/hosts
127.0.0.1 localhost
10.37.161.12 sh0su012 tr0su010 loghost


Doh, they are identical....
Look at the other suggestions for your answers and ignore the suggestions of using the tail command.

Regards,
Shannon
Microsoft. When do you want a virus today?
Rodney Hills
Honored Contributor

Re: Simple Script Problem - reordering

Also "tail" does have a limitation on how many lines it can process.

If you say tail -1000000, tail does a sequential read and has to keep the last 1000000 lines in memory. Sometime tail doesn't allocate enough memory, and I've seen where it just drops off the remaining lines.

My 2 cents...

-- Rod Hills
There be dragons...
Shannon Petry
Honored Contributor

Re: Simple Script Problem - reordering

Sadly the guy who said tail for this got 9 points and a magic answer....guess the user didnt test it, just had faith.. ;(
Microsoft. When do you want a virus today?
Reinhard Burger
Frequent Advisor

Re: Simple Script Problem - reordering

Ok Guys
You got me !!!
Next time i'm hopefully a little bit mor carefull
:;-)
keep it simple
Suhas_2
Regular Advisor

Re: Simple Script Problem - reordering

Hi Guys,
Here are my 2 cens.
On Solaris, IBM-AIX and DIGITAL a mere
"tail -r /path/to/file/filename"
would do the job. On HP-UX "-r" option is not available.

Rgds..
Suhas.
Never say "Die"
U.SivaKumar_2
Honored Contributor

Re: Simple Script Problem - reordering

Hi Shanon,
I do agree that. I posted a theory that i have not tried practically. I regret , If i have hurted you all.

regards,
U.SivaKumar
Innovations are made when conventions are broken
U.SivaKumar_2
Honored Contributor

Re: Simple Script Problem - reordering

And shannon,

"For you two who said to use the tail command to do this, I have a suggestion for you. Know what you are saying before giving answers on the forums!"

To err is human, You can be more polite in pointing out other's mistake.

regards,
U.SivaKumar

Innovations are made when conventions are broken
Russ Hancock_1
Frequent Advisor

Re: Simple Script Problem - reordering

Thanks Guys, Enough already!

Shannon, maybe 9 points was excessive for the tail command, but it did point me to the solution of using tail to pick the last line and then looping backup one line at a time.. See my reply earlier.

So tail didn't work as suggested... but it did prove an answer.... and that what mattered to me.

Russ