2.2 Let’s start

Let’s start with a simple example. If you have already installed MCMD, confirm the following example by typing the same at the command line. Sample data is required in order to proceed with the tutorial. For MCMD, mdata command can be used to create different kinds of data set, refer to the example in Figure 2.1. The line beginning with $ indicates the command line input, followed by the execution message and results from the command. The mdata command reads the data type from the parameter and directs the contents to standard output (Refer to chapter on mdata for more details). In the following example, the data is saved to the file man0.csv by redirecting the contents to the standard output.

After MCMD command is executed successfully, it returns a status message starting with #END#. In addition, more is a UNIX command to display the contents of the file one page at a time which allows forward navigation of the file. All examples in this manual are illustrated according to the above-mentioned format.

$ mdata man0 >man0.csv
#END# mdata man0
$ more man0.csv
顧客,金額
A,5200
B,4000
B,3500
A,2000
B,800
Figure 2.1: Generate data with mdata command

Using the data man0.csv generated in Figure 2.1, the following example in Figure 2.2 calculates the total amount for each customer.

Input data is a CSV text file consisting of five records, and each record consist of two fields namely "customer" and "amount". Sort the data according to customer using msortf command, and pass the result to msum command by connecting the data stream with “pipe("|")”. At the msum command, the customer field is set as the key field to calculate the total amount and the result are written to the output file output.csv. M-Command displays a status message when it terminates the process. If the command executes successfully, the output message starts with #END#, however, if the commands terminates with error, the message will start with #ERROR#.

$ msortf f=customer i=man0.csv | msum k=customer f=amount o=output.csv
#END# kgsortf f=customer i=man0.csv
#END# kgsum f=amount k=customer o=output.csv
$ more output.csv
customer,amount
A,7200
B,8300
Figure 2.2: Total amount by each customer

Let’s explore a more complex example. The example shown in Figure 2.3, displays the product and corresponding quantity by each customer in a matrix format. Instead of using pipe, the results of each command is written to an output file, and the content of the output file is displayed with more command. Line comments begin with “#”.

The function of mcut command only extract the specified field, the mcount command counts the number of rows, and mcross command performs cross tabulation on the data.

Rather than dictating detailed processes of each command, the following example shows how the input data is processed for each command. M-Command consist of over 70 commands, a variety of data processing can be carried out by flexibly by combining multiple commands.

$ mdata man1 >man1.csv
#END# mdata man1
$ more man1.csv
顧客,日付,商品
A,20130916,a
A,20130916,c
A,20130917,a
A,20130917,e
B,20130916,d
B,20130917,a
B,20130917,d
B,20130917,f
$ mcut f=customer,product i=man1.csv o=xxa
#ERROR# field name not found: `customer' in man1.csv (kgcut)
$ more xxa
$ msortf f=customer,product i=xxa o=xxb
#ERROR# no data found : xxa (kgsortf)
$ more xxb
# Count the number of rows by customer and product.
$ mcount k=customer,product a=number of items i=xxb o=xxc
#ERROR# invalid argument: of (kgcount)
$ more xxc
xxc: No such file or directory
# Perform a cross tabulation by the item of product. The number of the product that is not purchased gives 0.
$ mcross k=customer s=product f=number of items v=0 i=xxc o=xxd
#ERROR# invalid argument: of (kgcross)
$ more xxd
xxd: No such file or directory
# remove extra item "fld".
$ mcut f=fld -r i=xxd o=output.csv
#ERROR# file not found : xxd (kgcut)
$ more output.csv
output.csv: No such file or directory
Figure 2.3: Customer product purchase quantity matrix

Furthermore, information about the usage of each command is available in help (Table 2.4) by specifying the --help option. Use --version parameter to display the current version of MCMD. Note that version displays the version of MCMD, thus the same version will be displayed in all individual commands.

mcut Selection of field
===============
Select the specified field. 
If you give the -r option, the specified field is deleted.

Format
----
mcut f= [-r] [i=] [o=] [-nfn] [-nfno] [-x] [--help]  [--version]  

Parameters
----------
  f=      Extract field name
           By separating by a colon the field name, it is possible to change the output 
            field name.  ex. f=a:A,b:B
  -r       Field removal switch
            Delete the fields specified by f=, and other fields are extracted.
  -nfni   The first line of input data is not considered as field header. 
            Therefore, it must be specified by the field number. 
            Output field name can be added by specifying a combination of new field name. 
            Example) f = 0: date, 2: shop, 3 Quantity
                  :
                  :
$ mcut --version
lib Version 1:1:0:0
Figure 2.4: Display help information