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

Edgar Crockett

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?

David C. Rankin

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Bash script to read from a file and execute commands

From Dev

Bash script to read from a file and execute commands

From Dev

Python shell script open less with +F option

From Dev

Python shell script open less with +F option

From Dev

Shell out into zsh and execute commands from bash script

From Dev

How to execute shell commands in a loop from within a bash script?

From Dev

Cannot execute shell commands in bash script

From Dev

Bash Script execute commands from a file but if cancel on to jump on next one

From Dev

How can we open minicom from current terminal and pass multiple commands to execute and exit to 1st terminal using shell script

From Dev

How to execute git commands from bash script?

From Dev

How to parse commands from a text file to a bash script in the CLI

From Dev

Can Jenkins run shell script on agent-less linux host

From Dev

Open a .raw file as text in less

From Dev

Writing commands to a file from a shell script

From Dev

How to execute commands in docker container as part of bash shell script

From Dev

How to open cygwin from python script, execute commands and take data from command line in file

From Dev

How to open cygwin from python script, execute commands and take data from command line in file

From Dev

Cannot execute a shell script from gradle file

From Dev

Execute gcloud commands in a bash script

From Dev

Bash script to spawn and execute commands

From Dev

Can't execute sqlplus commands in shell script created by PHP page

From Dev

Can't execute shell script commands in sandboxed cocoa app

From Dev

Can I use a shell or python script to execute commands in a chroot?

From Dev

Why is bash denying my shell script permission to open a text file with default text editor?

From Dev

Execute random file from a folder in a bash script

From Dev

bash script, execute shell from another shell and assign results to a variable

From Dev

bash script, execute shell from another shell and assign results to a variable

From Dev

Bash script won't compile LESS

From Dev

result of "less than" operator in bash script

Related Related

  1. 1

    Bash script to read from a file and execute commands

  2. 2

    Bash script to read from a file and execute commands

  3. 3

    Python shell script open less with +F option

  4. 4

    Python shell script open less with +F option

  5. 5

    Shell out into zsh and execute commands from bash script

  6. 6

    How to execute shell commands in a loop from within a bash script?

  7. 7

    Cannot execute shell commands in bash script

  8. 8

    Bash Script execute commands from a file but if cancel on to jump on next one

  9. 9

    How can we open minicom from current terminal and pass multiple commands to execute and exit to 1st terminal using shell script

  10. 10

    How to execute git commands from bash script?

  11. 11

    How to parse commands from a text file to a bash script in the CLI

  12. 12

    Can Jenkins run shell script on agent-less linux host

  13. 13

    Open a .raw file as text in less

  14. 14

    Writing commands to a file from a shell script

  15. 15

    How to execute commands in docker container as part of bash shell script

  16. 16

    How to open cygwin from python script, execute commands and take data from command line in file

  17. 17

    How to open cygwin from python script, execute commands and take data from command line in file

  18. 18

    Cannot execute a shell script from gradle file

  19. 19

    Execute gcloud commands in a bash script

  20. 20

    Bash script to spawn and execute commands

  21. 21

    Can't execute sqlplus commands in shell script created by PHP page

  22. 22

    Can't execute shell script commands in sandboxed cocoa app

  23. 23

    Can I use a shell or python script to execute commands in a chroot?

  24. 24

    Why is bash denying my shell script permission to open a text file with default text editor?

  25. 25

    Execute random file from a folder in a bash script

  26. 26

    bash script, execute shell from another shell and assign results to a variable

  27. 27

    bash script, execute shell from another shell and assign results to a variable

  28. 28

    Bash script won't compile LESS

  29. 29

    result of "less than" operator in bash script

HotTag

Archive