我正在做家庭作业。 问题是:
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}'
这将在目录中添加两列,其名称和.conf文件的大小相同。
它有效,但是我认为这不是他想要的。 我过得很开心。
我发现在Unix历史的这个晚期,您的机器手正在教您awk,这简直是天才。 有很多更好的方法来编写可读的脚本,并且,如果您的草签者坚持用芒草教给您不可读的样式,那么至少您可以修改perl。
对此评论常驻! :)
让我们看看这里...
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++;}
要获取文件名和大小,请查看ls -l的输出并计数列
BEGIN{count=0}
/^-/ {
count++;
SIZE=$5;
FNAME=$9;
}
这里最大的困难是awk不能通过对基元进行排序来提供很多功能,因此这是困难的部分。如果您想变得聪明,但这可能会失败,但是它并不是特别有效(请参阅我在[code-golf]解决方案中所做的糟糕事情)。最简单的方法是将输出的一部分传递到sort,因此...我们将每个文件的一行收集到一个大字符串中
BEGIN{count=0}
/^-/ {
count++
SIZE=$5;
FNAME=$9;
OUTPUT=sprintf("%10d\t%s
%s",SIZE,FNAME,OUTPUT);
}
END{
printf("%d files
",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
(顺便说一句-这里有一个test子目录,您会注意到它没有出现在输出中。)
很彻底,谢谢! 逐步说明是一个巨大的帮助。
可能是这样的事情会让您前进-
ls -lrS /etc/*.conf |
awk '
BEGIN{print"Size:\tFilename:"} # Prints Headers
/^-/{print $5"\t"$9} # Prints two desired columns, /^-/ captures only files
END{print"Total Files ="(NR-1)}' # Uses in-built variable to print count
测试:#后面的文字是注释,供您参考。
[jaypal:~/Temp] ls -lrS /etc/*.conf |
awk '
BEGIN{print"Size:\tFilename:"}
/^-/{print $5"\t"$9}
END{print"Total Files ="(NR-1)}'
Size: Filename:
0 /etc/kern_loader.conf
22 /etc/ntp.conf
54 /etc/ftpd.conf
105 /etc/launchd.conf
168 /etc/memberd.conf
242 /etc/notify.conf
366 /etc/ntp-restrict.conf
526 /etc/gdb.conf
723 /etc/pf.conf
753 /etc/6to4.conf
772 /etc/syslog.conf
983 /etc/rtadvd.conf
1185 /etc/asl.conf
1238 /etc/named.conf
1590 /etc/newsyslog.conf
1759 /etc/autofs.conf
2378 /etc/dnsextd.conf
4589 /etc/man.conf
Total Files = 18
免责声明:我不是Shell专家。
以为我会试一试,但在回复速度上却被击败了:-):
clear
FILE_COUNT=`find /etc/ -name '*.conf' -type f -maxdepth 1 | wc -l`
echo"Number of files: $FILE_COUNT"
ls -lrS /etc/[^-]*.conf | awk '
BEGIN {print"NAME | SIZE"}\
{print $9," | ",$5}\
END {print"- DONE -"}\
'
我的输出很丑:-(:
Number of files: 21
NAME | SIZE
/etc/kern_loader.conf | 0
/etc/resolv.conf | 20
/etc/AFP.conf | 24
/etc/ntp.conf | 42
/etc/ftpd.conf | 54
/etc/notify.conf | 132
/etc/memberd.conf | 168
/etc/Symantec.conf | 246
/etc/ntp-restrict.conf | 366
/etc/gdb.conf | 526
/etc/6to4.conf | 753
/etc/syslog.conf | 772
/etc/asl.conf | 860
/etc/liveupdate.conf | 861
/etc/rtadvd.conf | 983
/etc/named.conf | 1238
/etc/newsyslog.conf | 1590
/etc/autofs.conf | 1759
/etc/dnsextd.conf | 2378
/etc/smb.conf | 2975
/etc/man.conf | 4589
/etc/amavisd.conf | 31925
- DONE -
通过column -t传递管道以固定对齐方式:]
我首先会找到类似find /etc -type f -name '*.conf'的文件;这样您就可以获得正确的文件列表。然后对它们执行ls -l(也许使用xargs)。然后使用awk应该很简单。
但是我不认为如果我做更多的功课对你有帮助。您需要自己思考并找出答案。