Results 1 to 5 of 5

Thread: Capture with grep only numbers between the following rule

  1. #1
    Join Date
    Apr 2016
    Beans
    356

    Question Capture with grep only numbers between the following rule

    hello,

    I need pick up only this value "137216" from this output
    Code:
    Disk 2016-05-27-raspbian-jessie.img: 3,8 GiB, 4019191808 bytes, 7849984 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: dos
    Disk identifier: 0x14c20151
    
    Device                          Boot  Start     End Sectors  Size Id Type
    2016-05-27-raspbian-jessie.img1        8192  137215  129024   63M  c W95 FAT32 (LBA)
    2016-05-27-raspbian-jessie.img2      137216 7849983 7712768  3,7G 83 Linux
    I use this command "fdisk -l 2016-05-27-raspbian-jessie.img | grep img2", but I receive entire line: 2016-05-27-raspbian-jessie.img2 137216 7849983 7712768 3,7G 83 Linux

    How can I create the following rule: grep all numbers between img2 and second block numbers, in this case "7849983"?
    Thanks

  2. #2
    Join Date
    Jun 2016
    Beans
    2,824
    Distro
    Xubuntu

    Re: Capture with grep only numbers between the following rule

    Does this do what you're looking for?
    Code:
    fdisk -l 2016-05-27-raspbian-jessie.img | grep -o -i -P 'img2\s+[0-9]+[^0-9]' | sed -r -e 's/img2 +//g'
    Last edited by halogen2; September 11th, 2016 at 01:34 AM. Reason: typo
    Xubuntu 22.04, ArchLinux ♦ System76 hardware, virt-manager/KVM, VirtualBox
    If your questions are resolved to your satisfaction, please use Thread Tools > "Mark this thread as solved..."

  3. #3
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Capture with grep only numbers between the following rule

    FYI it's often easier and more natural to do this kind of thing with `awk` e.g.

    Code:
    fdisk -l 2016-05-27-raspbian-jessie.img | awk '/img2/ {print $2}'

  4. #4
    Join Date
    Apr 2016
    Beans
    356

    Re: Capture with grep only numbers between the following rule

    Hello,

    Every answers works, thanks folks!
    I share with you my script, if you have any suggestion be sure to communicate: https://github.com/EdgarOliveira/scr...u_choose_vm.sh

  5. #5
    Join Date
    Jun 2016
    Beans
    2,824
    Distro
    Xubuntu

    Re: Capture with grep only numbers between the following rule

    You're welcome!
    Xubuntu 22.04, ArchLinux ♦ System76 hardware, virt-manager/KVM, VirtualBox
    If your questions are resolved to your satisfaction, please use Thread Tools > "Mark this thread as solved..."

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •