How to extract values from a command with multi-line output?
Oliver Grawert
ogra at ubuntu.com
Mon Apr 4 18:58:04 UTC 2022
hi,
Am Montag, dem 04.04.2022 um 20:40 +0200 schrieb Bo Berglund:
...
> $ fdisk -lu filename.img | tail -n 2
> filename.img1 8192
532479 524288 256M c W95 FAT32 (LBA)
> filename.img2 532480
3923967 3391488 1.6G 83 Linux
...
> The response from this command is two lines (one for each partition
> in the
>
> image) and I am only interested in the 3 numbers on the two lines
you could read the lines with a while loop and assign each column to a
variable:
$ LC_ALL=C sudo fdisk -l jammy-desktop-amd64.iso | tail -3
jammy-desktop-amd64.iso1 64 6688419 6688356 3.2G Microsoft basic
data
jammy-desktop-amd64.iso2 6688420 6696923 8504 4.2M EFI System
jammy-desktop-amd64.iso3 6696924 6697523 600 300K Microsoft basic
data
$
$ LC_ALL=C sudo fdisk -l jammy-desktop-amd64.iso | tail -3 | while read
-r name start end size type; do echo "$name $size $type"; done
jammy-desktop-amd64.iso1 6688356 3.2G Microsoft basic data
jammy-desktop-amd64.iso2 8504 4.2M EFI System
jammy-desktop-amd64.iso3 600 300K Microsoft basic data
$
just replace the 'echo "$name $size $type"' in that line with the code
you want to process any of the variables with or assign them to some
global variables outside the while loop ...
ciao
oli
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20220404/b5c688bd/attachment.sig>
More information about the ubuntu-users
mailing list