It's normal behavior of sed
. From POSIX sed documentation:
Addresses in sed
An address is either a decimal number that counts input lines
cumulatively across files, a '$' character that addresses the last
line of input, or a context address (which consists of a BRE, as
described in Regular Expressions in sed , preceded and followed by a
delimiter, usually a slash).
An editing command with no addresses shall select every pattern space.
An editing command with one address shall select each pattern space
that matches the address.
An editing command with two addresses shall select the inclusive range
from the first pattern space that matches the first address through
the next pattern space that matches the second. (If the second address
is a number less than or equal to the line number first selected, only
one line shall be selected.) Starting at the first line following the
selected range, sed shall look again for the first address.
Thereafter, the process shall be repeated. Omitting either or both of
the address components in the following form produces undefined
results:
[address[,address]]
You can see, sed
will print the inclusive range from the first address through the next matched address.
In your case, 1,/1/p
, sed
print the first line because it matches address 1
. Then from second line, sed will search for second address that matches pattern /1/
. And stop print if found. Because from second line, you don't have any pattern that matches /1/
, so sed
print the rest.
In case with 1./2/p
, sed print the first line as above, then second line match pattern /2/
, sed
print it and repeat action for the rest. But You can not match address 1
for the rest, so sed
does not print anything.
An example:
$ echo 1 2 3 1 4 1 | tr ' ' $'\n' | sed -rn '1,/1/p'
1
2
3
1
Because you use GNU sed
, you can use form 0,addr2
:
0,addr2
Start out in "matched first address" state, until addr2 is
found. This is similar to 1,addr2, except that if addr2 matches
the very first line of input the 0,addr2 form will be at the end
of its range, whereas the 1,addr2 form will still be at the
beginning of its range. This works only when addr2 is a regular
expression.
So, your command become:
seq 1 4 | tr ' ' $'\n' | sed -rn '0,/'"$1"'/p'
Then:
$ ./1.sh 1
1
1,1
. And the issue has nothing to do with general regexes. It's about matching only one line with1,/pattern/
range. If you want to delete everything from the start of the file to some particular line, and that line happens to be the first one.