Scenario linux--How to Solve the Hard Coding Problem of read Command

scene

We know that the read command reads the contents of the file and assigns them 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 documents are index, number, name and age. Read the file with shell script 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 to see 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 found out that there are obvious drawbacks in this way of implementation:

  1. The column name (read index number name age) is explicitly specified in the code. If you just want to understand the meaning of each column of the data file, you need to read the script.
  2. The script specifies the name of each column. If you want to change the English name of each field (for example, the English name of the serial number wants to be changed to NUMBER), you need to modify the script and modify many places.
  3. The script reads the data file in a certain order, so if the column order in the data file changes, the script still needs to be modified.
  4. If other data files need to be read in this way, a new script needs to be rewritten according to the actual situation of the data files.

Although the above-mentioned implementation seems simple, based on the above drawbacks, we should optimize it.

programme

The fundamental solution should be to write scripts as general as possible, independent of the number of columns, column order, column name (meaning) of the data file itself.

The field names of the data file can be stored in the first line of the data file. When reading a data file, first read the first line of the data file to get a list of field names; when reading other lines, the value of the first line corresponds to the value of the non-first line.

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

# Read the header line and store it in an array
tablehead=(`head -n 1 new_data.txt`)

# Read from the second line of the file and read the fields in the order of the above array
tail -n +2 new_data.txt | while read ${tablehead[*]}
do
    # Traverse the subscripts of the array to get the corresponding value of the tablehead array and the value of the variable named after that value
    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, we can also make some judgments and processing, such as: data files as parameters, check the number of rows of data files, check the number of columns of data files, and so on.

Expanding knowledge

From the point of view of script improvement, it is slightly more complex than the original script, but more general.
If you find it hard to read scripts, you can learn them pertinently, especially the following knowledge points:

  • Array related knowledge: array length, array content, array elements, etc.
  • The difference between variables ${abc} and ${abc}

Keywords: Linux shell

Added by Saethyr on Mon, 24 Jun 2019 00:20:47 +0300