Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

extract nth number in a string using bash commands

running the following command I get 0271, is there a way to get 0, 27, and 1 separately?

echo '!    ibrav = 0, nat = 27, ntyp = 1' | sed -r 's/[^1-9]*//g'

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Use grep instead of sed. The -o option prints just the matching parts, and each match is on a separate line.

echo '!    ibrav = 0, nat = 27, ntyp = 1' | grep -E -o '[0-9]+'

Output:

0
27
1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading