Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
591 views
in Technique[技术] by (71.8m points)

linux - 如何编写Shell脚本以基于输入过滤数据(How to write a shell script to filter data based on the input)

Below is the scenario:

(下面是方案:)

Given a file that contains a log (timestamp, customer id, page id), please write a script to parse it and output the list of pages visited by each customer.

(给定一个包含日志的文件(时间戳,客户ID,页面ID),请编写脚本进行解析并输出每个客户访问的页面列表。)

Input CSV File:

(输入CSV文件:)

 Time, Customer ID, Page ID 1, C1, P1 2, C2, P2 3, C3, P3 4, C2, P1 5, C2, P3 6, C2, P2 7, C1, P3 8, C1, P2 9, C3, P1 10, C2, P1 11, C2, P3 12, C2, P2 13, C1, P1 14, C1, P3 15, C1, P2 

Example execution of script.

(脚本执行示例。)

The Customer ID must be passed as a parameter.

(客户ID必须作为参数传递。)

That is, ./script "C1"

(也就是说,。/ ./script "C1")

Output:

(输出:)

 P1, P3, P2, P1, P3, P2 

As of now, I got the following code to parse a CSV file

(到目前为止,我得到了以下代码来解析CSV文件)

Code:

(码:)

INPUT=/filepath/customers.csv
CUSTOMER_NAME=$1
OLDIFS=$IFS
IFS=','
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read f1 f2 f3
do
        echo "Time : $f1"
        echo "Customer ID : $f2"
        echo "Page_ID : $f3"
done < $INPUT
IFS=$OLDIFS

How can I write the logic to filter the data based on the customer input?

(如何编写逻辑以根据客户输入过滤数据?)

  ask by user2075759 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your scrip was not far from what you wanted.

(您的股票离您想要的不远。)

Let's see what is missing:

(让我们看看缺少了什么:)

  • you read the customer ID in $f2 but when reading, the space between comma and the customer name is stored in variable.

    (您在$f2读取了客户ID,但是在读取时,逗号和客户名称之间的空格存储在变量中。)

    (Check it with echo "f2 is: \"$f2\"" ).

    ((使用echo "f2 is: \"$f2\"" )。)

    To remove the extra space, you can use tr : CNAME=$(echo "$f2" | tr -d ' \t') will remove space from f2 and store the result in CNAME

    (要删除多余的空间,可以使用trCNAME=$(echo "$f2" | tr -d ' \t')将删除f2空间并将结果存储在CNAME)

  • Once you've get the customer name from file, you can compare it with CUSTOMER_NAME

    (从文件中获取客户名称后,可以将其与CUSTOMER_NAME进行比较)

  • For the output, you can store the pages index in a RESULT variable inserting the necessary comma.

    (对于输出,您可以将页面索引存储在RESULT变量中,并插入必要的逗号。)

So your script could looks like:

(因此您的脚本可能如下所示:)

#!/bin/sh
INPUT=customers.csv                                            
CUSTOMER_NAME=$1                                               
OLDIFS=$IFS                                                    
IFS=','         
RESULT=""
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }  
while read f1 f2 f3                                            
do                                                             
    CNAME=$(echo "$f2" | tr -d ' ')                              
    if [ "$CNAME" = "$CUSTOMER_NAME" ]                         
    then                                                                   
        if [ -z "$RESULT" ] 
        then
            RESULT="$f3"
        else
            RESULT="$RESULT,$f3"
        fi
    fi                                                         
done < $INPUT                                                  
IFS=$OLDIFS
echo "$RESULT"

Note that if one of the customer ID has a space in it, this script won't work.

(请注意,如果其中一个客户ID包含空格,则此脚本将无法工作。)

You should consider using awk as suggested in comments.

(您应该考虑按照注释中的建议使用awk 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...