Question: We are using flat text files to store in teradata table after transformations from Informatica. The file contains 14 fields (~ separated). But as the records are getting scattered over multiple lines informatica is not able to pick it up.

Is there's any way that we join the record by counting the delimiter using sed/awk or any other command?

Sample record---

```
48602040112~4100010080701242015~2010-01-21 10:23:44~Foods~7~Poultry ~Perdue Smart Chicken~Circular~06
-JAN-10~24-JAN-10~$5.99~24 oz., select varieties
up to 4 at this price, additional
Save up to $4.00 
load up on savings~~1598

```

Ideally it should be like --

```
48602040112~4100010080701242015~2010-01-21 10:23:44~Foods~7~Poultry ~ Perdue Smart Chicken~Circular~06-JAN-10~24-JAN-10~$5.99~24 oz., select varieties up to 4 at this price, additional Save up to $4.00 load up on savings~~1598

```

If it's not obvious, it contains new line characters.

Answer: 
```
tr -d \\n <infile | tr \~ \\n | paste -d~ - - - - - - - - - - - - - -

```

That will work.
You could try using `sed` :

```
sed -rn ':a;/^([^~]*~){13}[^~]*$/!{N;s/\n//;ba};p' yourfile.tsv

```

**What it does**

The script has three parts separated by a `;`:

* `:a` defines a label we can branch to
* `/^([^~]*~){13}[^~]*$/!{N;s/\n//;ba}` searches for a complete column
	+ `/^([^~]*~){13}[^~]*$/` looks for a line with exactly 14 fields (0 or more occurrences of anything that is not a `~`), separated by 13 occurrences of `~`.
	+ `!` Inverts the search result (if not found, then...)
	+ `{N;s/\n//;ba}` the block that is executed if the column is not complete
		- `N` reads in another line
		- `s/\n//` removes the newline between the two lines
		- `ba` branches (b) to our previously defined label (a)
* `p` prints the complete column