0

My sed or awk can print line between two patterns, but how can I make it display one by one?

Example - It should display the first part of the output, and when I press Space it should display the 2nd part of the output, and so on until it has displayed all parts matching the pattern.

---------------------START HERE--------------------
Server Name : ABC 
xxxx 
xxxx 
xxxx 
xxxx 
---------------------END HERE---------------------- 

---------------------START HERE--------------------
Server Name : XYZ 
YYYY 
YYYY 
YYYY 
---------------------END HERE-------------------- 

For a file containing the above information, how may I display the information one by one, with a break/pause between until I press a key to show the next output.

My sed command

sed -n '/START/,/END/p' /tmp/output 

displays everything at once.

  • Add your sed command to print lines between two patterns to your question. – Cyrus Jul 14 at 11:57
  • sed and awk are designed for non-interactive usage, so you need to combine it with a different tool. – Philippos Jul 15 at 7:02
0

With sed, you can force the processing to "pause" when it hits the END HERE pattern:

sed -n -e '/START HERE/,/END HERE/{p' -e '/END HERE/r /dev/stdin' -e '}' file

or, as a sed script:

/START HERE/,/END HERE/ {
    p
    /END HERE/ r /dev/stdin
}

This simply tests for the END HERE pattern in the input and starts reading from standard input. This temporarily halts the processing of the data until the r command is done.

You "unpause" by pressing Ctrl+D.

The same thing with awk:

awk '/START HERE/,/END HERE/ { print; if (/END HERE/) getline dummy <"/dev/stdin" }' file

Here, since we use getline to get input from the user, it's enough to press Enter to display the next section of output.

  • Kusalananda . Thank you , Yes it work also . – Bangau Putih Jul 17 at 3:52
0

Are you ok with Python?

from __future__ import print_function
import fileinput

show = False

for line in fileinput.input('a.txt'):
  if "START HERE" in line:
    show = True
    continue
  if "END HERE" in line:
    show = False
    try:
      input("Press ENTER to continue")
    except:
      pass
    print(chr(27) + "[2J") # Clear screen
    continue
  if show:
    print(line, end='')
  • HI , Fedor Dikarev . I not familiar with python but I just search on how to call the python script within shell script. I will try that . Many Thank with your help. But if someone else also can help to provide a shell script also really appreciate. – Bangau Putih Jul 15 at 6:38
  • HI , Fedor Dikarev . i'm using 2.7 phyton . so getting error on print(line, end='') - I fixed the issue (found from MR. google) but there is a another issue 1. there is a space for each output. 2. it start read from 2nd output not the first output. 3. Error Press ENTER to continue Traceback (most recent call last): File "./disp.py", line 14, in <module> input("Press ENTER to continue") File "<string>", line 0 ^ SyntaxError: unexpected EOF while parsing – Bangau Putih Jul 15 at 7:15
  • 4. how can i make this script read permanent file without to supply the input files. – Bangau Putih Jul 15 at 7:17
  • 5. how to make a screen clear after each output or after press enter ----absolutely_unused_variable = os.system("cls") – Bangau Putih Jul 15 at 7:18
  • @BangauPutih I updated my answer: added from __future__ import print_function, add filename on fileinput.input('a.txt'):, add try/except block for input function, added print escape sequence to clear screeb. – Fedor Dikarev Jul 15 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.