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

Awk how to print the next line of a specific expression but only of it contains a number

Here is a test.txt file

sometext
someothertext
...
lol
74
hhhhh
lol
789AA

I want a bash command that only displays:

74

For this I have tried:

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

awk '$0 == "lol" {i=1;next};i && i++ <= 1' test.txt

which for now displays

74
789AA

So, how can I augment this command so that it only displays a line that only contains a number (74) ?

Thanks

>Solution :

This awk should work for you:

awk 'p == "lol" && $0+0 == $0; {p = $0}' file

74

$0+0 == $0 will return true only when $0 contains a number only.

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