Help on cat with sed!

Hi All!

I have a script having one statement like.

cat xfile | sed 's/A/a/g' > xfile

I have two boxes which have similar version of linux running but one is greater in speed compared to other. Lets say A is faster than B.

A which is faster writes output as NULL
B which is slower comparatively to A writes the entire content with A replaced to a
. Though I know its not a good practice. I want to know why is this behavior. Is there some delay writing in one compared to the other ? I remmeber comming across this jargon in buffer cache

Thanks
Balaji Kamal Kannadassan

What do you mean 'writes output as NULL' - no output all ?

If you are asking 'why do I get different output for the same command on the same data?'

The answer lies in how the environment is set up. As a simple example, there may be alias settings which change the behavior of commands for example.

Unusual results can occur when input and output filenames are the same. Most of the time that I have seen, your command line will result in xfile being an empty file. Try again on both machines, but put output to to xfile2.

joeyg

thanks for the response, but the problem is irrespective of any number of tries both shows such a difference in behavior. Is there anyway this behavior can be identified i.e the reason for this behavior.

If the output had been different then the story is different.
But what you are saying is that there is no output in one of them.
The reason could be any thing.
It will be easier if you post the data also.

  1. While moving the code you may have missed to move it as binary?
    Check if the input has CR/LF (like DOS file).<-- I would bet on this.
    Check if the code has CR/LF (like DOS file).

  2. There could be some issues with the environment variables.
    For example (this may or may not apply to your case):
    If your input is delimited with tab, then IFS is important.
    If the variable is messed up then you will have issues.

  3. There may be permission issues.
    Or "tmp" directory is filled up or quota is not enough.

  4. May be the version of SED are different.
    "whence" command and file date and size should confirm that.

  5. Run your code with the trace.
    ksh -x myscript.sh
    Add a few echo's and see what is happening.

  6. Run the same command at the prompt and see if it works.

Just for the heck of it, can you post the input data also?

Ok edidata to be more specific and clear cat x | sed 's/A/a/g' > x. Where x is say a file then the output in one case is empty i.e file x is now empty even the old content is gone. In another case x which had some text with 'A' has replace with 'a' and is written into file. BTW checked sed version cat version linux kernel version all are same in the box only difference is one is faster than the other in speed.

Oh.. boy!
The problem has nothing to do with the server...:slight_smile:
It is with your code.
This is wrong:

cat x | sed 's/A/a/g' > x

It should read as:

cat infile | sed 's/A/a/g' > outfile

The input file name and the output file name CANNOT be the same.
That is why you are getting 0 byte file.