Separate numbers, strings from one line using bash

18,678

Solution 1

GNU grep or compatible solution:

s="string123anotherstr456thenanotherstr789"
grep -Eo '[[:alpha:]]+|[0-9]+' <<<"$s"
  • [[:alpha:]]+|[0-9]+ - regex alternation group, matches either alphabetic character(s) or number(s); both will be considered as separate entries on output

The output:

string
123
anotherstr
456
thenanotherstr
789

Solution 2

POSIXly:

string=string123anotherstr456thenanotherstr789
sed '
  s/[^[:alnum:]]//g; # remove anything other than letters and numbers
  s/[[:alpha:]]\{1,\}/&\
/g; # insert a newline after each sequence of letters
  s/[0-9]\{1,\}/&\
/g; # same for digits
  s/\n$//; # remove a trailing newline if any' << EOF
$string
EOF

Solution 3

GNU sed (or compatible) solution:

s="string123anotherstr456thenanotherstr789"
sed 's/[a-zA-Z]*\|[0-9]*/&\n/g; s/\n$//' <<<"$s"

The output:

string
123
anotherstr
456
thenanotherstr
789

Solution 4

python3

python3 -c '
from itertools import groupby
s = ("".join(g) for k, g in 
    groupby("string123anotherstr456thenanotherstr789", lambda x: x.isalpha()))
print(*s, sep="\n")
'

string
123
anotherstr
456
thenanotherstr
789

Solution 5

Used below one liner to achieve the same. As tested its worked fine

sed "s/[0-9]\{3\}/\n&/g" filename | sed "s/[0-9]\{3\}/&\n/g"| sed '/^$/d'

output

string
123
anotherstr
456
thenanotherstr
789
Share:
18,678

Related videos on Youtube

HUY
Author by

HUY

Updated on September 18, 2022

Comments

  • HUY
    HUY over 1 year

    How to separate strings and numbers from one line using a bash command.

    Example: I have a string containing

    string123anotherstr456thenanotherstr789

    The output should be:

    string
    123
    anotherstr
    456
    thenanotherstr
    789
    
  • G-Man Says 'Reinstate Monica'
    G-Man Says 'Reinstate Monica' over 6 years
    @wjandrea:  The OP says (in a comment) “consider it is in a file”.
  • G-Man Says 'Reinstate Monica'
    G-Man Says 'Reinstate Monica' over 6 years
    It seems to me that you might as well say that the answer is printf 'string\n123\nanotherstr\n456\nthenanotherstr\n789\n'. I admit that the question is unclear, in the sense that it is incomplete, but the string presented in the question is clearly labeled as an example. I see no reason to assume that every number that will ever be in the input will be a three-digit number.
  • G-Man Says 'Reinstate Monica'
    G-Man Says 'Reinstate Monica' over 6 years
    (1) Gotcha!  (2) Why do you believe that the OP wants non-alphanumeric characters to be ignored?
  • Stéphane Chazelas
    Stéphane Chazelas over 6 years
    @G-Man, no good reason other than that's what the accepted answer does. It still differs from the accepted answer in that it will print an empty line for each line of input that contains neither letters nor digits.
  • ilkkachu
    ilkkachu over 6 years
    I think you could combine the first two seds and add both newlines at one go s/.../\n&\n/g. I also don't see why insist on the numbers being exactly three digits long, since it's trivial to make a more general solution.
  • G-Man Says 'Reinstate Monica'
    G-Man Says 'Reinstate Monica' over 6 years
    An additional (IMHO, more significant) difference: the accepted answer treats non-alphanumeric characters as word separators, and then discards them, so 3.14 becomes two output lines (3 and 14) and G-Man@stack becomes three (G, Man and stack). Your answer ignores them, so those inputs yield one output each (314 and GManstack).