Home  >  Article  >  Operation and Maintenance  >  Scenario linux--How to solve the hard coding problem caused by the read command?

Scenario linux--How to solve the hard coding problem caused by the read command?

PHP中文网
PHP中文网Original
2017-06-21 11:48:351674browse

Scenario

We know that the read command can read the contents of the file and assign the contents to variables.

Take the following data file as an example.

$ cat data.txt
1 201623210021 wangzhiguo 25
2 201623210022 yangjiangbo 26
3 201623210023 yangzhen 24
4 201623210024 wangdong 23
5 201623210025 songdong 25

The four columns of the above file are serial number (index), student number (number), name (name), and age (age). Use a shell script to read the file and output the value of each line:

$ cat read_data.sh
#!/bin/bash

cat data.txt | while read index number name age
do
    echo "index:${index}"
    echo "number:${number}"
    echo "name:${name}"
    echo "age:${age}"
    echo " "
done

Execute the script and check the results:

$ sh read_data.sh
index:1
number:201623210021
name:wangzhiguo
age:25
 
index:2
number:201623210022
name:yangjiangbo
age:26
 
index:3
number:201623210023
name:yangzhen
age:24
 
index:4
number:201623210024
name:wangdong
age:23
 
index:5
number:201623210025
name:songdong
age:25

I wonder if you have noticed that this implementation has obvious disadvantages:

  1. The column name (read index number name age) is explicitly specified in the code. If you just want to figure out the meaning of each column of the data file, you need to read the script;

  2. The name of each column is specified in this script. If you want to modify the English name of each field (for example, if you want the English name of the serial number to be changed to NUMBER), you need to modify the script and modify it in many places. ;

  3. This script reads the data file in a certain order, so if the column order in the data file changes, you still need to modify the script;

  4. If there are other data files that need to be read in this way, a new script needs to be rewritten based on the actual situation of the data file;

Although the above implementation method seems simple , but based on the above disadvantages, we should also optimize it.

Solution

The fundamental solution should be toWrite a script that is as versatile as possible, which does not depend on the number of columns, column order, column names (meanings), etc. of the data file itself.

You can store the field names of the data file in the first line of the data file. When reading a data file, first read the first row of the data file to obtain a list of field names; when reading other rows, make a one-to-one correspondence between the values ​​in the first row and the values ​​in the non-first row.

Data file

$ cat new_data.txt
index number name age
1 201623210021 wangzhiguo 25
2 201623210022 yangjiangbo 26
3 201623210023 yangzhen 24
4 201623210024 wangdong 23
5 201623210025 songdong 25

Script

$ cat new_read_data.sh
#!/bin/bash

# 读取文件头行,存于一个数组中
tablehead=(`head -n 1 new_data.txt`)

# 从文件第二行开始读取,按上述数组顺序读取各字段
tail -n +2 new_data.txt | while read ${tablehead[*]}
do
    # 遍历数组的下标,获取tablehead数组的对应值,以及以该值命名的变量的值
    for i in `seq 0 $((${#tablehead[@]}-1))`
    do
        temp=${tablehead[$i]}
        echo "${temp}:${!temp}"
    done
    echo ""
done

Result

$ sh new_read_data.sh
index:1
number:201623210021
name:wangzhiguo
age:25

index:2
number:201623210022
name:yangjiangbo
age:26

index:3
number:201623210023
name:yangzhen
age:24

index:4
number:201623210024
name:wangdong
age:23

index:5
number:201623210025
name:songdong
age:25

To write a more general script, you can also do some judgment and processing, such as: data File is passed in as a parameter, check the number of rows in the data file, check the number of columns in the data file, etc.

Extended knowledge

From the perspective of the improvement of the script, it is slightly more complicated than the original script, but it is more versatile.
If you find it difficult to read scripts, you can learn it in a targeted manner, especially the following knowledge points:

  • Related knowledge of arrays: array length, array content, array elements, etc.

  • The difference between variables ${abc} and ${!abc}

The above is the detailed content of Scenario linux--How to solve the hard coding problem caused by the read command?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn