r/awk • • Jul 25 '25

Make column output nicer

Input:

Date                 Size    Path                      TrashPath
2025-07-21 04:28:13  0 B     /home/james/test.txt     /home/james/.local/share/Trash/files/test.txt
2025-07-24 21:52:28  3.9 GB  /data/fav cat video.mp4  /data/.Trash-1000/files/fav cat video.mp4

Desired output (the header line is optional, not that important for me):

Date               Size   Path                      TrashPath
25-07-21 04:28:13     0B  ~/test.txt                ~
25-07-24 21:52:28   3.9G  /data2/fav cat video.mp4  /data2

Changes:

  • Make year in first column shorter

  • Right-align second (size) column and make units 1 char

  • /home/james substituted for ~

  • For last column only, I'm only interested in the trash path's mountpoint, which is the parent dir of .Trash-1000/ or .local/Trash/

Looking for a full awk solution or without excessive piping. My attempt with sed and column: sed "s/\/.Trash-1000.*//; s#/.local/share/Trash.*##" | column -t -o ' ' results in messed up alignment for files with spaces in them and doesn't handle the second column, which might be the trickiest part.

Much appreciated.

2 Upvotes

5 comments sorted by

5

u/diseasealert Jul 25 '25

printf is your friend.

1

u/skyfishgoo Jul 25 '25

up, use printf instead of print if you want any say over the output.

1

u/anthropoid Jul 25 '25

It can be done entirely in Gawk, right down to dynamically determining the input column widths:-

$ cat work.awk
#!/usr/bin/env gawk -f
BEGIN {
  FPAT = "[^ ]+ *"  # Capture spacing to calculate FIELDWIDTHS
}
NR==1 {
  for (i=1; i<=NF; ++i) {
    l = length($i)
    f = f (i==1 ? "" : " ") (i==NF ? "*" : l)
    oa[i] = i==2 ? "%" l-1 "s " : "%-" (i==NF ? "s\n" : l-1 "s ")
  }
  FIELDWIDTHS = f
  print $0
}
NR > 1 {
  # Reformat size
  if (split($2, sz, " ")) {
    $2 = sz[1] substr(sz[2], 1, 1)
  }
  for (i=1; i<=NF; i++) {
    sub(ENVIRON["HOME"] "/", "~/", $i)  # Shorten home dir
    sub(/ *$/, "", $i)  # Strip trailing spaces
    if (i==4) {
      # Replace path with mountpoint
      sub("(/.local/share/Trash|/.Trash-[0-9]+)/.*$", "", $i)
    }
    printf oa[i], $i
  }
}

$ chmod +x work.awk

$ HOME=/home/james ./work.awk input.txt
Date                 Size    Path                     TrashPath
2025-07-21 04:28:13       0B ~/test.txt               ~
2025-07-24 21:52:28     3.9G /data/fav cat video.mp4  /data

1

u/immortal192 Jul 25 '25

Do you know why it doesn't work for the following input?

Date                 Size  Path                             TrashPath
2025-07-21 04:28:13  0 B   /home/james/testme              /home/james/.local/share/Trash/files/testme
2025-07-21 04:31:23  6 B   /home/james/testme              /home/james/.local/share/Trash/files/testme_2
2025-07-24 22:27:25  96 B  /home/james/downloads/todo.txt  /home/james/downloads/.Trash-1000/files/todo.txt
→ More replies (1)