I have a file
line a - this is line a
line b - this is line b
line c - this is line c
line d - this is line d
line e - this is line e
The question is: How can I output the lines starting from "line b" till "line d" using bash commands? I mean, to obtain:
"line b - this is line b
line c - this is line c
line d - this is line d"
You can do it using bash alone, though I agree with Pax that using other tools is probably a better solution. Here's a bash-only solution:
If by bash, you mean actually bash alone, I can't help you. You really should be using the right tools for the job. If you mean standard UNIX utilities that you can call from bash, I would be using
awk
for that.This outputs:
The way it works is simple.
I've made an assumption here that you don't want to exit on a line d unless you're already echoing. If that's wrong, move the exit outside of the if statement for line d:
Then, if you get a line d before your line b, it will just exit without echoing anything.
The
"/^line X/"
-type clauses can be made very powerful to match pretty well anything you can throw at it.for your set of sample data:
Or
Your example is not enough to infer what you want in the general case, but assuming you want to remove the first and last line, you can simply use
Here
tail -n+2
prints all the lines starting from the second, andhead -n-1
prints all the lines except the last.Another approach which depends on what you mean: