Linux reads the specified domain of the data file into the shell script

Source: Internet
Author: User

This example shows how to read a specific field (field) from a data file in a Linux shell script and manipulate it. For example, suppose the format of the file Employees.txt is {employee-name}:{employee-id}:{department-name}, separated by a colon, as shown below.

$ cat Employees.txt
Emma thomas:100:marketing
Alex Jason:200:sales
Madison randy:300:product Development
Sanjay Gupta:400:support
Nisha Singh:500:sales

The following shell script shows how to read a specific field (field) from this employee.txt file.

$ VI read-employees.sh
#!/bin/bash
Ifs=:
echo "Employee Names:"
echo "---------------"
While read name Empid dept
Todo
echo "$name is part of $dept Department"
Done < ~/employees.txt

Execute this script after giving the script executable permissions

$ chmod u+x read-employees.sh
$./read-employees.sh
Employee Names:

Note: In Linux shell is a good command to facilitate our use, but also convenient for hackers to use, so we should pay attention to the shell security and permissions on the set OH.

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.