POSIX: abcdef to ab bc cd de ef

Using same routine, it can be done in bash itself:

s='abcd 10001.'

for((i=0; i<${#s}-1; i++)); do echo "${s:i:2}"; done
ab
bc
cd
d
 1
10
00
00
01
1.

With GNU awk could you please try following. Written and tested with shown samples and tested it in link https://ideone.com/qahp0S

awk '
BEGIN{
  FS=""
}
{
  for(i=1;i<=(NF-1);i++){
    print $i$(i+1)
  }
}
' Input_file

Explanation: setting field separator as NULL in the BEGIN section of program for all lines here. Then in main program running a for loop which runs from 1st field to till 2nd last field. In that loop's each iteration printing current and next field.


Try:

$ echo "abcd 10001." | awk '{for(i=1;i<length($0);i++) print substr($0,i,2)}'
ab
bc
cd
d 
 1
10
00
00
01
1.

You may use

sed --posix -e 's/./&\
&/g' example.txt | sed '1d;$d'

The first sed command finds every char in the string and replaces with the same char, then a newline and then the same char again. Since it replaces first and last chars, the first and last resulting lines must be removed, which is achieved with sed '1d;$d'.

Had sed supported lookarounds, one could have used (?!^).(?!$) (any char but not at the start or end of string) and the last sed command would not have been necessary, but it is not possible with sed. You could use it in perl though, perl -pe 's/(?!^).(?!$)/$&\n$&/g' example.txt (see demo online, $& in the RHS is the same as & placeholder in sed, the whole match value).

Tags:

Shell

Awk

Posix

Sed