Skip to content
Advertisement

IF statement for different lines [closed]

Question is simple, but I can’t find a fast and elegant way to solve.

I want to decide next: if block size of files is more than 6 then I should print filenames.

stat a.txt b.txt | awk '/Blocks/ {print $4} /File/ {print $2}'

This code returns

'a.txt'
3
'b.txt'
10

But if I use NR==... it cuts row with filename.

Advertisement

Answer

You can format the output of stat to make processing easier (requiring GNU stat):

stat -c '%b %n' a.txt b.txt | awk '$1 > 6 {print $2}'

where (excerpts from man page)

%b number of blocks allocated
%n file name

Or, if your filenames contain special characters, you can get them to be printed with quotes:

stat -c '%b %N'

where

%N quoted file name with dereference if symbolic link

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement