【问题标题】:Beginner problem in UNIX shell commands and pipesUNIX shell 命令和管道中的初学者问题
【发布时间】:2011-03-09 16:09:51
【问题描述】:

这是来自课程的材料。根据我的研究,代码总是以 shebang 开头

#! /bin/sh

我还想解决这个问题,我可能需要使用

的组合
wc, tail, grep, head

但是我很难把它放在一起。非常感谢您的帮助。

Write a shell command that processes a file 
that is 200 lines long or more. It outputs the number of those lines within lines 100 through 
199 *inclusive* that contain the character string “hello”.


Write a shell command that outputs the number of lines in the lines range 
100..199  that contain "hello, " but is NOT followed by "world".

【问题讨论】:

  • 行数是从0还是1开始?
  • 在第一个任务中你的意思是你只需要打印相关行的行号?

标签: shell unix pipe


【解决方案1】:

第一个任务

head -n 199 $FILE | tail -n 100 | grep "hello" | wc -l

第二个任务

head -n 199 $FILE | tail -n 100 | grep "hello, " | grep -v "hello, world" | wc -l

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-06
    • 2022-12-04
    • 2017-04-10
    • 2021-08-26
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多