Linuxshell reads the file line by line

Source: Internet
Author: User

Linuxshell reads the file line by line
Read File lines
Command to replace code block redirection while
Pipeline Code block redirection

IFS=$'\n'for line in `ls -l`do    (( count++ ))doneecho $count

Note:
When reading for, the system automatically uses spaces as the delimiter. Therefore, you need to define IFS as separated by line breaks

ls -l > forout.logmaxlength=$(wc -l < forout.log)for i in `seq $maxlength`do (( count++ ))done < forout.logecho $count

Note:
Wc requires redirection. Otherwise, the output format is "number of rows file name ".

while
while read line do (( count++ )) done < readFile.log echo $count

Note:
If the while condition expression does not write read, line is output, but the final result is the same.

 cat readFile.log | while read line do #(( count++ )) echo "$line" done #echo $count

Note:
The final output count is a null value. It feels that a pipeline is a sub-process, and the sub-process variables cannot return the parent process.

time

Ime commandPrint the execution time of a command or program
The time Command prints the time, system time, and time used to execute a command in seconds.Standard Error.
Usage: time [-p] Command [Argument... ]
The time Command result consists of three lines: real, user, and sys. Here we use real values, and the CPU is divided into user and sys.

The real value indicates the time consumed from the beginning of the program to the end of the program execution, including the CPU time. The user value indicates the time used by the program and the Child routines in the library it calls. Sys is the time when a system call is executed by a program directly or indirectly.

Test the above four methods
1.
Real 0m0. 022 s
User 0m0. 000 s
Sys 0m0. 004 s

2.
real 0m0.066s
user 0m0.004s
sys 0m0.016s

3.
real 0m0.001s
user 0m0.000s
sys 0m0.000s

4.
real 0m0.015s
user 0m0.000s
sys 0m0.008s

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.