Wonder if someone can explain the difference between Next (N) and the next(n) command in sed. i was trying following.
this is my input file
Consult Section 3.1 in the Owner and Operator Guide
Consult Section 3.1 in the Owner and Operator Guide
for a description of the tape drives
available on your system.
i was using following (next command)
/Operator/{
n
s/Owner and Operator Guide/Installation Guide/
}
result is
Consult Section 3.1 in the Owner and Operator Guide
Consult Section 3.1 in the Installation Guide
for a description of the tape drives
available on your system.
then i was using following
/Operator/{
N
s/Owner and Operator Guide/Installation Guide/
}
result is
Consult Section 3.1 in the Installation Guide
Consult Section 3.1 in the Owner and Operator Guide
for a description of the tape drives
available on your system.
can you please explain why I'm getting two different results ? do i need to use Next command (N) only in multiline pattern space ? and where to use next (n) command.
If i use sed 's/Owner and Operator Guide/Installation Guide/' input.txt will change everything as expected.
The n
command prints out the pattern space (unless the "-n" flag is used), clears the pattern space, and reads in the next line. In your example,
/Operator/{
n
s/Owner and Operator Guide/Installation Guide/
}
 /Operator/
is matched for the first line. After performing the n
command, we print the pattern space so your first line is print as is. Second line is loaded in to pattern space and the substitution is performed. As a result, you see the changes on the second line.
The N
command, unlike n
command, does not print the current pattern space, and does not clear it. Instead it appends the next line to the pattern space separated by a newline. As a result your substitution is performed on the first line since you are not using a global flag.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.