How to use Perl to cobble together data from different columns in a table and then use the "|" To connect together

Source: Internet
Author: User
Tags perl script

Recently wrote a Perl script that implements the function of piecing together data from two columns in a table and then using the "|" Connected together.

The table reads as follows:

Employee number Employee Name Position Date of entry
1001 Tom Sales 1980/12/17 0:00:00
1002 John doe Financial 1981/02/20 0:00:00
1003 Harry Manager 1981/02/22 0:00:00
1004 As Accounting 1981/04/02 0:00:00

Requirements are as follows:

Put the employee name and entry date together in the form of key-value pairs, and then use the pieced-together data with the "|" Connected together.

The results are as follows:

Zhang San: 1980/12/17 0:00:00| John Doe: 1981/02/20 0:00:00| Harry: 1981/02/22 0:00:00| Chen II: 1981/04/02 0:00:00

The script is as follows:

#!/usr/bin/perluse strict;my $line; while (<>) {     s/\r\n//;     if (/(\s+) \s+ (\s+) \s+ (\s+) \s+ (. *)/) {     $line. =$2. ': '. $4. ' | ';     }} print "$line \ n";

The results of the script execution are as follows:

Employee Name: entry date | Zhang San: 1980/12/17 0:00:00| John Doe: 1981/02/20 0:00:00| Harry: 1981/02/22 0:00:00| Chen II: 1981/04/02 0:00:00|

Basically meet the requirements.

Summarize:

1. \s matches white space characters in Perl. \s matches non-whitespace characters. Originally started with \w, but \w default matches are English characters, numbers and _. Does not match Chinese characters, so I use \s to represent it here.

2. This requirement is quite common in practice, and the common database is easy to make up for a few columns, but it is a challenge to put together the results once again, and Perl solves the problem well.

How to use Perl to cobble together data from different columns in a table and then use the "|" To connect together

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.