Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Several common methods for shell to read files by line

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

1. By redirecting the + read command

While read linedo # Operation on each line of string shell_commandsdone < File.txt

two。 Redefine through cat + IFS

IFS_old=$IFSIFS=$'\ n'for line in $(cat File.txt) do shell_commandsdoneIFS=$IFS_old

3. Pipe + read command (often seen, but not easy to use. )

Cat File.txt | read linedo shell_commandsdone

Note: this approach has a very obvious flaw. Variables defined by shell_command will "fail" at the end of the loop!

For example:

The file1.txt is as follows:

Abcd

The shell code for method 1:

# number of lines line_count=0# file contents file_content= "# start reading while read linedo echo" I read: "echo ${line} line_count=$ (expr $line_count + 1) echo" this is the ${line_count} line "file_content=" $file_content $line "done"

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report