In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to write Shell script to realize iterative output of lines, words and characters in files". In daily operation, I believe that many people have doubts about how to write Shell scripts to realize iterative output of lines, words and characters in files. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful for you to answer the question of "how to write a Shell script to iterate the output of lines, words and characters in a file"! Next, please follow the editor to study!
1. Iterate through each line of the text
Use the while loop to read from standard input, because to read in standard input, you redirect the file to stdin, as follows:
The code is as follows:
While read line
Do
Echo $line
Done < file.txt
The first line of code reads one line from stdin, while the source of stdin is file.txt, because the last line redirects the contents of file.txt to stdin using data stream redirection.
2. Iterate over each word in a line
We can use the for loop to iterate over the words in a line, as follows:
The code is as follows:
Read line
For word in $line
Do
Echo $word
Done
The first line of code, reading a line from stdin, then iterating over all the words in a line with a for loop, and outputting it, is really simple and practical.
3. Iterate over each character in a word
Iterating over each character from a word is arguably the most difficult of the three iterations, because it takes some skill to extract characters from a word, as follows:
Use the for loop to iterate over the variable I, ranging from 0 to the length of the character-1. So how do you take out the characters in the word? We can use a special expression to take out the first letter in the word, ${string:start_position:count_of_characters}, which means to return the string consisting of count_of_characters characters from the start _ position in the string string, and for iterating the first character in a word, of course, from the I character of string, return a substring of length 1, this is the substring extraction technique. So the code is as follows:
The code is as follows:
For ((iTuno; I)
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.