Search code examples
linuxubuntush

How can I get a column that is ending with a specific text (KiB, MiB, GiB) without using the column number?


I want to get the FILESIZE column as an output (only the KiB, MiB, and GiB values). The column number could change, so I can't use the column number for this (imagine you don't know the column number of the FILESIZE column).

The following command will create a file in your Linux OS what I am talking about…

But you have to install yt-dlp before running it:

yt-dlp -F https://youtu.be/njX2bu-_Vw4 > cp1.txt

Or you can use this image for just see it (https://i.sstatic.net/YWpWh.png)

This command doesn't work for my problem.

#!/bin/bash
string=FILESIZE
a2=$(echo `awk -v b="$string" '{for (i=1;i<=NF;i++) { if ($i == b) { print i } }}' cp1.txt`)
e23=`awk -v col="$a2" '{print $col}' cp1.txt`
echo "e23 = "$e23 > 2list-11.txt

This is another command I tried.

grep -o '\w*iB\b' cp1.txt >> best2.txt

and I want to get this:

476.73KiB
525.94KiB
755.10KiB
768.69KiB
1024.00KiB
2.95MiB
1.95MiB
2.02MiB
5.80MiB
5.80MiB
5.85MiB
1.06MiB
504.26KiB
342.80KiB
595.45KiB
879.16KiB
2.00MiB
2.34MiB
1.29MiB
1.29MiB
4.22MiB
5.02MiB
3.69MiB
5.80MiB
3.56MiB
8.94MiB
11.00MiB
6.53MiB
5.39MiB
16.61MiB
21.07MiB
22.76MiB
20.20MiB
10.61MiB
34.04MiB
18.16MiB
45.92MiB
51.46MiB
59.18MiB
34.81MiB
73.54MiB
86.07MiB
119.66MiB
203.30MiB
225.43MiB
298.42MiB
370.87MiB
414.98MiB

How can I solve this problem using a shell command or shell script on Linux?


Solution

  • Just grep it:

    $ cat f
    asas 768.69KiB sdfsdf
    1424 safaf 1024.00KiB
    2.95MiB afs 121212
    
    $ grep -Eo '[0-9]+\.*[0-9]*[KMG]iB' f
    768.69KiB
    1024.00KiB
    2.95MiB