How does Unix shell read files by row and parse rows?

Source: Internet
Author: User

Read files by row:

Java code
  1. While read myline
  2. Do
  3. Echo $ myline
  4. Done </etc/passwd

This program is to put the input file/Etc/passwdOne output.

If we only need to output username and home directory, We need to parse the file.
Each row is roughly as follows, with the column separator: username in the first column and home directory in the sixth column.

Java code
  1. Ecrane :! : 300801: 1: Eric Crane = ecrane@us.ibm.com:/home/ecrane:/usr/bin/KSh

We only need to print the two columns:

Java code
  1. While read myline
  2. Do
  3. Echo $ myline | cut-D:-F1, 6
  4. Done </etc/passwd

The program is very simple.-D: is to define the separator:,-F1, 6 is to specify the first and sixth columns of the operation.

Of course, there is another way to use the awk command:

Java code
  1. While read myline
  2. Do
  3. Echo $ myline | awk-F: '{print $1, $6 }'
  4. Done </etc/passwd
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.