简体   繁体   English

bash shell脚本可以用less打开一个文本文件,然后从脚本执行更少的命令吗?

[英]Can a bash shell script open a text file with less and then execute less commands from the script?

I would like to write a shell script that would call less to open a text file. 我想编写一个shell脚本,可以少打开一个文本文件。

less filename.txt 

I then would like the script to search forward through the file using /'search string' or search back using ?'search string'. 然后,我希望脚本使用/'搜索字符串'搜索文件或使用搜索字符串'搜索字符串'。 I also want the script to read the results into variables. 我还希望脚本将结果读入变量。

I can get a script to call less to open the file , but how do I get bash to call a series of less commands once the file is opened? 我可以得到一个脚本来少打开文件,但是如何在打开文件后让bash调用一系列较少的命令? Can this be done? 可以这样做吗?

I suspect you may be able to do this more easily with awk if you showed us how your file is structured. 如果您向我们展示了文件的结构,我怀疑您可以使用awk更轻松地完成此操作。 You would start at the top, and each time you encountered a new book, you would note the book name. 您将从顶部开始,每次遇到新书时,都会记下书名。 Each time you encountered a chapter, you would note the chapter name. 每次遇到章节时,都会注意章节名称。 Then each time you encountered a word you wanted, you would print the last book, chapter etc that you had seen. 然后每当你遇到你想要的单词时,你就会打印出你看过的最后一本书,章节等。

Imagining your file looks something like this: 想象你的文件看起来像这样:

Book:Book 1
Chapter:Chapter 1
Word word word word
Word word word
Chapter:Chapter 2
Word word interesting word

Then, to find 'interesting' 然后,找到'有趣'

awk -v x="interesting" -F':' '
    /^Book/    {book=$2}
    /^Chapter/ {chapter=$2}
    $0 ~ x     {print book,chapter,$0} ' OFS=":" YourFile

Untested - as I am on my iPad - but probably pretty close :-) 未经测试 - 因为我在我的iPad上 - 但可能非常接近:-)

That says... set the variable x to the word interesting . 那说...将变量x设置为interesting的单词。 When reading lines, use colons to separate fields, so everything up to the first colon is field 1 ( $1 ), everything between the first and second colon is field 2 ( $2 ). 在读取行时,使用冒号分隔字段,因此第一个冒号的所有内容都是字段1( $1 ),第一个和第二个冒号之间的所有内容都是字段2( $2 )。 If you see a line starting with Book , remember the second field in the variable book . 如果您看到以Book开头的行,请记住变量book的第二个字段。 If you see a line starting with Chapter , remember the second field in the variable chapter . 如果您看到以Chapter开头的行,请记住变量chapter的第二个字段。 On any other lines, where the whole line ( $0 ) contains the word interesting , print out the variable book , chapter and the entire current line. 在任何其他行上,整行( $0 )包含interesting的单词,打印出变量bookchapter和整个当前行。 Oh, and by the way, separate anything I print out with colons. 哦,顺便说一句,把我用冒号打印出来的东西分开。 And do this on a file called YourFile . 并在名为YourFile的文件上执行此YourFile

You could keep track of the verse number yourself by incrementing it every time you encounter a new line and resetting it to zero every time you encounter a new chapter... 您可以通过在每次遇到新行时将其增加并在每次遇到新章节时将其重置为零来自己跟踪诗歌编号...

awk -v x="interesting" -F':' '
               {verse++}
    /^Book/    {book=$2}
    /^Chapter/ {chapter=$2;verse=0}
    $0 ~ x     {print book,chapter,verse,$0} ' OFS=":" YourFile

Output: 输出:

Book 1:Chapter 2:1:Word word interesting word

You can use 您可以使用

less -pbazorka filename.txt

-ppattern or --pattern=pattern -ppattern或--pattern = pattern
The -p option on the command line is equivalent to specifying +/pattern; 命令行上的-p选项等同于指定+ / pattern; that is, it tells less to start at the first occurrence of pattern in the file. 也就是说,它告诉较少从文件中第一次出现模式开始。
http://linux.die.net/man/1/less - http://linux.die.net/man/1/less

You cannot do it with less from within a script. 你不能在脚本中less办事。 There is no language that would control less once you turn control over to it. 一旦你将控制权交给它,就没有任何语言可以控制得更少。 In order to do what you want, the easiest way is to use grep . 为了做你想做的事,最简单的方法是使用grep For example, you can read the matching text into an array in bash with: 例如,您可以使用以下命令将匹配的文本读入bash中的数组:

declare -a results
IFS=$'\n'                                # set IFS to only break on newline
results=( $(grep $srchterm $filename) )  # read results into array
<parse as necessary>

A second option would be to use while read -r line; do... done <<< $(grep $srchterm $filename) 第二种选择是while read -r line; do... done <<< $(grep $srchterm $filename) while read -r line; do... done <<< $(grep $srchterm $filename) to essentially do the same thing. while read -r line; do... done <<< $(grep $srchterm $filename)基本上做同样的事情。

In either case, this allows you to get the wanted lines of text into variables into your script. 在任何一种情况下,这都允许您将所需的文本行转换为脚本中的变量。 You can then further parse the lines into additional variables using pattern matching and substring extraction. 然后,您可以使用模式匹配和子字符串提取进一步将行解析为其他变量。 If you do not use grep , then your alternative is to read the file line-by-line and manually search for the text of interest. 如果您不使用grep ,那么您可以选择逐行读取文件并手动搜索感兴趣的文本。 grep simply automates the search part of the process for you. grep只是为您自动执行流程的搜索部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM