Help on korn shell (RedHat)

30 pts.
Tags:
Korn Shell
Linux Scripts
Red Hat
Red Hat Enterprise Linux
Shell scripts
I have a file like that: ... Title 1     Abcdef     Ghijklm     Nopqrst     ... Title 2      Abcdef      Ghijkllm      Nopqrst      ... ... Under each block (Title) number of line could be between 0 and > 100 _________________________________________________________________ I'd like to know using script korn sheel how to keep only all lines between each Title?

Software/Hardware used:
Linux Red Hat

Answer Wiki

Thanks. We'll let you know when a new response is added.

I assume, you need some commands to embed into a korn shell script to get rid of title line…

If you want to keep all “non-title” lines, just use:

grep -v ‘^Title [0-9]*$’ filename

If you want to extract a specific “chapter”, you can use a combination of grep, sed, head, and tail:

grep -n ‘^Title [0-9]*$’ filename > index

then, assuming you need chapter 12 :

grep -A 1 ‘^Title 12$’ index | sed ‘s/:.*$//’

and you will have two lines with indexes of chapters 12 and 13 , which can be put into variables $index12 and $index13 and reused by tail and head:

tail +$[$index12+1] filename | head -$[$index13 – $index12 -1]

which will output the contents of chapter 12 without Title… headings. Unfortunately, I’m not familiar with korn shell syntax to assist you with putting output in variables and passing the required chapter number from commandline, but can assure you this works perfectly in bash script using $1 for passing the script argument and back-quotes ( `…` , $(…) ) for capturing the grep | sed output.

If you are interested in bash script – feel free to write back, otherwise make some research in ksh capabilities and syntax.

Good luck,

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Pjr
    Like that, I only have line Title N because, the content of index is Title N What I need is to have the content of each Title (All lines between each Title) and that, Title by Title. Thank for You help.
    30 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following