Author Topic: [SOLVED] Formatting / Filtering Text  (Read 867 times)

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6849
  • MLU
[SOLVED] Formatting / Filtering Text
« on: July 23, 2010, 08:51:47 AM »
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.  ;D

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

Code: [Select]
fdisk -l   | grep 'Disk /dev/'  which gives the output
Code: [Select]
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
Code: [Select]
/dev/sda
/dev/sdb
/dev/sdf
as the only output. So I begin to strip out what I do not need

Code: [Select]
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  :D )
Code: [Select]
fdisk -l   | grep '^Disk /dev/'  | tr -d : | tr  ' ' :
My output now looks like this
Code: [Select]
Disk:/dev/sda:250.1:GB,:250059350016:bytes
Disk:/dev/sdb:80.0:GB,:80000000000:bytes
Disk:/dev/sdf:32:MB,:32112640:bytes

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 ...

Code: [Select]
fdisk -l  | grep '^Disk /dev/sd' | tr -d : | tr  ' ' : | cut -d: -f2
/dev/sda
/dev/sdb
/dev/sdf

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   :D

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.

Thanks.
« Last Edit: July 24, 2010, 04:52:10 AM by JohnBoy »

Online tschommer

  • PCLinuxOS Tester
  • Hero Member
  • *******
  • Posts: 1904
  • MLU and BLU (Bacon lovin' user)
Re: Formatting Text
« Reply #1 on: July 23, 2010, 08:59:00 AM »
JohnBoy, if you try awk it should be easier:

Code: [Select]
fdisk -l   | grep '^Disk /dev/'  | awk '{print $2}'
In this case awk simply takes the output and prints the second part, separated by a space (the default separator).

HTH
Torsten
What country can preserve its liberties if its rulers are not warned from time to time that their people preserve the spirit of resistance?
Thomas Jefferson

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6849
  • MLU
Re: Formatting Text
« Reply #2 on: July 23, 2010, 09:03:51 AM »
Thank you!
That does leave the ' : ' which presumable have to be removed separately?
Code: [Select]
fdisk -l   | grep '^Disk /dev/'  | awk '{print $2}' | cut -d: -f1Awk & sed I have so far avoided ....  just too deep for my present abilities, but I will begin by remembering that simple application of it.  ;)

I was coming back to say that looking at what I went through just shows how one cannot see the wood for the trees .....  and immediately came up with the following .......

Code: [Select]
fdisk -l  | grep '^Disk /dev/sd'  | cut -d: -f1 | cut -d ' ' -f2
/dev/sda                                                                                     
/dev/sdb
/dev/sdf

regards.

EDIT for spelling
« Last Edit: July 23, 2010, 09:11:48 AM by JohnBoy »

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6849
  • MLU
Re: Formatting Text
« Reply #3 on: July 23, 2010, 09:18:46 AM »
JohnBoy, if you try awk it should be easier:

Code: [Select]
fdisk -l   | grep '^Disk /dev/'  | awk '{print $2}'
In this case awk simply takes the output and prints the second part, separated by a space (the default separator).

HTH
Torsten

Reading this again I just wondered if there is a means using awk only to extract the /dev/sd* field from the main output of fdisk -l

That might be interesting .....   :D

Thanks again

Offline critter

  • Full Member
  • ***
  • Posts: 220
Re: Formatting Text
« Reply #4 on: July 23, 2010, 10:39:13 AM »
FWIW
Code: [Select]
fdisk -l   | grep '^Disk /dev/'  | awk '{print $2}' | sed -e s/://achieves the same although the command is getting rather long
Motherboard   Gigabyte Z68X-UD3H-B3
Hard Drives      2 x Maxtor STM350032 500GB SATA
Memory      16GB RAM
Processor      Intel core i5 3.30GHz
Video         nVidia GeForce GT430
Sound      HDA Intel PCH
PCLinuxOS          KDE

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6849
  • MLU
Re: Formatting Text
« Reply #5 on: July 23, 2010, 11:04:15 AM »
FWIW
Code: [Select]
fdisk -l   | grep '^Disk /dev/'  | awk '{print $2}' | sed -e s/://achieves the same although the command is getting rather long

Thanks ........  not a whole lot of difference in the command length.


Offline critter

  • Full Member
  • ***
  • Posts: 220
Re: Formatting Text
« Reply #6 on: July 23, 2010, 11:51:43 PM »
using only awk to filter the results:
Code: [Select]
fdisk -l   | grep '^Disk /dev/'  | awk 'split ($2, dev, ":") { print dev[1] }'
slightly shorter! (in execution time!)
« Last Edit: July 24, 2010, 01:25:05 AM by critter »
Motherboard   Gigabyte Z68X-UD3H-B3
Hard Drives      2 x Maxtor STM350032 500GB SATA
Memory      16GB RAM
Processor      Intel core i5 3.30GHz
Video         nVidia GeForce GT430
Sound      HDA Intel PCH
PCLinuxOS          KDE

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6849
  • MLU
Re: Formatting Text
« Reply #7 on: July 24, 2010, 02:41:32 AM »
Thank you.

I think I can now mark this as solved.   ;)

uncleV

  • Guest
Re: [SOLVED] Formatting Text
« Reply #8 on: July 24, 2010, 04:50:57 AM »
I found it hard to find a description for the title of this thread  :(

"Filtering" instead of "formatting"?

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6849
  • MLU
Re: [SOLVED] Formatting Text
« Reply #9 on: July 24, 2010, 04:52:30 AM »
I found it hard to find a description for the title of this thread  :(

"Filtering" instead of "formatting"?

added   ;)