Linux Shell Learning: How to iterate through the lines, words, and characters in a file

Source: Internet
Author: User
Tags stdin linux

Iterating and traversing the lines, words, and characters in a file is a very common operation when you are working on a text file. Using a simple loop for iterations, plus a redirection from stdin or files, is the basic way to iterate through the lines, words, and characters in a file.

Do not say much nonsense, immediately to see how to achieve it.

1, each row in the iteration
Use a while loop to read from standard input, because to read in standard input, the file is redirected so that it is redirected to stdin, as follows:

while read line;  
Do  
echo $line;  
Done < file.txt

The first line of code reads a row from stdin, and the stdin source is file.txt, because the last line redirects with the data stream, redirecting the contents of the file.txt to stdin.

2, each word in the iteration line
We can use a for loop to iterate through a line of words, as follows:

Read line;  
for word in $line;  
Do  
echo $word;  
Done

The first line of code, reading a row from stdin, and then iterating through all the words in a row with a For loop, and outputting, is really very simple and practical.

3, iterate each character in a word
Iterating each character from a word is one of the most difficult of the three iterations, because extracting characters from a word requires some skill, as follows:

The variable i is iterated using the For loop, with an iteration range from 0 to the length of the character-1. How do you remove the characters from the word? We can use a special expression to take out the first letter of the word, ${string:start_position:count_of_characters}, which means to return a string of strings, from the first Start_ Position a string of count_of_characters characters that, for the first character in a single word, is, of course, a substring of length 1, starting with the character of string, which is the substring extraction technique. So the code is as follows:

for ((i=0; i<${#word}; ++i))  
do  
Echo ${word:i:1};  
Done

Note: ${#word} returns the length of the value of the variable word, which is the length of the word.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/Linux/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.