I want to replace the text sd in the following command with a variable:
lsblk -r --output NAME,MOUNTPOINT | awk -F \/ '/sd/ { dsk=substr($1,1,3);dsks[dsk]+=1 } END { for ( i in dsks ) { if (dsks[i]==1) print i } }'
I have tried:
lsblk -r --output NAME,MOUNTPOINT | awk -F -v dev=sd \/ '/{print $dev}/ { dsk=substr($1,1,3);dsks[dsk]+=1 } END { for ( i in dsks ) { if (dsks[i]==1) print i } }'
but that's not working. Can anyone help me? Thanks!
The lsblk output, which is piped to awk is this:
NAME MOUNTPOINT
sda
sda1 /boot/efi
sda2 /var/lib/kubelet/pods/fae467d4-05e9-4aea-8341-0d1524a3ec9c/volume-subpaths/tigera-ca-bundle/calico-kube-controllers/1
sda3
sdb
sdc
sr0
expected output is:
sdb
sdc
So all disks, starting with sd, except the os disk, sda
lsblk
returns on your system), ii) desired output. That way we can test our solutions. Also, when there is an error, don't tell us something is "not working" since that really doesn't tell us anything. Instead, show us the exact error message or, if none, describe how the behavior is not what you expected. Finally, it is always good to mention your operating system so we know what you are working with (we support several). – terdon Mar 06 '24 at 14:54lsblk -nro name | grep -Eo '^sd[a-z]+' | sort | uniq -u
– Stéphane Chazelas Mar 06 '24 at 15:31lsblk -r --output NAME,MOUNTPOINT
) and you don't show us the final output you want from the desired tool given that input then you're making it very much harder than necessary, maybe impossible, for us to be able to best help you – Ed Morton Mar 06 '24 at 15:59substr($1,1,3)
to convertsda1
intosda
but if you want to use a variabledev
instead of hard-codingsd
then is it safe to assume the contents of that variable will also be 2 chars, always with 1 letter then digits after it in the input, and you won't have something likedev=FOOBAR
with input likeFOOBARabc123 /var/lib/kubelet/pods/fae467d4...
and then need to convertFOOBARabc123
intoFOOBARabc
or something else? – Ed Morton Mar 06 '24 at 17:43/{print $dev}/
to do in that second AWK script – ilkkachu Mar 06 '24 at 18:05