65.0k views
5 votes
Which of the following Linux commands will print lines from the file that contain matches to "changes" and "changed"?

a) grep "changes|changed" filename
b) sed -n '/changes|changed/p' filename
c) awk '/changes|changed/' filename
d) find "changes" "changed" filename

User Nanego
by
8.1k points

1 Answer

5 votes

Final answer:

The correct Linux command to print lines with 'changes' or 'changed' is 'grep "changes|changed" filename'. The 'grep' command uses patterns to search text, where '|' serves as a logical OR. Both 'sed' without an escape before '|' and 'find' for file names, are not correct uses for this case.

Step-by-step explanation:

The command that will print lines from the file that contain matches to "changes" and "changed" in Linux is grep "changes|changed" filename. This command uses the grep utility, which stands for global regular expression print. It is designed to search for text within files using patterns. When using grep, the pipe symbol '|' acts as a logical OR operator, meaning that it will match lines containing either "changes" or "changed".The other commands listed have issues:sed command would also work correctly but it requires an escape character before the pipe symbol '|', otherwise, it is interpreted as a literal character.awk command has the correct syntax to perform the operation as well, as awk is another text processing utility that can handle such patterns.

The find command is not used for searching inside the contents of a file, but rather for searching for files and directories.The correct Linux command to print lines from a file that contain matches to both 'changes' and 'changed' is the grep command. Option (a) grep 'changes|changed' filename is the correct command. This command uses the regular expression 'changes|changed' in the grep pattern to find lines that contain either 'changes' or 'changed'.

User LEMUEL  ADANE
by
9.1k points