[SOLVED] Extract specific words from a line by using Shell
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I hope someone here can help me. I have a line in a text file looking like this:
Disk /dev/mmcblk0: 15.7 GB, 15737028608 bytes, 30736384 sectors
I want to extract only the Size of mmcblk0. I would prefer the solution being bash using sed or grep.
Ok..we'll be glad to HELP you, so what have you done/tried to do this?? Read the "Question Guidelines" in my posting signature...asking us to do this for you is fairly rude, as is even specifying what you want it done IN.
To start, read the man page on grep, then read the man page on either awk or cut. You can build your own solution from those.
I haven't used sed much, but curious if this would this work?
Code:
fdisk -l | sed -n '/mmcblk0:/,/,/p'
You don't have to be curious, try and see what you get? The only thing would be to first check the output of the fdisk command as you may not have an entry with 'mmcblk0' in it
@OP - your current solution assumes you already know the length of the string which refers to your size information, so you might get returned ' 15.7', but this gives you no real
idea of the size as there is no measurement, ie. MB,GB,TB ...
Knowing the format of the data I would use sed or awk as a quick and easy way to get the detail:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.