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

How to realize Shell script to read text file line by line

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to realize Shell script to read text file line by line". In daily operation, I believe that many people have doubts about how to realize Shell script to read text file line by line. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the question of "how to realize Shell script to read text file line by line". Next, please follow the editor to study!

There are many examples of shell script reading text files on the Internet, but they don't tell the whole story, only half of it. For example, read lines of text in the following format from an testfile file:

The code is as follows:

$vi testfile

Ls-a-l / bin | sort

Ls-a-l / bin | sort | wc

Ls-a-l | grep sh | wc

Ls-a-l

Ls-a-l | sort | wc

The most common example of line by line reading the contents of a file is:

The code is as follows:

$vi readfile

#! / bin/sh

Testfile=$1

While read-r line

Do

Echo $line

Done < $testfile

$chmod + x readfile

$. / readfile testfile

Ls-a-l / bin | sort

Ls-a-l / bin | sort | wc

Ls-a-l | grep sh | wc

Ls-a-l

Ls-a-l | sort | wc

The problem with this example is that after reading the text line, the text format has changed, which is not exactly consistent with the content of the original testfile file, and some space characters are automatically deleted. What causes it? Because IFS, if IFS is not explicitly specified in shell script, IFS defaults to dividing spaces, tabulation, line breaks, and so on, so the extra spaces and line breaks in the above lines of text are automatically indented.

What if you want to output the original format of the testfile file and print out each line (as a whole) intact? At this point, you need to specify the IFS variable and tell shell to read in rows.

The code is as follows:

$vi readfile

#! / bin/sh

IFS= ""

Testfile=$1

While read-r line

Do

Echo $line

Done < $testfile

$. / readfile testfile

Ls-a-l / bin | sort

Ls-a-l / bin | sort | wc

Ls-a-l | grep sh | wc

Ls-a-l

Ls-a-l | sort | wc

The output of the above two methods is not similar, what does it have to do with it, the first one is more beautiful? It matters a lot. VPSee wrote a C program to simulate shell yesterday, and then wrote a shell script to test the C program. The script needs to read the whole line from the above testfile to the C program. If you follow the above two methods, you will get two different input formats, which have completely different meanings:

The code is as follows:

$. / mypipe ls-a-l | sort | wc

$. / mypipe "ls-a-l | sort | wc"

Obviously what I want is the second input, passing "ls-a-l | sort | wc" to my mypipe as a whole to test whether my mypipe can correctly recognize the various commands in the string.

If you don't use IFS, there is another way to get the effect of the second method above:

The code is as follows:

#! / bin/sh

Testfile=$1

X = `wc-l $testfile | awk'{print $1}'`

I, 1

While [$I-le $x]

Do

Echo "`head-$I $testfile | tail-1`"

I = `expr $I + 1`

Done

At this point, the study on "how to implement Shell scripts to read text files line by line" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report