4

Is there a command either standalone or in vi or similar (can be gui) that will show me the numeric sequence of a string?

Input: The cat hopped in a box.

Output:

T h e   c a t   h o  p  p  e  d     i  n     a     b  o  x  .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
  • Not the exact output that you want but you can run strings into od -c to show numbered output. – slm Oct 30 '14 at 13:30
8

Here's a hybrid perl/fold approach:

$ echo "The cat hopped in a box." | fold -w 1 | 
     perl -lne 'push @k, "$_ "; push @l,sprintf "%-2s",$.; END{print "@k\n@l"}'
T  h  e     c  a  t     h  o  p  p  e  d     i  n     a     b  o  x  . 
1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

Explanation

  • fold -w 1: this will fold the input at a width of one character, resulting in each input character printed on a separate line.
  • perl -lne : the -l removes trailing newlines from the input and adds a newline to each print call; the n reads input line by line and the e provides the script to run on it.
  • push @k, " $_"; : Add a space to the the current line ($_) and save it in in the array @k.
  • push @l,sprintf "%-2s",$.; : sprintf will return a formatted string, here we are giving it the current line number ($.) and telling it to print it with spaces added as needed to make its length 2. The string is then added to the @l array.
  • END{print "@k\n@l"}' : once the whole file has been read, print the two arrays.

If you just need to number the characters and don't mind multi-line output, a simpler approach is (using a shorter string for brevity):

$ echo "foo bar" | fold -w1 | cat -n
 1  f
 2  o
 3  o
 4   
 5  b
 6  a
 7  r
  • I think the second approach is much better - once the input string goes over about 30 characters and starts to wrap, the one-per-line output ensures you still have the numbers and the letters aligned. The perl example would need some more work to interlace properly. – Floris Oct 30 '14 at 17:36
7

It's very simple to use the shell and basic tools:

$ echo "The cat hopped in a box." |
    (read a ; echo $a | sed 's/./&  /g' ; seq -ws" " 01 ${#a})
 T  h  e     c  a  t     h  o  p  p  e  d     i  n     a     b  o  x  .  
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

Explanation

The string is read into the $a variable. The ( ) will run the commands inside them in a subshell, needed to make the $a variable accessible. It is then passed to sed which replaces each character with itself followed by two spaces. Finally, the seq command prints a 0-padded list of numbers from 01 to the length of the $a string.

  • You don't need the subshell; the command work if you replace ( ) with { ;}. – G-Man Oct 30 '14 at 17:38
6

zsh:

$ text='The cat hopped in a box.'
$ chars=(${(s::)text})
$ print -aC$#chars $chars {1..$#chars}
T   h   e       c   a   t       h   o   p   p   e   d       i   n       a       b   o   x   .
1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  19  20  21  22  23  24
2

You can use perl:

$ echo The cat hopped in a box. | perl -nle '
    printf "%-3s", $_ for split //;
    printf "\n";
    printf "%-3s", $_ for 1..length'
T  h  e     c  a  t     h  o  p  p  e  d     i  n     a     b  o  x  .  
1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.