user1641496
user1641496

Reputation: 457

Add new line using awk, sed

I have a large file which is slightly corrupted. The new lines have disappeared. There should have been a new line at every 250th character. How can I fix that?

Thanks in advance.

Upvotes: 1

Views: 2992

Answers (5)

Thor
Thor

Reputation: 47229

There is a command in coreutils that can wrap lines, it is called fold:

fold -w 250

Upvotes: 4

Jotne
Jotne

Reputation: 41460

An awk version

awk '{L=250;for (i=1;i<=length($0);i+=L) print substr($0,i,L)}'

Upvotes: 2

Kent
Kent

Reputation: 195269

try this:

sed -r 's/.{250}/&\n/g'

gawk:

awk -v FPAT='.{1,25}' -v OFS='\n' '$1=$1'

Upvotes: 4

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed 's/^.\{250\}/&\
/;P;D' YourFile

Could be faster on huge file

Upvotes: 2

Tom Fenech
Tom Fenech

Reputation: 74705

How about

sed 's/.\{250\}/&\n/g'

The .\{250\} captures 250 of any type of character. The characters are replaced by themselves, plus a newline.

Upvotes: 5

Related Questions