Search code examples
shellawkscriptingcshxargs

Csh - Fetching fields via awk inside xargs


I'm struggling to understand this behavior:

Script behavior: read a file (containing dates); print a list of files in a multi-level directory tree and get their size, print the file size only, (future step: sum the overall file size).

Starting script:

 cat dates | xargs -I {} sh -c "echo '{}: '; du -d 2 "/folder/" | grep {} | head"
2000-03:
1000 /folder/2000-03balbasldas
2000-04:
12300 /folder/2000-04asdwqdas
[and so on]

But when I try to filter via awk on the first field, I still get the whole line

  cat dates | xargs -I {} sh -c "echo '{}: '; du -d 2 "/folder/" | grep {} | awk '{print $1}'"
2000-03:
    1000 /folder/2000-03balbasldas
    2000-04:
    12300 /folder/2000-04asdwqdas

I've already approached it via divide-et-impera, and the following command works just fine:

du -d 2 "/folder/" | grep '2000-03' | awk '{print $1}'
1000

I'm afraid that I'm missing something very trivial, but I haven't found anything so far.

Any idea? Thanks!

Input: directory containing folders named YYYY-MM-random_data and a file containing strings:

ls -l
drwxr-xr-x  2 user  staff  68 Apr 24 11:21 2000-03-blablabla
drwxr-xr-x  2 user  staff  68 Apr 24 11:21 2000-04-blablabla
drwxr-xr-x  2 user  staff  68 Apr 24 11:21 2000-05-blablabla
drwxr-xr-x  2 user  staff  68 Apr 24 11:21 2000-06-blablabla
drwxr-xr-x  2 user  staff  68 Apr 24 11:21 2000-06-blablablb
drwxr-xr-x  2 user  staff  68 Apr 24 11:21 2000-06-blablablc
[...]

cat dates
2000-03
2000-04
2000-05
[...]

Expected output: sum of the disk space occupied by all the files contained in the folder whose name include the string in the file dates

2000-03: 1000
2000-04: 2123
2000-05: 1222112
[...]

====== But in particular, I'm interested in why awk is not able to fetch the column $1 I asked it to.


Solution

  • Ok it seems I found the answer myself after a lot of research :D I'll post it here, hoping that it will help somebody else out.

    https://unix.stackexchange.com/questions/282503/right-syntax-for-awk-usage-in-combination-with-other-command-inside-xargs-sh-c

    The trick was to escape the $ sign.

     cat dates | xargs -I {} sh -c "echo '{}: '; du -d 2 "/folder/" | grep {} | awk '{print \$1}'"