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:

  1. 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
  1. 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
  1. 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.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds