I found it hard to find a description for the title of this thread
Here I am asking for pointers in the most efficient means of formatting and/or stripping lines of text so that the output contains only the information required, and none of the extra output from some query.
As an example of what I mean look at the output of fdisk -l.
It gives as output a lot of information about the disks attached to the PC.
Let us say for instance I only want to get the device node of the disks from that output and list them, with no other information, what would be the optimum method to do so?
To show that I have been doing some little homework on this let me show you the steps I took in my wanderings ..... and that is what they are - wanderings. Like all wanderings you get to your destination eventually, maybe by a scenic route, but most definitely not efficiently.
So first I decide I only want to see the line of the output with a dev node name in it (/dev/) so I pipe the fdisk output through grep like this
fdisk -l | grep 'Disk /dev/'
which gives the output
Disk /dev/sda: 250.1 GB, 250059350016 bytes
Disk /dev/sdb: 80.0 GB, 80000000000 bytes
Disk /dev/sdf: 32 MB, 32112640 bytes
My aim now is to get
as the only output. So I begin to strip out what I do not need
fdisk -l | grep '^Disk /dev/' | tr -d :
That will take out the ' :
' and the next one puts ' : ' back in in place of the spaces. (I now cannot remember why I wanted to get rid of the spaces
fdisk -l | grep '^Disk /dev/' | tr -d : | tr ' ' :
My output now looks like this
I had left the ' ,
' there thinking I would use it as I had intended originally to keep the disk size. So to get rid of the unwanted stuff I am now able to use the ' :
' as delimiters and get this as the full line ...
fdisk -l | grep '^Disk /dev/sd' | tr -d : | tr ' ' : | cut -d: -f2
Admittedly I got to my destination, but not I believe by any efficient means.
Hence my post here.
What would be some of the preferred methods of doing this?
To be honest there are so many different commands that could be used in conjunction with each other that it is difficult at first to find what might be best for any situation. ..... or it is for me at least
Having read back over what I have written it seems obvious to me what should be done (I think) if I knew how to do it. Maybe just look for the /dev* field and drop everything else .....then strip the ' :
' from them.
Your suggestions will be much appreciated.