In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to read files line by line in the Shell script, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article, without saying much, follow the editor to have a look.
Method 1. Use input redirection
The easiest way to read a file line by line is to use input redirection in the while loop.
To demonstrate, create a text file called "mycontent.txt" here with the following contents:
[root@localhost ~] # cat mycontent.txt This is a sample fileWe are going through contentsline by lineto understand
Create a script called "example1.sh" that uses input redirection and loops:
[root@localhost ~] # cat example1.sh #! / bin/bashwhile read rowsdo echo "Line contents are: $rows" done
-start the while loop and save the contents of each line in the variable "rows"
-use echo to display the output, and the $rows variable is for each line in the text file
-use echo to display the output, including custom strings and variables, and the $rows variable is for each line in the text file
Tips: you can reduce the above script to one line of command, as follows:
[root@localhost ~] # while read rows; do echo "Line contents are: $rows"; done
-use a pipe to send the output of the cat command as input to the while loop.
-| the pipe character stores the cat output in the "$rows" variable.
-use echo to display the output, including custom strings and variables, and the $rows variable is for each line in the text file
Tips: you can reduce the above script to one line of command, as follows:
[root@localhost ~] # cat mycontent.txt | while read rows;do echo "Line contents are: $rows"; done
Method 3. Use the passed file name as a parameter
The third method is to append the text file name to the script when the script is executed by adding the $1 parameter.
Create a script file named "example3.sh" as follows:
[root@localhost ~] # cat example3.sh #! / bin/bashwhile read rowsdo echo "Line contents are: $rows" done
-start the while loop and save the contents of each line in the variable "rows"
-use echo to display the output, and the $rows variable is for each line in the text file
-read the contents of the file from the command line argument $1 using input redirection
Method 4. Use the awk command
By using the awk command, you can read the contents of the file line by line with only one command.
Create a script file named "example4.sh" as follows:
[root@localhost ~] # cat example4.sh #! / bin/bashcat mycontent.txt | awk'{print "Line contents are:" $0}'
Running result:
The above is how to read the file line by line in the Shell script. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.