LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 7725|回复: 89

多发发一些有意思的题做做呀![+sed/awk/regex练习]

[复制链接]
发表于 2006-2-21 12:47:39 | 显示全部楼层 |阅读模式
找找有趣的编程题目发上来做做呀,既能提高水平,又能增强对shell的兴趣。大家说好不好!
 楼主| 发表于 2006-2-21 14:31:37 | 显示全部楼层
我先出一个!
求给定范围内的质数。比如:求1-100范围的质数。
回复 支持 反对

使用道具 举报

发表于 2006-2-21 21:36:42 | 显示全部楼层
也加一个:
找出前几个完全数,越多越好.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-2-21 22:29:56 | 显示全部楼层
好!我画蛇添足的说明一下:完全数是指其所有因子(包括1但不包括 该数自身)的和等于该数自身的数,如:6=1+2+3,是所有真因子的和,所以6是完全数。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-2-23 20:47:54 | 显示全部楼层
我写了一个程序,请大家指正。英文不好,请勿见笑。
  1. #!/bin/bash
  2. read -p "input the begin number:" begin
  3. read -p "input the end number:" end
  4. for (( i=begin;i<=end;i++ ))
  5. do
  6.         if [[ $i = [14] ]]; then
  7.                 continue
  8.         elif [[ $i = [23] ]]; then
  9.                 zhi[i]=$i
  10.         else
  11.                 is_zhi=1
  12.                 for (( j=2;j<i;j++ ))
  13.                 do
  14.                         if (( i%j != 0 && is_zhi == 1 )); then
  15.                                 is_zhi=1
  16.                         elif (( i%j == 0 )); then
  17.                                 is_zhi=0
  18.                                 yinzi[j]=$j
  19.                         fi
  20.                 done
  21.                 if [ $is_zhi = 1 ]; then
  22.                         zhi[i]=$i
  23.                 elif [ $is_zhi = 0 ]; then
  24.                         yinzi_he=1
  25.                         for a in ${yinzi[*]}
  26.                         do
  27.                                 yinzi_he=$((yinzi_he+a))
  28.                         done
  29.                         if [ $i = $yinzi_he ]; then
  30.                                 wanquan[i]=$i
  31.                         fi
  32.                 fi
  33.         fi
  34.         unset yinzi
  35. done
  36. printf "the zhishu is:${zhi[*]}\n"
  37. printf "the wanquanshu is:${wanquan[*]}\n"
  38.                                                                                                                              
复制代码
回复 支持 反对

使用道具 举报

发表于 2006-2-27 04:32:51 | 显示全部楼层
支持!我稍候会收集一些sed/awk类的题。
回复 支持 反对

使用道具 举报

发表于 2006-3-1 10:10:14 | 显示全部楼层
Post by yongjian
支持!我稍候会收集一些sed/awk类的题。
我把这个帖子顶起来,今天论坛里感觉有点冷清,斑竹出点题吧,呵呵。
回复 支持 反对

使用道具 举报

发表于 2006-3-1 13:37:53 | 显示全部楼层
习题1

There are two files. First one is like this:

  1. ifconfig
  2. shutdown
  3. iptables
  4. fdisk
复制代码

and the second is like:

  1. The ifconfig  command is  used to configure
  2. the kernel-resident network interfaces.
  3. It is used at boot time to set up interfaces as necessary.
  4. After that,
  5. it is usually only needed when debugging or when system tuning is needed.



  6. shutdown  command brings  the system down in a secure way.
  7. All logged-in users are notified that the system is going down, and login(1) is blocked.  It
  8. is possible to shut the system down immediately or after a specified delay.




  9. Iptables  is  used
  10. to  set up, maintain, and inspect the tables of IP packet filter rules in the
  11. Linux kernel.  Several different tables may be
  12.        defined.
  13. Each table contains a number of built-in
  14. chains and may also contain user-defined chains.


  15. Hard disks can be divided into one or more logical disks called partitions.
  16. This division is described
  17. in the partition table found in sector 0
  18.        of the disk.




复制代码

文件结构特征:
1.文件1有很多命令,每个命令一行,中间没有任何空格。
2.文件2是相对文件1各个命令的解释,但由于转换的时候丢失了格式,现在是乱七八糟。各个命令的解释都被切成了几行,中间有不等的空格分开。文件的最后也有很多空行。(由于网页的原因,paste上来的最后的空行好象都被切掉了。请自己在最后加些空行)。

要求:用sed (如果你sed作出来了想用awk试试,也行。但要求是用sed)将这两个文件合成一个文件,同时这个文件的格式必须是这样的:
1. 文件1中的命令仍占一行,以:结尾。
2. 文件2中的解释直接接到各个命令之下,没有空行,同时所有的切开的地方全部都要合成一行。
3. 各个命令和解释之间有1行空格分开。
4. 文件末尾没有空格。

  1. ifconfig:
  2. The ifconfig command is used to configure the kernel-resident network interfaces. It is used at boot time to set up interfaces as necessary. After that, it is usually only needed when debugging or when system tuning is needed.

  3. shutdown:
  4. shutdown command brings the system down in a secure way. All logged-in users are notified that the system is going down, and login(1) is blocked. It is possible to shut the system down immediately or after a specified delay.

  5. iptables:
  6. Iptables is used to set up, maintain, and inspect the tables of IP packet filter rules in the Linux kernel.  Several different tables may be defined. Each table contains a number of built-in chains and may also contain user-defined chains.

  7. fdisk:
  8. Hard disks can be divided into one or more logical disks called partitions. This division is described in the partition table found in sector 0 of the disk.
复制代码
回复 支持 反对

使用道具 举报

发表于 2006-3-1 21:39:33 | 显示全部楼层

小弟先来awk的

假设yongjian兄的第一个文件名为first,第二个文件名为second



  1. # filename: sort.a

  2. BEGIN { getline title < "first"; print title":" }

  3. {

  4. if ($0 == "") {
  5. getline title < "first"
  6. print "\n\n"title":"
  7. getline
  8. while ($0 == "") getline
  9. }

  10. for (i=1;i<=NF;i++) printf "%s ",$i

  11. }

  12. END { print "" }

复制代码



  1. bash# awk -f sort.a second

复制代码


结果出来了,HOHO,不过最后那个空格还在哦,请yongjian兄点评。
回复 支持 反对

使用道具 举报

发表于 2006-3-1 22:36:01 | 显示全部楼层
空格解决了,不过用了个比较傻的方法



  1. # filename: sort.a

  2. BEGIN { getline title < "first"; content=title":\n" }

  3. {

  4. if ($0 == "") {
  5.         getline title < "first"
  6.         content=content"\n\n"title":\n"
  7.         getline
  8.         while ($0 == "") getline
  9. }
  10. line=""
  11. for (i=1;i<=NF;i++) line=sprintf("%s%s ",line,$i)
  12. content=sprintf("%s%s",content,line)

  13. }

  14. END { print substr(content,1,length(content)-1) }

复制代码



  1. bash# awk -f sort.a second

复制代码


没有最后那个空格了,呵呵
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表