Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the methods for shell to read files by line?

Shulou Source: shulou.com Published: 2022-06-03 07:51:44 11月15日 Update

This article mainly introduces "what are the methods for shell to read files by line". In daily operation, I believe that many people have doubts about the method of reading files by line in shell. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is the method of reading files by line by shell?" Next, please follow the editor to study!

There are many methods, three of which are written below:

Write method 1:

The code is as follows:

#! / bin/bash

While read line

Do

Echo $line

Done < filename (file to be read)

Writing method 2:

The code is as follows:

#! / bin/bash

Cat filename (File to be read) | while read line

Do

Echo $line

Done

The third way of writing:

The code is as follows:

For line in `cat filename (file to be read) `

Do

Echo $line

Done

Description:

There is a difference between for line-by-line reading and while line-by-line reading, such as:

The code is as follows:

$cat file

1111

2222

3333 4444 555

$cat file | while read line; do echo $line; done

1111

2222

3333 4444 555

$for line in $(

Tags: Files methods code learning writing more help practical next articles theories knowledge articles websites materials follow questions easy to use practice solutions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MySQL Shulou Information Redmi Xiaomi