In AWK how to do output of 18 lines [closed]

0

I have to do an output of 18 lines:

ls -lh ~cis140u/lab6examples |awk  '{print" \t",$8, $6,-$7,"\t",$NF , $5, "\t",$10; } '

in order of DATE ARCHIVE SIZE

Is it okay?

    
asked by richdep 06.08.2017 в 17:03
source

1 answer

3

Look at the default output of ls -lh :

$ ls -lh
total 0
-rw-r--r-- 1 m m 0 Aug  6 11:42  archivo
-rw-r--r-- 1 m m 0 Jul  6  2012 'archivo viejo'

If you ignore the first line, you can see what will be the fields that will read awk :

|          1 | 2 | 3 | 4 | 5 |   6 | 7 |     8 |       9 |     10 |
+------------|---|---|---|---|-----|---|-------|---------|--------+
| -rw-r--r-- | 1 | m | m | 0 | Aug | 6 | 11:42 | archivo |        |
| -rw-r--r-- | 1 | m | m | 0 | Jul | 6 |  2012 |'archivo | viejo' |

The date corresponds to fields 6, 7 and 8, but as you can see, field 8 is not consistent.

The file corresponds to field 9 and since we have a file name with spaces, also to field 10.

The size is field 5.

So if you do not have file names with spaces or files whose last modification date is "old", it's quite simple (we skipped the first record with condition NR>1 ):

 $ ls -lh | awk 'NR>1 { print $6,$7,$8"\t"$9"\t"$5 }'
 Aug 6 11:42     archivo 0
 Jul 6 2012      archivo 0

That is, the field $ 6, $ 7 and $ 8 followed by a tabulation, followed by the $ 9 field, followed by a tabulation, followed by the $ 5 field.

However, as you can see, in this example it is not so simple, because it cut the name of the file with spaces and the date is not consistent.

To get a consistent result on the date you can use the argument --full-time of ls :

$ ls -lh --full-time | awk 'NR>1 { print $6"\t"$9"\t"$5 }'
2017-08-06      archivo 0
2012-07-06      archivo 0

However, we still have problems with the file name, so we must do something more complicated if we want to continue with awk :

$ ls -lh --full-time | awk 'NR>1 { archivo=$9;for (i=10;i<=NF;++i) archivo=archivo" "$i; print $6"\t"archivo"\t"$5 }'
2017-08-06      archivo 0
2012-07-06      archivo viejo   0

Perhaps a simpler way to do it can be stat :

stat -c "%y %n %s" *
    
answered by 06.08.2017 в 19:13