Linux shell reads rows of files line by line

Source: Internet
Author: User

Read a file line
    • For
      • Command substitution
      • Code block redirection
    • While
      • Pipe character
      • Code block redirection
For
IFS=$‘\n‘forin-l`do    (( count++ ))doneecho$count

Note:
For read, automatically press a space as an interval character. It is therefore necessary to define IFS to be delimited by line breaks

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

Note:
The WC needs to use redirection, otherwise the output format is "number of rows file name."

While
whileread line     do        (( count++ ))    donereadFile.log    echo$count

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

    readwhileread line    do        #(( count++ ))        echo"$line"    done    #echo $count

Note
The last output count is null, and it feels as if a pipe Fuzhou into a child process, and the child process variable cannot return to the parent process

Time

IME commands are used to print out a command or a program's execution time
The time command prints a standard error , in seconds, of how long a command takes to execute, the system time, and the time command.
Usage: Time [-P] Command [Argument ...]
The result of the time command consists of three rows: real, user, and Sys. We use real values here, and CPU time is divided into user and sys two blocks.

    • The real value represents the elapsed time from the beginning of the program to the end of the program execution, including the CPU.
    • The user value represents the program itself, and the time that the subroutines in the library it calls are used.
    • SYS is the time that is executed by a system call that is called directly or indirectly by the program.

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

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

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux shell reads rows of files line by line

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.