Question: OS: 14.04
sed: 4.2.4

I have multiple large files (2-4gb) that I want to perform some simple manipulations on. The entire file is in one line, which makes me wonder how to perform sed operations on it.

There are three things I want to do with each file:

1) Remove all `[` characters

2) Remove all `]` characters

3) Replace all occurrences of `},{` with `}{`.

So far I have tried `sed -e 's/},{/}{/g' file.json > file_new.json` with and without the `g` option, without any luck. I have also tried `sed -e 's/\[//g' file.json > file_new.json` without any luck. I only get a duplicate file.

Any ideas?

Answer: 
When I've got huge single-line files like that, for which the usual line-based tools won't work, I usually turn to: `tr`!

> 
> 1) Remove all [ characters  
> 
> 2) Remove all ] characters
> 

That's easy:

```
tr -d '[]' < file > strippedfile

```

(This might not work with a really, really old SysV version of tr, but it should be fine with any modern version.)

> 
> 3) Replace all occurrences of },{ with }{.
> 

That's trickier, because you care about context, so it's really a job for `sed`. One kludge I've used is to use `tr` to temporarily change some other character to a newline -- that is, to temporarily change the huge single-line file into a multi-line file -- then run `sed`, and finally change it back to a single-line file. Something like

```
tr '{' '\n' < file | sed 's/},$/}/' | tr '\n' '{' > newfile

```

This last works *only* if the original file contains *no* newlines. You could run through `tr -d '\n'` first to be sure.
Try this to place a newline at the end of the file:

```
echo "" >> file
sed 'whatever' file

```

Many UNIX tools will simply not recognize a file with no ending newlines as a text file and so will not operate on them so that MAY be your problem. If that doesn't work then edit your question to include a concise, testable example of your file.