作者姓名: Yuan Huang

系統

使用 SQL 取得有開過 EDM 的會員部份資訊

發送 EDM 的時候會夾帶 ID,可以藉此知道是哪一位會員有開過 EDM。

從 Web Server 的 Log 撈出不重複的資料後,使用 phpMyAdmin 匯入到 MySQL。

再跟會員資料表做 JOIN,可以得到有開過 EDM 的會員部份資訊。

(JOIN 後的資料表)

SQL 語法如下:

// customer 是會員資料表 
// mytest 是不重複的 ID 資料表

SELECT
  id,
  sex,
  birthday,
  2024 - SUBSTR(birthday, 1, 4) AS age,
  CASE
    WHEN 2024 - SUBSTR(birthday, 1, 4) < 20
      THEN '1'
    WHEN 2024 - SUBSTR(birthday, 1, 4) >= 20
      AND 2024 - SUBSTR(birthday, 1, 4) < 30
      THEN '2'
    WHEN 2024 - SUBSTR(birthday, 1, 4) >= 30
      AND 2024 - SUBSTR(birthday, 1, 4) < 40
      THEN '3'
    WHEN 2024 - SUBSTR(birthday, 1, 4) >= 40
      AND 2024 - SUBSTR(birthday, 1, 4) < 50
      THEN '4'
    WHEN 2024 - SUBSTR(birthday, 1, 4) >= 50
      THEN '5'
    ELSE 'unknow'
  END AS 'age_group'
FROM customer
RIGHT JOIN mytest
  ON customer.id = mytest.customer_id
WHERE birthday IS NOT NULL
ORDER BY id ASC;
系統

檢查 Email 格式

為了發 EDM 整理網站的會員 email。

發現好多奇怪的 email….

到底是怎麼通過驗證的呢?!

問了 ChatGPT,修改一下,下面是用 Node.js 驗證 email 格式的 code,有用到 isemail 這個 package。

email.txt 裡的 email 為一行一個。

const fs = require('fs');

const isEmail = require('isemail');

let emails;

fs.readFile('./email.txt', 'utf8', (err, data) => {
  if (err) {
    console.error('Err:', err);
    return;
  }

  emails = data.split(/\r?\n|\r/).filter((line) => line.length > 0);

  console.log('emails.length:', emails.length);

  emails.forEach((email) => {
    if (!isEmail.validate(email)) {
      console.log(email);
    }

    // if (isEmail.validate(email, { errorLevel: true }) !== 0) {
    //  console.log(email);
    // }
  });
});
系統

使用 awk 擷取 Log 檔的特定欄位

想要從 Apache 的 log 檔擷取特定欄位。

下面是問 ChatGPT 的回答:

// 要使用 awk 指令從 Apache 日誌中提取第一欄(IP 地址)和第四欄(時間戳)的數據
// 可以使用以下命令
// '{print $1, $4}' 意思是印出第一欄和第四欄
$ awk '{print $1, $4}' log_file

// 不過,第四欄的時間戳會包含方括號,你可以用 awk 去除方括號,使輸出更乾淨
// gsub(/\[|\]/, "", $4) 是 awk 的一個函數調用,用來替換 $4 欄中的方括號 [ 和 ]
// 將其替換為空字符串 ""
$ awk '{gsub(/\[|\]/, "", $4); print $1, $4}' log_file

下面是我自己修改後的寫法:

// 欄位之間的 \t 是 tab 的意思
$ awk '{gsub(/\[|\]/, "", $4); gsub(/\"/, "", $7); print $1 "\t" $4 "\t" $7 "\t" $9 "\t" $10}' log_file
網頁

[JS] 日期

取得格式為 YYYY-MM-DD 的今天日期。

下面是問 ChatGPT 的結果:

const today = new Date();

const year = today.getFullYear();

// 月份從 0 開始,所以要加 1,並確保兩位數
const month = String(today.getMonth() + 1).padStart(2, '0');

// 確保日期為兩位數
const day = String(today.getDate()).padStart(2, '0');

const formattedDate = `${year}-${month}-${day}`;

// 2024-05-23
console.log(formattedDate);

在網路上還看到其他方式:

const today = new Date();

const year = today.getFullYear();
const month = ('0' + (today.getMonth() + 1)).slice(-2);
const date = ('0' + today.getDate()).slice(-2);

const hours = ('0' + today.getHours()).slice(-2);
const minutes = ('0' + today.getMinutes()).slice(-2);
const seconds = ('0' + today.getSeconds()).slice(-2);

// 2024-05-23 10:55:11
console.log(`${year}-${month}-${date} ${hours}:${minutes}:${seconds}`);

———

取得最近七天日期。

下面是問 ChatGPT 的結果:

const today = new Date();

for (let i = 0; i < 7; i++) {
  const tempDate = new Date();

  // 設定日期為今天減去 i 天
  tempDate.setDate(today.getDate() - i);

  const year = tempDate.getFullYear();

  // 月份從 0 開始,所以要加 1,並確保兩位數
  const month = String(tempDate.getMonth() + 1).padStart(2, '0');

  // 確保日期為兩位數
  const day = String(tempDate.getDate()).padStart(2, '0');

  const formattedDate = `${year}-${month}-${day}`;
  
  console.log(formattedDate);
}

在網路上看到的方式:

const today = new Date();

for (let i = 0; i < 7; i++) {
  const newDate = new Date(today.getTime() - i * 24 * 60 * 60 * 1000);
  
  const year = newDate.getFullYear();
  const month = String(newDate.getMonth() + 1).padStart(2, '0');
  const day = String(newDate.getDate()).padStart(2, '0');
  
  const formattedDate = `${year}-${month}-${day}`;
  
  console.log(formattedDate);
}


其他參考連結:

返回頂端