Wednesday, October 26, 2022

[SOLVED] Shell separate line into multiple lines after every number

Issue

So I have a selection of text files all of which are on one line I need a way to seperate the line into multiple lines after every number.

At the minute I have something like this

a 111111b 222c 3d 444444

and I need a way to get it to this

a 11111
b 222
c 3
d 444444

I have been trying to create a gawk with regex but I'm not aware of a way to get this to work. (I am fairly new to shell)


Solution

Easy with sed.

$: cat file
a 51661b 99595c 65652d 51515

$: sed -E 's/([a-z] [0-9]+)\n*/\1\n/g' file
a 51661
b 99595
c 65652
d 51515

Pretty easy with awk.

$: awk '{ print gensub("([a-z] [0-9]+)\n*", "\\1\n", "g") }' file
a 51661
b 99595
c 65652
d 51515

Could even do with bash built-ins only...but don't...

while read -r line
do while [[ "$line" =~ [a-z]\ [0-9]+ ]]
   do printf "%s\n" "$BASH_REMATCH"
      line=${line#$BASH_REMATCH}
   done
done < file
a 51661
b 99595
c 65652
d 51515


Answered By - Paul Hodges
Answer Checked By - Mary Flores (WPSolving Volunteer)