DStage
DStage

Reputation: 1

Removing a newline from the middle of a row of data

I have tried the following which have not worked. The file has no field delimiters. It is fixed width. I do not care if I use awk or sed. The newline appears to be in column 50. I have also tried column 49. Any help would be greatly appreciated.

awk '{ gsub(/\n,"", $50); print } ' newsqtp160 > UPD_newsqtp16  
awk '{ gsub(\n,"", $50); print } ' newsqtp160 > UPD_newsqtp16
awk '{gsub(/\\n/, "", $49); print}' newsqtp160 > UPD_newsqtp16
sed 's/\n//50' newsqtp160 > UPD_newsqtp16

Upvotes: 0

Views: 111

Answers (1)

Kent
Kent

Reputation: 195209

You can think about this problem in another perspective instead of replacing the new line using gsub or sed's s/.../.

You have already told us that your file has fixed width lines, if there is a broken line, it's length should be shorter than the "fixed" width, no matter the linebreak sits in 50th or 49th column. So you can just do:

awk -v n="FIXED_LENGTH" '{s=s $0}length(s)==n{print s;s=""}' file

Basically, the above line does:

If a line has length == fixed width, print it out, otherwise, join with next line, and next line, and next... till it reaches the fixed width.

Test

Let's see a small test:

Here the fixed_width is "4".

kent$  cat f
1234
1234
12
34
1234
1
2
3
4
1234

kent$  awk -v n="4" '{s=s $0}length(s)==n{print s;s=""}' f
1234
1234
1234
1234
1234
1234

Upvotes: 1

Related Questions