How to read a file line by line in Bash?

When it is necessary to read a file line by line in Bash (Unix/Linux command line environment), there are various ways to achieve this goal. This article will detail the different ways of reading a file line by line in Bash along with their uses and examples. The following are detailed instructions for reading a file line by line in Bash.

insert image description here

Method 1: Use while loop and read command

In Bash, you can use a while loop combined with the read command to read a file line by line. The read command is used to read a line from standard input or a file and store it in a variable. Here is sample code using this method:

#!/bin/bash

file="example.txt"

# 检查文件是否存在
if [ -f "$file" ]; then
  # 逐行读取文件
  while IFS= read -r line; do
    echo "$line"
  done < "$file"
else
  echo "文件 $file 不存在"
fi

In the above example, we first check if the file exists. If the file exists, we use a while loop to read the file line by line. IFS=Used to disable automatic trimming of line separators, ensuring that the contents of each line are read as-is. Each row read is stored in a variable lineand can be processed on demand. In this example, we simply output each line to standard output.

Method 2: Use the cat command and pipeline

Another way to read a file line by line is to use the cat command with the pipe operator ( |) to pass the contents of the file to a while loop. Here is sample code using this method:

#!/bin/bash

file="example.txt"

# 检查文件是否存在
if [ -f "$file" ]; then
  # 逐行读取文件
  cat "$file" | while IFS= read -r line; do
    echo "$line"
  done
else
 当我们使用`cat`命令和管道操作符`|`将文件的内容传递给`while`循环时,每一行都会作为标准输入传递给`read`命令。`read`命令将每行的内容存储在变量`line`中,并在循环体内对其进行处理。

需要注意的是,使用这种方法时,`while`循环将在子进程中运行,因此在循环体内对变量的修改不会影响到父进程。

## 方法三:使用grep命令

另一种逐行读取文件的方法是使用`grep`命令并指定行号。这种方法适用于只需要处理文件中特定行的情况。以下是使用该方法的示例代码:

```bash
#!/bin/bash

file="example.txt"

# 检查文件是否存在
if [ -f "$file" ]; then
  # 逐行读取文件
  line_number=1
  while IFS= read -r line; do
    if [ "$line_number" -eq 3 ]; then
      echo "第 $line_number 行的内容是: $line"
    fi
    line_number=$((line_number + 1))
  done < <(grep -n "" "$file" | cut -d: -f2-)
else
  echo "文件 $file 不存在"
fi

In the above example, we used grepthe command and an empty pattern ( "") to get the line number and content of each line in the file. We then use cutthe command to extract the line content and pass it to whilethe loop for processing. In this example, we only print the contents of the third line in the file.

< <(command)syntax for passing the output of a command as a file to the loop to be read line by line.

These methods provide different ways of reading a file line by line in Bash. Depending on the specific needs, you can choose the appropriate method to process each line of the file. These methods provide flexible and reliable solutions for working with files in Bash, whether iterating over file contents, extracting specific lines, or otherwise.

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/131447020