To print only the lines that match a specific pattern using the sed
command, you can use the -n
option and the p
command.
The -n
option causes sed
to suppress normal output, so that only lines that are explicitly printed using the p
command are displayed.
For example, to print only the lines that contain the word "apple" in the file input.txt
, you can use the following sed
command:
sed -n '/apple/p' input.txt
This will print all lines in the input.txt
file that contain the word "apple".
You can also use this approach to print lines that match a specific pattern. For example, to print only the lines that start with the word "apple", you can use the following sed
command:
sed -n '/^apple/p' input.txt
This will print all lines in the input.txt
file that start with the word "apple".
It is important to note that the p
command is only applied to lines that match the pattern specified in the sed
command. If you want to print all lines that match the pattern, you will need to use the -n
option and the p
command in combination with a regular expression that matches the pattern you want to find.