我正在做家务工作.问题是:
Write an
awk
script to select all regular files (not directories or
links) in/etc
ending with.conf
, sort the result by size from
smallest to largest, count the number of files, and print out the
number of files followed by the filenames and sizes in two columns.
Include a header row for the filenames and sizes. Paste both your
script and its output in the answer area.
我真的在努力通过使用awk来努力工作.这就是我想出来的.
ls -lrs /etc/*.conf |wc –l
将返回数字33,即目录中的.conf文件数.
ls -lrs /etc/*.conf |awk '{print "File_Size"": " $5 " ""File_Name and Size"": " $9}'
解决方法:
我们在这看……
select all regular files (not directories or links)
到目前为止,你还没有解决这个问题,但如果你在ls -l …的输出中输入,这很容易,请选择
/^-/
因为目录以d开头,符号链接以l开头,依此类推.只有普通旧文件以 – 开头.现在
print out the number of files followed
好吧,计算比赛很容易……
BEGIN{count=0} # This is not *necessary*, but I tend to put it in for clarity
/^-/ {count++;}
BEGIN{count=0}
/^-/ {
count++;
SIZE=$5;
FNAME=$9;
}
这里最大的困难是awk没有通过排序原语提供太多,所以这是困难的部分.如果你想要聪明但不是特别有效,那就可以打败(看看我在a [code-golf] solution所做的糟糕事情).容易(和unixy)做的事情是管道输出的一部分进行排序,所以…我们为每个文件收集一行到一个大字符串
BEGIN{count=0}
/^-/ {
count++
SIZE=$5;
FNAME=$9;
OUTPUT=sprintf("%10d\t%s\n%s",SIZE,FNAME,OUTPUT);
}
END{
printf("%d files\n",count);
printf(" SIZE \tFILENAME"); # No newline here because OUTPUT has it
print OUTPUT|"sort -n --key=1";
}
给出类似的输出
11 files
SIZE FILENAME
673 makefile
2192 houghdata.cc
2749 houghdata.hh
6236 testhough.cc
8751 fasthough.hh
11886 fasthough.cc
19270 HoughData.png
60036 houghdata.o
104680 testhough
150292 testhough.o
168588 fasthough.o
(顺便说一句 – 这里有一个测试子目录,你会注意到它没有出现在输出中.)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。