How does the shell read specific lines and columns of a file?
In the shell, you can use the following command to read specific lines and columns of a file:
- Retrieve content from a specified line.
sed -n '<行号>p' <文件路径>
For example, to read the contents of the 5th line of the file file.txt, you can use the following command:
sed -n '5p' file.txt
- Fetch content in specific column:
awk '{print $<列号>}' <文件路径>
For example, to read the content of the 3rd column in the file file.txt, you can use the following command:
awk '{print $3}' file.txt
- Read content of a specific row and column.
awk 'NR==<行号> {print $<列号>}' <文件路径>
For example, to read the content in the 3rd column of the 5th row of the file file.txt, you can use the following command:
awk 'NR==5 {print $3}' file.txt
Note: Both row number and column number start counting from 1.