sed -n '/xyz/,$ p' input_file | sed -n '1,/xyz/ p'
If you are in solaries: you can use
/usr/xpg4/bin/awk (or) define such that even awk will use nawk only!!! a synonym minght be!!
WATCH OUT : This code will exit at the second occurrence met so it does not exactly behave as you specified (since you expect thing be printed until the last occurrence of the pattern
# echo "one
> one
> one
> one
> bla
> " | nawk 'f && /one/{print;exit}/one/{f=1}f'
one
one
#
The following sed script should do the same job as that nawk but in one pass. It will ignore any lines before the first occurence of the pattern. Beginning with the first occurrence of pattern inclusive, append the line in the pattern space to the hold space. When a matching line is found, we output the accumulated chunk and clear the hold space. Finally, the next line in the file is read into the pattern space before returning to the top of the loop:
#n
/xyz/! d
:top
H
/xyz/ {
s/.*//
x
s/\n//
p
}
n
b top
If that is saved in a file named first-last.sed, you can invoke it thusly: sed -f first-last.sed test.txt
If for some reason you'd rather not store the script in a file, but prefer to have it inline:
sed -n '/xyz/!d; :top
H; /xyz/ {s/.*//; x; s/\n//; p;}; n; b top' test.txt
To ensure maximum portability, this sed script uses only POSIX-compliant syntax (e.g. labels are terminated by a newline).
Looks like it's not looping properly. Try the following:
sed -n '/xyz/!d; :top
H; /xyz/ {s/.*//; x; s/\n//; p;}; n;
b top
' rem
If that doesn't work, I'd be curious to know if the script file version works (using the code that begins with #n in a file and executing it with sed's -f option).
this code will not give the wanted output since it will just toggle between on and off as soon as it find an occurence of the pattern and may skip some wanted lines.
# echo "one
> bla
> one
> bla
> bla
> one" | sed -n '/one/,/one/p'
one
bla
one
one
#
---------- Post updated at 07:37 PM ---------- Previous update was at 07:16 PM ----------