cat in the command line doesn't match cat in the script

Hello,

So I sorted my file as I was supposed to:

sort -n -r -k 2 -k 1 file1 | uniq > file2

and when I wrote

> cat file2

in the command line, I got what I was expecting, but in the script itself

 
...
sort -n -r -k 2 -k 1 averages | uniq > temp
cat file2

It wrote a whole different thing, why is that?

Thanks,
Shira.

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

LOL...joey, I had to log back in...you got to this before I could

It was a typo; in the second example I meant:

sort -n -r -k 2 -k 1 file1 | uniq > file2
cat file2

(I forgot to change the names of the files in this example. I changed the names, so it would be clear that these are files.)

sorry :slight_smile:

Please don't laugh at me. :o
Channel your enrgies to help me. :frowning:

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

sure, mate:

########### first #############

sort -n -r -k 2 -k 1 file1 | uniq > file2
cat file2

output:
246 12 egged
246 12 egged
468 -1 egged
246 12 egged
468 -1 egged

########## second ############

sort -n -r -k 2 -k 1 file1 | uniq > file2
#cat file2 => note that this command is disabled at the moment!

> cat file2
246 12 egged
468 -1 egged

# this should be the correct answer.

No hard feelings :slight_smile:

Hi System_Shock,

All my files are in the same folder.
I create them in the same folder, hence that's where there'll be, I guess...

I checked in the remote computer window in my SSH program, and they're in the same folder.

Thank you, though! :slight_smile:

The difference appears to be that the uniq command runs in one example, but not in the other.
Odd and perplexing....
must ponder a little more...

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:

file1:
------
246 12 egged
468 -1 egged
468 -1 egged

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?

#  cat file1
    246 12 egged
    468 -1 egged
    468 -1 egged

#  cat script.ksh
   sort -n -r -k2 -k1 file1 | uniq > file2
   cat file2
   
# ./script.ksh
    246 12 egged
    468 -1 egged

# 

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).

sort -n -r -k2 -k1 file1 | uniq -u -d > file2
cat file2

Shira, run a more on your file2.

I'm creating file1 and file2 in the same script.
although in order to create file1 I used a different script.
But this part works fine.

No...
Actually there are some, but they have no connection to file2. They only help me to create file1, and as I said, this file is okay.

I'm not sure what it is, perhaps I don't know the English term for this...?

Hey Nixnoob,

it's not working.
And you're not supposed to put -u and -d at the same time, it's sort of a rule.

and I tried everything you said...

more didn't work, but what's the difference between more and cat?

btw, before I replaced the cat with the more:

sort -n -r -k2 -k1 file1 | uniq -u -d > file2
cat file2

It didn't print out anything, as if file2 was empty...

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.

Happy hunting... I'm out.

OK, I fixed it!

It wasn't a syntax problem.

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.

The IOU coffee table:
--------------------------
Joey | 2
--------------------------
System_Shock | 1
--------------------------
nixnoob | 1
--------------------------

Shira. :slight_smile:

Way to go girl...figuring it out on your own makes it a lot sweeter in the end.

:b: :b: :b: :b: :b:

You don't need uniq as well as sort; use the -u option:

sort -u -n -r -k 2 -k 1 file1 > file2

Perhaps because you sorted a different file?

You sent the ouput to temp, but you printed file2.