Home > OS >  Linux/Unix using awk function with logical &&
Linux/Unix using awk function with logical &&

Time:10-25

ls -la | awk 'BEGIN {printf "%s\t%s\n","Name","Size"}{if ($9 == "." && $5 == 0) print $9,"\t"$5}'

drwxrws---  6 rsy512 group20 4096 Oct 24 20:54 .
drwxrws---  6 root   group20   68 Oct 24 14:19 ..
-rwxrw----. 1 rsy512 group20  568 Mar  3  2010 adhoc
-rw-r-----. 1 rsy512 group20    0 Mar  3  2010 .ghost1.c
-rw-r-----. 1 rsy512 group20    0 Mar  3  2010 .ghost2
-rw-r-----. 1 rsy512 group20    0 Mar  3  2010 .ghost3.cpp 
drwxr-s---  2 rsy512 group20    6 Mar  3  2010 .ghostdir
-rwxrw----. 1 rsy512 group20   21 Feb 17  2010 input4.txt
-rwxrw----. 1 rsy512 group20 1878 Feb 26  2008 lab1.cpp
-rwxrw----. 1 rsy512 group20 1171 Feb  4  2010 Lab2.cpp
-rwxrw----. 1 rsy512 group20 1013 Mar  3  2010 proc
-rwxrw----. 1 rsy512 group20  109 Mar  3  2010 prog1.c
-rwxrw----. 1 rsy512 group20  104 Mar  3  2010 prog2.c
-rwxrw----. 1 rsy512 group20    0 Mar  3  2010 prog3.c.txt
-rwxrw----. 1 rsy512 group20    0 Mar  3  2010 prog.4c 
drwxrws---  2 rsy512 group20    6 Mar  3  2010 programs.c 
drwxrws---  2 rsy512 group20    6 Mar  3  2010 programs.cpp
-rw-rw----. 1 rsy512 group20   46 Oct 24 20:54 script1_t20 
drwxrws---  2 rsy512 group20    6 Mar  3  2010 test1

I need to output the 3 files ".ghost..." and their file size. With the header name and size.

Im trying to use a logical AND (&&) to achieve this effect. Only after both conditions are satisfied, proceed to print the information. Add a header ,"Name", "Size" to your results. Add this awk script (preceded by ls -l |) to the content of script1_tXX

CodePudding user response:

you can do the required filtering with find, not sure printing the 0 size will do but here it is.

$  { echo -e "name\tsize"; find . -type f -name ".*" -size 0 -printf "%p\t%k KB\n"; } | 
     column -ts$'\t'

CodePudding user response:

Let your;s ls -la output be

drwxrws---  6 rsy512 group20 4096 Oct 24 20:54 .
drwxrws---  6 root   group20   68 Oct 24 14:19 ..
-rwxrw----. 1 rsy512 group20  568 Mar  3  2010 adhoc
-rw-r-----. 1 rsy512 group20    0 Mar  3  2010 .ghost1.c
-rw-r-----. 1 rsy512 group20    0 Mar  3  2010 .ghost2
-rw-r-----. 1 rsy512 group20    0 Mar  3  2010 .ghost3.cpp 
drwxr-s---  2 rsy512 group20    6 Mar  3  2010 .ghostdir
-rwxrw----. 1 rsy512 group20   21 Feb 17  2010 input4.txt
-rwxrw----. 1 rsy512 group20 1878 Feb 26  2008 lab1.cpp
-rwxrw----. 1 rsy512 group20 1171 Feb  4  2010 Lab2.cpp
-rwxrw----. 1 rsy512 group20 1013 Mar  3  2010 proc
-rwxrw----. 1 rsy512 group20  109 Mar  3  2010 prog1.c
-rwxrw----. 1 rsy512 group20  104 Mar  3  2010 prog2.c
-rwxrw----. 1 rsy512 group20    0 Mar  3  2010 prog3.c.txt
-rwxrw----. 1 rsy512 group20    0 Mar  3  2010 prog.4c 
drwxrws---  2 rsy512 group20    6 Mar  3  2010 programs.c 
drwxrws---  2 rsy512 group20    6 Mar  3  2010 programs.cpp
-rw-rw----. 1 rsy512 group20   46 Oct 24 20:54 script1_t20 
drwxrws---  2 rsy512 group20    6 Mar  3  2010 test1

then

$9 == "."

does check if whole 9th field is just dot, this condition only holds for one line in your's ls -la output namely

drwxrws---  6 rsy512 group20 4096 Oct 24 20:54 .

but you want to

output the 3 files ".ghost..."

so you should be looking for .ghost somewhere inside 9th field, this for example might be expressed as

$9 ~ /\.ghost/

note that . needs to be escaped to mean literal dot, above condition holds for lines

-rw-r-----. 1 rsy512 group20    0 Mar  3  2010 .ghost1.c
-rw-r-----. 1 rsy512 group20    0 Mar  3  2010 .ghost2
-rw-r-----. 1 rsy512 group20    0 Mar  3  2010 .ghost3.cpp 
drwxr-s---  2 rsy512 group20    6 Mar  3  2010 .ghostdir

if you wish to exclude dirs you might do that by adding condition

!/^d/

which might be read as line does not (!) starts with (^) character d, therefore complete condition might be written as

!/^d/ && $9 ~ /\.ghost/ && $5 == 0

which does hold for

-rw-r-----. 1 rsy512 group20    0 Mar  3  2010 .ghost1.c
-rw-r-----. 1 rsy512 group20    0 Mar  3  2010 .ghost2
-rw-r-----. 1 rsy512 group20    0 Mar  3  2010 .ghost3.cpp 

Disclaimer: this assumes you never uses any whitespace in files names.

(test in gawk 4.2.1)

  • Related