简体   繁体   中英

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.

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? 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. 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 :-)

That says... set the variable x to the word 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 ). If you see a line starting with Book , remember the second field in the variable book . If you see a line starting with Chapter , remember the second field in the variable 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. Oh, and by the way, separate anything I print out with colons. And do this on a file called 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
The -p option on the command line is equivalent to specifying +/pattern; that is, it tells less to start at the first occurrence of pattern in the file.
http://linux.die.net/man/1/less

You cannot do it with less from within a script. 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 . For example, you can read the matching text into an array in bash with:

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) to essentially do the same thing.

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 simply automates the search part of the process for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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