- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
您好,我得到了如下所示的字符串,一切正常,但后来我记得我需要添加一个命令,以便用户可以搜索他们想要查看的文件内的字符串。我尝试使用 grep 但我不知道如何很好地使用 grep。我需要用户输入文件名,然后输入字符串位,并且需要报告字符串是否在文件中。我用来测试这个的程序称为 pi 和terminal。如果你能帮忙那就太棒了。预期的结果是它将显示文本文件并告诉您输入的字符串是否在该字符串文件中。
到目前为止,我的代码是这样的,我突出显示了我不知道的部分(grep):
#!/usr/bin/env bash
echo 'Welcome!'
while [ "$response" != "n" ]
do
read -p "Would you like to find a file? [y/n]:" response
case $response in
y) read -p "What is the name of the file? " file
read grep "text string to search” $file
$grep "redeem reward" $file
find . -type f -name "$file" -exec less {} \;
;;
n)
echo "goodbye"
exit ;;
esac
done
最佳答案
y) read -p "What is the name of the file? " file
read grep "text string to search” $file
$grep "redeem reward" $file
find . -type f -name "$file" -exec less {} \;
;;
这在很多方面都被打破了。
read grep "text string to search” $file
为什么grep
?为什么读入与文件名相同的变量($file
)?哦,当给read
一个变量时,不要使用$
来取消引用(你在第一行就这样做了......)
正如用户 Biffen 正确评论的那样,第二个引号也不正确。大多数编程语言都非常挑剔,只接受 ASCII 0x22 作为“真正的”引号,因此您应该坚持这一点。
$grep "redeem reward" $file
您没有名为 grep
的变量,因此 $grep
是无意义的。此外,我以为你想 grep 输入的 字符串?
find . -type f -name "$file" -exec less {} \;
好吧,我想你现在只是感到困惑。
试试这个:
y) read -p "What is the name of the file: " file
read -p "Text string to search: " text
grep -F "$text" $file
;;
grep
的 -F
选项使用 $text
作为固定字符串而不是模式。如果这不是您想要的,请放弃它。
i don't know how to use grep well.
为此,有 man grep
。在命令行上尝试一下,它就在那里。 (如果您正在寻找 man read
,这是 bash shell 的内置命令,因此请使用 man bash
并跳到“SHELL BUILTIN COMMANDS
”部分。
关于Linux:在用户已准备好输入名称的文本文件中搜索字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26137328/
我正在尝试做这样的事情:Name[i] = "Name"+ (i+1) 在 forloop 中,这样数组的值将是:Name[0] = Name1,Name[1] = Name2,Name[2] = N
我读了here,在GSP中我们可以这样写: ${params.action} 从GSP中,我们可以使用${params.action}作为参数调用Javascript函数(请参阅here)。 是否有其
我的问题:非常具体。我正在尝试想出解析以下文本的最简单方法: ^^domain=domain_value^^version=version_value^^account_type=account_ty
我创建了一条与此类似的路线: Router::connect("/backend/:controller/:action/*"); 现在我想将符合此模式的每个 Controller 路由重命名为类似
我在 Visual Studio 2013 项目中收到以下警告: SQL71502 - Procedure has an unresolved reference to object 最佳答案 这可以
任何人都可以指导我使用名称/值 .NET 集合或 .NET 名称/值字典以获得最佳性能吗?请问最好的方法是什么?我的应用程序是 ASP.NET、WCF/WF Web 应用程序。每个集合应该有 10 到
我在 Zend Framework 2 中有一个默认模块: namespace Application\Controller; use Zend\Mvc\Controller\AbstractActi
这是表格: 关于javascript - 在 javascript 中,这是一个有效的结构吗? : document. 名称.名称.值?,我们在Stack Overflow上找到一个类似的
HtmlHelper.ActionLink(htmlhelper,string linktext,string action) 如何找出正确的路线? 如果我有这个=> HtmlHelper.Actio
我需要一些有关如何将 Controller 定义传递给嵌套在 outer 指令中的 inner 指令的帮助。请参阅http://plnkr.co/edit/Om2vKdvEty9euGXJ5qan一个
请提出一个数据结构来表示内存中的记录列表。每条记录由以下部分组成: 用户名 积分 排名(基于积分)- 可选字段- 可以存储在记录中或可以动态计算 数据结构应该支持高效实现以下操作: Insert(re
错误 : 联合只能在具有兼容列类型的表上执行。 结构(层:字符串,skyward_number:字符串,skyward_points:字符串)<> 结构(skyward_number:字符串,层:字符
我想要一个包含可变数量函数的函数,但我希望在实际使用它们之前不要对它们求值。我可以使用 () => type 语法,但我更愿意使用 => type 语法,因为它似乎是为延迟评估而定制的。 当我尝试这样
我正在编写一个 elisp 函数,它将给定键永久绑定(bind)到当前主要模式的键盘映射中的给定命令。例如, (define-key python-mode-map [C-f1] 'pytho
卡在R中的错误上。 Error in names(x) <- value : 'names' attribute must be the same length as the ve
我有字符串,其中包含名称,有时在字符串中包含用户名,后跟日期时间戳: GN1RLWFH0546-2020-04-10-18-09-52-563945.txt JOHN-DOE-2020-04-10-1
有人知道为什么我会收到此错误吗?这显示将我的项目升级到新版本的Unity3d之后。 Error CS0103: The name `Array' does not exist in the curre
由于 Embarcadero 的 NNTP 服务器从昨天开始就停止响应,我想我可以在这里问:我使用非数据库感知网格,我需要循环遍历数据集以提取列数、它们的名称、数量行数以及每行中每个字段的值。 我知道
在构建Android应用程序的子项目中,我试图根据根build.gradle中的变量设置版本代码/名称。 子项目build.gradle: apply plugin: 'com.android.app
示例用例: 我有一个带有属性“myProperty”的对象,具有 getter 和 setter(自 EcmaScript 5 起支持“Property Getters 和 Setters”:http
我是一名优秀的程序员,十分优秀!