Do you realize that you are referring to different filenames in you 2nd example?
Your 1st example file1 > file2, then cat file2
Your 2nd example average > temp, then cat file2
Oh no...I wasn't laughing at you...I thought perhaps you made a typo but I was too slow to reply to this thread and joey pointed out what we saw was obvious.
Could you post an example of what you see as different between the script output and the command line output?
... without looking at actual script and outputs you are getting, we can only guess. My guess is that you are running the script from a location different to the location where the targeted file1 is.
If, say, file1 is in /tmp, and you manually cd to /tmp and run your commands, you will get the expected result. But if your script is in, say, /home/myhome, if you don't give the script a full path to where the right file1 is and where file2 will be created, you will get something else
But you know what's strange?
That the original file doesn't even look like that!
The cat command adds and doubles lines!
The original file looks like this:
It works fine for me, so there's more to it.
Are you writing this script in one box and moving it to another which could have a different version of sort?
Any variables/paths you are setting in the script?
Are you doing this through a serial console?
I had to add -u -d to the end of the uniq command to get it to work the way shira wants it to.
I think this may be a simple syntax issue, no spaces between -k and the numbers, add -u -d to the uniq command (that is supposed to be the default for the uniq command with no flags).
Yeah, it's not supposed to even be necessary...-u and -d are the defaults if you just execute uniq.
More just prints the output 1 page at a time where cat prints every thing to the output at once. I was thinking maybe cat was doing something it shouldn't be...it was just a random thought.
If you do an ls -la on file2 it will probably show the size as zero...that means the command errored and didn't actually create a readable file.
Now I'm just puzzled...I tried your original script and my way on a newly imaged HP-UX 11.23 server and I got the exact output I wanted.
*EDIT* Shira, as System Shock suggests, could you post the entire script as it is on your system?
This thread is going to go on forever with everyone trying to guess what's wrong until someone somehow hits it by chance. As shown in my last post, the syntax works.
My last suggestion is to post the entire script here and the location of all files including paths.
This is what I did:
I have a main script which creates a file. This file needs to create another file which is being "built" in a secondary script.
What I did, is that I wrote the "cat" part in the secondary script, and it didn't work, because the file was still in the making process.
Of course that I needed to cat, sort and uniq the file in the main script, and over there to create "temp" which is my final output file.
This is why the cat command worked so well in the command line - because it printed out "temp" after all the process, whilst the cat command in the secondary script printed "temp" in the middle of the making.
I thank you both for your patience - System_Shock, you were really close! :), and nixnoob - you're awfully nice, I'm sorry I puzzled you.
Joey - I'm sorry I puzzled you as well.