I need to use a regex in unix to make sure no line in a text document is over 70 characters, and I just cannot find the right expression. I've been trying:
sed "s/\(^.{70}\)/\1\n\r/g" firstMondayNoParas2.txt > firstMondayLined.txt
This expression isn't working though. What am I missing?
You need to escape also the curly braces.
sed "s/^\(.\{70\}\)/\1\n\r/"
Example:
$ echo 'foobar' | sed 's/^\(.\{3\}\)/\1\n\r/'
foo
bar
$ echo 'foobar' | sed 's/^.\{3\}/&\n\r/'
foo
bar
If you want to cut for every 70 chars then you may try like this,
sed 's/.\{3\}/&\n/g' file
Example:
$ echo 'foobarbuzbu' | sed 's/.\{3\}/&\n/g'
foo
bar
buz
bu
You could use perl
also.
$ echo 'foobarbuz' | perl -pe 's/(.{3})(?!$)/\1\n/g'
foo
bar
buz
(.{3})(?!$)
would capture each three chars but not the one which was present at the last. So this won't add a extra new line character at the last if your input has chars multiple of 3. To do an in-place edit, you must need to add -i
parameter like
perl -i -pe 's/(.{70})(?!$)/\1\n/g' file
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.