I'd like to print the first 255 characters followed by entire line separated with a '^' character.
(Right now I'm testing with 10 characters at a time instead of 255.) For example, this works fine with one huge problem:
cat myfile.txt | sed -e 's:^\(.\{10\}\)\(.*\)$:\1^\1\2:'
The problem is that some lines are very short, in which case I want to print the entire line twice separated with '^' character.
For example:
1234567890987654321
123
should print as:
1234567890^1234567890987654321
123^123
I don't really need to use sed, but it seems that sed should be able to do this - but any one line command would be nice.
It only requires an almost trivial tweak to your sed
script — you want to print up to the first N characters of a line, followed by the caret and the whole line, so you specify a range with a number and a comma before the upper bound:
sed -e 's:^\(.\{1,10\}\)\(.*\)$:\1^\1\2:'
or (cutting down on the number of backslashes and remembered strings):
sed -e 's:^.\{0,10\}:&^&:'
This adds the caret to what were empty lines; the version with 1,10
leaves empty lines empty.
Incidentally, Mac OS X sed
requires a number before the comma; GNU sed
follows tradition and does not require it, and treats the leading missing number as 0. Portable code, therefore, will not write:
sed -e 's:^.\{,10\}:&^&:'
It will work with some, maybe most, but not all versions of sed
.