hi,
I have the following file
hello
world
this
is
to
say
bye
to
everyone
so
bye
I want to get the lines from hello to the first bye inclusive into another file?
how can I do this
thanks
hi,
I have the following file
hello
world
this
is
to
say
bye
to
everyone
so
bye
I want to get the lines from hello to the first bye inclusive into another file?
how can I do this
thanks
To keep the forums high quality for all users, please take the time to format your posts correctly.
First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags
```text
and
```
by hand.)
Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.
Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.
Thank You.
The UNIX and Linux Forums
---------- Post updated at 02:28 PM ---------- Previous update was at 02:26 PM ----------
sed -n '/hello/,/bye/p' myFile
Don't do it youngster, learn a bit of awk, it will give your life new meaning
nawk '/bye/{print;exit} /hello/{p=1}p' infile > another.file
thanks, that worked perfectly
HI Can u please tell if i pass the ,
start and end patterns as arguments i mean
nawk '/$end_Pattern/{print;exit} /$start_Pattern/{p=1}p' infile > another.file
where end_Pattern=bye
start_Pattern=hello
but the above is not working good
Hi.
You're trying to use shell variables inside awk, which doesn't work.
Either use something like:
awk '/'$start_pattern'/,/'$end_pattern'/' infile
or (taking the steadyonabix example)
awk -v S=$start_pattern -v E=$end_pattern '$0 ~ E {print;exit} $0 ~ S{p=1}p' infile > another.file
Thaks @scott
The First Command giving the lines along with the start_pattern and end_pattern.
can u please tell me the exact command to output the lines only between the pattern's.
You never said you didn't want the start and end patterns. And the original question asked for them. :rolleyes:
nawk -v E=$E -v S=$S '$0 ~ E {exit} $0 ~ S {getline; p=1}p' infile
Good solution.
sed '0,/hello/d;/bye/,$d' infile > outfile
For those not using a sed that supports the 0 address extension (as far as I know, that's a GNU extension), the following alternative will work:
sed -n '/hello/ {
:next
n; /bye/q; p; b next
}' infile > outfile
Regards,
alister
awk '/bye/{p=0}p;/hello/{p=1}' infile