Right split using awk

Viewed 403

I have a txt file filled with file sizes and full paths.

ex:

1 3788160 /nfs/extracted/UNENCRYPTED.bin.extracted/_0.extracted/vmlinux.img
2 5971656 /nfs/extracted/extracted/out_v2.1.3.zip.extracted/firmware.tar.extracted/_uImage.extracted/vmlinux.bin.30071
3 4202630 /nfs/extracted/v2.0.0.10/extracted/v2.0.0.10.zip.extracted/_install.img.extracted/package2/vmlinux.develop.avhdd.mars.nand.bin
4 117 /nfs/extracted/v1.0.0.14/extracted/1.0.0.14.zip.extracted

I'd like to print the second column and the filename (rsplit on "/") so that a[0] is the file name.

3788160 vmlinux.img
5971656 vmlinux.bin.30071
4202630 vmlinux.develop.avhdd.mars.nand.bin
117 1.0.0.14.zip.extracted

Is there an awk equilient to rsplit?

ex:

awk '{rsplit($2,a,"/"); print $1, a[0]}'
4 Answers

You may use this awk:

awk '{sub(/^.*\//, "", $3); print $2, $3}' file
3788160 vmlinux.img
5971656 vmlinux.bin.30071
4202630 vmlinux.develop.avhdd.mars.nand.bin
117 1.0.0.14.zip.extracted

Here:

  • sub(/^.*\//, "", $3): Removes everything upon start to last /
  • print $2, $3: Print 2nd and 3rd column

OP almost had it, just 2 smallish changes:

  • use the split() function (split() returns a count of the number of array entries which we'll catch and store in variable n)
  • access the last item of the array (instead of a[0])

Data set:

$ cat input.dat
1 3788160 /nfs/extracted/UNENCRYPTED.bin.extracted/_0.extracted/vmlinux.img
2 5971656 /nfs/extracted/extracted/out_v2.1.3.zip.extracted/firmware.tar.extracted/_uImage.extracted/vmlinux.bin.30071
3 4202630 /nfs/extracted/v2.0.0.10/extracted/v2.0.0.10.zip.extracted/_install.img.extracted/package2/vmlinux.develop.avhdd.mars.nand.bin
4 117 /nfs/extracted/v1.0.0.14/extracted/1.0.0.14.zip.extracted

One awk solution using the split() function:

$ awk '{ n=split($3,a,"/") ; print $2, a[n] }' input.dat
3788160 vmlinux.img
5971656 vmlinux.bin.30071
4202630 vmlinux.develop.avhdd.mars.nand.bin
117 1.0.0.14.zip.extracted

In native bash, no awk needed:

while read -r _ size name; do
  printf '%s\n' "$size ${name##*/}"
done

This throws away the first column by putting it into the variable $_; puts the second column into size, and puts the remaining content on each line into the variable name; then uses the parameter expansion ${name##*/} to strip everything up-to-and-including the last / from name on output.

Could you please try following, looks like this could be done with creating separate field delimiters tested and written with shown samples only. Written and tested in https://ideone.com/sk8zV1

awk -F'[ /]' '{print $2,$NF}' Input_file

Explanation: Simply making space and / as field delimiters and printing 2nd and last fields as per shown samples. Note NF denotes number of fields in current line so $NF will print last field's value here.

Related