abernier
abernier

Reputation: 28228

piping and redirecting weird result

Considering 2 json files:

fileA.json:

{
  "foo": "hey",
  "bar": "ola"
}

fileB.json:

{
  "foo": "hoy"
}

, executing:

% cat fileA.json fileB.json | json

returns

{
  "foo": "hoy",
  "bar": "ola"
}

Ok

--

Now why when redirecting stdout to fileB.json I get:

% cat fileA.json fileB.json | json > fileB.json

I get:

{
  "foo": "hey",
  "bar": "ola"
}

Ie: fileA.json ???

PS: json utility is here: http://trentm.com/json

Upvotes: 0

Views: 22

Answers (1)

Thomas
Thomas

Reputation: 182063

The shell sets up output redirection, > fileB.json, so it opens and truncates fileB.json before cat has started reading from it. This causes cat to read an empty file. (It might even end up reading partially written output data.)

Never read from and write to the same file in a pipeline. Try something like > fileC.json instead.

Upvotes: 2

Related Questions