How to extract values from a command with multi-line output?
Peter Teuben
teuben at gmail.com
Mon Apr 4 18:59:37 UTC 2022
Hi Bo,
I have this "problem" a lot, and you can do this with awk, but the
scripts are so "awkward" looking, and I wrote a little C program for
this (which really you don't want to use), but I call this one as follows:
fdisk -lu filename.img | tail -n 2 | txtpar -p0=1,2 p1=1,4 p2=2,2
but in awk I might write a 123.awk script: (chmod +x 123.awk)
#! /usr/bin/awk -f
#
{
if (NR==1) { a=$2; b=$4;}
if (NR==2) { c=$2; }
}
END {
print a,b,c;
}
then you cn
fdisk -lu filename.img | tail -n 2 | ./123.awk
but in unix there are probably 10 different solutions to this.
On 4/4/22 14:40, Bo Berglund wrote:
> I am trying to script working with a file system image file after I have found
> out how I can do this manually.
>
> When scripting the procedure I have one big problem and that is how I can
> extract data from the output of an fdisk command...
> I am not versed in the various tools on Linux I have seen mentioned when
> searching. Something simple to just extract the numbers is what I need.
>
> I do this to get the info (the lines may be wrapped by the newsreader):
> ----
> $ 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:
> 8192 524288 532480
>
> These are the 1st and 3rd number of the first line and the first number on the
> second line, following the file name.
>
> How can I get these into 3 variables inside the script?
>
> I use them in order to mount the images:
>
> sudo mount -t ext4 -o loop,offset=$((532480*512)) filename.img /mnt/disk
> sudo mount -t vfat -o loop,offset=$((8192*512)),sizelimit=$((524288*512))
> filename.img /mnt/disk/boot
>
> I want to automate this mount operation in a "mountimage" script, so how to
> extract the numbers from the response shown?
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20220404/85a86ca3/attachment.html>
More information about the ubuntu-users
mailing list